2017-02-10 20:36:26 +03:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2015-12-18 01:29:49 +03:00
|
|
|
using Orleans.Runtime;
|
2016-10-20 06:33:02 +03:00
|
|
|
using TestExtensions;
|
2015-12-18 01:29:49 +03:00
|
|
|
using UnitTests.GrainInterfaces;
|
2016-07-06 00:50:27 +03:00
|
|
|
using Xunit;
|
2015-12-18 01:29:49 +03:00
|
|
|
|
2016-12-14 01:19:54 +03:00
|
|
|
namespace DefaultCluster.Tests
|
2015-12-18 01:29:49 +03:00
|
|
|
{
|
2016-02-24 22:21:56 +03:00
|
|
|
public class ClientAddressableTests : HostedTestClusterEnsureDefaultStarted
|
2015-12-18 01:29:49 +03:00
|
|
|
{
|
|
|
|
private object anchor;
|
2023-07-07 17:57:28 +03:00
|
|
|
private readonly IRuntimeClient runtimeClient;
|
2015-12-18 01:29:49 +03:00
|
|
|
|
|
|
|
private class MyPseudoGrain : IClientAddressableTestClientObject
|
|
|
|
{
|
|
|
|
private int counter = 0;
|
2023-07-07 17:57:28 +03:00
|
|
|
private readonly List<int> numbers = new List<int>();
|
2015-12-18 01:29:49 +03:00
|
|
|
|
|
|
|
public Task<string> OnHappyPath(string message)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(message))
|
|
|
|
throw new ArgumentException("target");
|
|
|
|
else
|
|
|
|
return Task.FromResult(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task OnSadPath(string message)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(message))
|
|
|
|
throw new ArgumentException("target");
|
|
|
|
else
|
|
|
|
throw new ApplicationException(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<int> OnSerialStress(int n)
|
|
|
|
{
|
2016-07-15 21:48:37 +03:00
|
|
|
Assert.Equal(this.counter, n);
|
2015-12-18 01:29:49 +03:00
|
|
|
++this.counter;
|
|
|
|
return Task.FromResult(10000 + n);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<int> OnParallelStress(int n)
|
|
|
|
{
|
|
|
|
this.numbers.Add(n);
|
|
|
|
return Task.FromResult(10000 + n);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void VerifyNumbers(int iterationCount)
|
|
|
|
{
|
2016-07-15 21:48:37 +03:00
|
|
|
Assert.Equal(iterationCount, this.numbers.Count);
|
2015-12-18 01:29:49 +03:00
|
|
|
this.numbers.Sort();
|
|
|
|
for (var i = 0; i < this.numbers.Count; ++i)
|
2016-07-15 21:48:37 +03:00
|
|
|
Assert.Equal(i, this.numbers[i]);
|
2015-12-18 01:29:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class MyProducer : IClientAddressableTestProducer
|
|
|
|
{
|
2023-09-01 18:40:37 +03:00
|
|
|
private int counter = 0;
|
2015-12-18 01:29:49 +03:00
|
|
|
|
|
|
|
public Task<int> Poll()
|
|
|
|
{
|
|
|
|
++this.counter;
|
|
|
|
return Task.FromResult(this.counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 00:39:41 +03:00
|
|
|
public ClientAddressableTests(DefaultClusterFixture fixture) : base(fixture)
|
|
|
|
{
|
2017-02-10 20:36:26 +03:00
|
|
|
this.runtimeClient = this.HostedCluster.ServiceProvider.GetRequiredService<IRuntimeClient>();
|
2017-01-19 00:39:41 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 00:11:51 +03:00
|
|
|
[Fact, TestCategory("BVT"), TestCategory("ClientAddressable")]
|
2015-12-18 01:29:49 +03:00
|
|
|
public async Task TestClientAddressableHappyPath()
|
|
|
|
{
|
|
|
|
var myOb = new MyPseudoGrain();
|
|
|
|
this.anchor = myOb;
|
2017-03-07 16:05:35 +03:00
|
|
|
var myRef = ((IInternalGrainFactory)this.GrainFactory).CreateObjectReference<IClientAddressableTestClientObject>(myOb);
|
2017-01-19 00:39:41 +03:00
|
|
|
var proxy = this.GrainFactory.GetGrain<IClientAddressableTestGrain>(GetRandomGrainId());
|
2015-12-18 01:29:49 +03:00
|
|
|
const string expected = "o hai!";
|
|
|
|
await proxy.SetTarget(myRef);
|
|
|
|
var actual = await proxy.HappyPath(expected);
|
2016-07-15 21:48:37 +03:00
|
|
|
Assert.Equal(expected, actual);
|
2015-12-18 01:29:49 +03:00
|
|
|
|
2017-02-10 20:36:26 +03:00
|
|
|
this.runtimeClient.DeleteObjectReference(myRef);
|
2015-12-18 01:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 00:11:51 +03:00
|
|
|
[Fact, TestCategory("BVT"), TestCategory("ClientAddressable")]
|
2015-12-18 01:29:49 +03:00
|
|
|
public async Task TestClientAddressableSadPath()
|
|
|
|
{
|
2016-02-24 22:21:56 +03:00
|
|
|
const string message = "o hai!";
|
|
|
|
|
|
|
|
var myOb = new MyPseudoGrain();
|
|
|
|
this.anchor = myOb;
|
2017-03-07 16:05:35 +03:00
|
|
|
var myRef = ((IInternalGrainFactory)this.GrainFactory).CreateObjectReference<IClientAddressableTestClientObject>(myOb);
|
2017-01-19 00:39:41 +03:00
|
|
|
var proxy = this.GrainFactory.GetGrain<IClientAddressableTestGrain>(GetRandomGrainId());
|
2016-02-24 22:21:56 +03:00
|
|
|
await proxy.SetTarget(myRef);
|
|
|
|
|
2016-07-15 21:48:37 +03:00
|
|
|
await Assert.ThrowsAsync<ApplicationException>(() =>
|
2016-02-24 22:21:56 +03:00
|
|
|
proxy.SadPath(message)
|
|
|
|
);
|
|
|
|
|
2017-02-10 20:36:26 +03:00
|
|
|
this.runtimeClient.DeleteObjectReference(myRef);
|
2015-12-18 01:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 00:11:51 +03:00
|
|
|
[Fact, TestCategory("BVT"), TestCategory("ClientAddressable")]
|
2015-12-18 01:29:49 +03:00
|
|
|
public async Task GrainShouldSuccessfullyPullFromClientObject()
|
|
|
|
{
|
|
|
|
var myOb = new MyProducer();
|
|
|
|
this.anchor = myOb;
|
2017-03-07 16:05:35 +03:00
|
|
|
var myRef = ((IInternalGrainFactory)this.GrainFactory).CreateObjectReference<IClientAddressableTestProducer>(myOb);
|
2017-01-19 00:39:41 +03:00
|
|
|
var rendez = this.GrainFactory.GetGrain<IClientAddressableTestRendezvousGrain>(0);
|
|
|
|
var consumer = this.GrainFactory.GetGrain<IClientAddressableTestConsumer>(0);
|
2015-12-18 01:29:49 +03:00
|
|
|
|
|
|
|
await rendez.SetProducer(myRef);
|
|
|
|
await consumer.Setup();
|
|
|
|
var n = await consumer.PollProducer();
|
2016-07-15 21:48:37 +03:00
|
|
|
Assert.Equal(1, n);
|
2015-12-18 01:29:49 +03:00
|
|
|
|
2017-02-10 20:36:26 +03:00
|
|
|
this.runtimeClient.DeleteObjectReference(myRef);
|
2015-12-18 01:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 00:11:51 +03:00
|
|
|
[Fact, TestCategory("BVT"), TestCategory("ClientAddressable")]
|
2015-12-18 01:29:49 +03:00
|
|
|
public async Task MicroClientAddressableSerialStressTest()
|
|
|
|
{
|
|
|
|
const int iterationCount = 1000;
|
|
|
|
|
|
|
|
var myOb = new MyPseudoGrain();
|
|
|
|
this.anchor = myOb;
|
2017-03-07 16:05:35 +03:00
|
|
|
var myRef = ((IInternalGrainFactory)this.GrainFactory).CreateObjectReference<IClientAddressableTestClientObject>(myOb);
|
2017-01-19 00:39:41 +03:00
|
|
|
var proxy = this.GrainFactory.GetGrain<IClientAddressableTestGrain>(GetRandomGrainId());
|
2015-12-18 01:29:49 +03:00
|
|
|
await proxy.SetTarget(myRef);
|
|
|
|
await proxy.MicroSerialStressTest(iterationCount);
|
|
|
|
|
2017-02-10 20:36:26 +03:00
|
|
|
this.runtimeClient.DeleteObjectReference(myRef);
|
2015-12-18 01:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 00:11:51 +03:00
|
|
|
[Fact, TestCategory("BVT"), TestCategory("ClientAddressable")]
|
2015-12-18 01:29:49 +03:00
|
|
|
public async Task MicroClientAddressableParallelStressTest()
|
|
|
|
{
|
|
|
|
const int iterationCount = 1000;
|
|
|
|
|
|
|
|
var myOb = new MyPseudoGrain();
|
|
|
|
this.anchor = myOb;
|
2017-03-07 16:05:35 +03:00
|
|
|
var myRef = ((IInternalGrainFactory)this.GrainFactory).CreateObjectReference<IClientAddressableTestClientObject>(myOb);
|
2017-01-19 00:39:41 +03:00
|
|
|
var proxy = this.GrainFactory.GetGrain<IClientAddressableTestGrain>(GetRandomGrainId());
|
2015-12-18 01:29:49 +03:00
|
|
|
await proxy.SetTarget(myRef);
|
|
|
|
await proxy.MicroParallelStressTest(iterationCount);
|
|
|
|
|
2017-02-10 20:36:26 +03:00
|
|
|
this.runtimeClient.DeleteObjectReference(myRef);
|
2015-12-18 01:29:49 +03:00
|
|
|
|
|
|
|
myOb.VerifyNumbers(iterationCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|