Exemplo n.º 1
0
        public async Task Simple_pubsub_tests()
        {
            var cluster = new TestClusterBuilder(3).Build();

            await cluster.DeployAsync();

            var sent = new HashSet <Guid>
            {
                Guid.NewGuid(),
                Guid.NewGuid(),
                Guid.NewGuid(),
            };

            var received1 = await CreateSubscriber(cluster.Client, sent.Count);

            var received2 = await CreateSubscriber(cluster.Client, sent.Count);

            var pubSub = new OrleansPubSub(cluster.Client);

            foreach (var message in sent)
            {
                pubSub.Publish(message);
            }

            await Task.WhenAny(
                Task.WhenAll(
                    received1,
                    received2
                    ),
                Task.Delay(10000));

            Assert.True(received1.Result.SetEquals(sent));
            Assert.True(received2.Result.SetEquals(sent));
        }
Exemplo n.º 2
0
        public async Task Should_use_async_local()
        {
            var cluster =
                new TestClusterBuilder(1)
                .Build();

            await cluster.DeployAsync();

            try
            {
                var grain = cluster.GrainFactory.GetGrain <IAsyncLocalGrain>(SingleGrain.Id);

                var result1 = await grain.GetValueAsync();

                var result2 = await grain.GetValueAsync();

                await cluster.KillSiloAsync(cluster.Silos[0]);

                await cluster.StartAdditionalSiloAsync();

                var result3 = await grain.GetValueAsync();

                Assert.Equal(1, result1);
                Assert.Equal(1, result2);
                Assert.Equal(1, result3);
            }
            finally
            {
                await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
            }
        }
        public async Task Should_make_request_with_json_serializer()
        {
            var cluster =
                new TestClusterBuilder(1)
                .Build();

            await cluster.DeployAsync();

            try
            {
                for (var i = 0; i < 100; i++)
                {
                    var id = DomainId.NewGuid().ToString();

                    var grain = cluster.GrainFactory.GetGrain <ICommandGrain>(id);

                    var result = await grain.ExecuteAsync(CommandRequest.Create(new TestCommand
                    {
                        Value = id
                    }));

                    Assert.Equal(id, result.Value.Payload);
                }
            }
            finally
            {
                await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
            }
        }
Exemplo n.º 4
0
        public async Task Should_communicate_with_orleans()
        {
            var cluster =
                new TestClusterBuilder(1)
                .Build();

            await cluster.DeployAsync();

            var culture   = CultureInfo.GetCultureInfo("de");
            var cultureUI = CultureInfo.GetCultureInfo("it");

            CultureInfo.CurrentCulture   = culture;
            CultureInfo.CurrentUICulture = cultureUI;

            var grain = cluster.Client.GetGrain <IContextGrain>("Default");

            var request = CommandRequest.Create(null !);

            var cultureFromGrain = await grain.GetCultureAsync(request);

            var cultureUIFromGrain = await grain.GetCultureAsync(request);

            Assert.Same(culture.Name, cultureFromGrain);
            Assert.Same(cultureUI.Name, cultureUIFromGrain);
        }
Exemplo n.º 5
0
        public async Task Should_communicate_with_orleans()
        {
            var cluster =
                new TestClusterBuilder(1)
                .Build();

            await cluster.DeployAsync();

            try
            {
                var culture   = CultureInfo.GetCultureInfo("de");
                var cultureUI = CultureInfo.GetCultureInfo("it");

                CultureInfo.CurrentCulture   = culture;
                CultureInfo.CurrentUICulture = cultureUI;

                var grain = cluster.Client.GetGrain <IContextGrain>("Default");

                var request = CommandRequest.Create(null !);

                var cultureFromGrain = await grain.GetCultureAsync(request);

                var cultureUIFromGrain = await grain.GetCultureUIAsync(request);

                Assert.Equal(culture.Name, cultureFromGrain);
                Assert.Equal(cultureUI.Name, cultureUIFromGrain);
            }
            finally
            {
                await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
            }
        }
        private static async Task <(TestCluster, IExceptionGrain)> GetGrainAsync()
        {
            var cluster =
                new TestClusterBuilder(1)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            return(cluster, cluster.GrainFactory.GetGrain <IExceptionGrain>(SingleGrain.Id));
        }
        private static async Task <TestCluster> CreateSilosAsync()
        {
            var cluster =
                new TestClusterBuilder(3)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            return(cluster);
        }
Exemplo n.º 8
0
        public async Task Should_receive_pubsub_message()
        {
            var cluster =
                new TestClusterBuilder(3)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            await WaitForClusterSizeAsync(cluster, 3);

            await PublishAsync(3);
        }
Exemplo n.º 9
0
        public async Task TestExchangeBasedTopology()
        {
            using var _cluster = new TestClusterBuilder()
                                 .AddSiloBuilderConfigurator <ExchangeTopologyClusterConfigurator>()
                                 .AddClientBuilderConfigurator <ExchangeTopologyClusterConfigurator>()
                                 .Build();

            await _cluster.DeployAsync();

            await _cluster.TestRmqStreamProviderOnFly(
                setupProxy : null,
                nMessages : 1000,
                itersToWait : 20);
        }
Exemplo n.º 10
0
        public async Task TestCustomNameTopology()
        {
            RmqHelpers.DeleteQueues(CustomQueueNames);
            using var _cluster = new TestClusterBuilder()
                                 .AddSiloBuilderConfigurator <CustomNameTopologyClusterConfigurator>()
                                 .AddClientBuilderConfigurator <CustomNameTopologyClusterConfigurator>()
                                 .Build();

            await _cluster.DeployAsync();

            await _cluster.TestRmqStreamProviderOnFly(
                setupProxy : null,
                nMessages : 1000,
                itersToWait : 20);
        }
Exemplo n.º 11
0
        public async Task Should_not_send_message_to_dead_but_not_unregistered_member()
        {
            var cluster =
                new TestClusterBuilder(3)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            await cluster.KillSiloAsync(cluster.Silos[1]);

            await PublishAsync(2);

            await cluster.DisposeAsync();
        }
Exemplo n.º 12
0
        public async Task Simple_grain_tests_with_mongo_exception()
        {
            var cluster =
                new TestClusterBuilder(1)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            var grain = cluster.GrainFactory.GetGrain <IExceptionGrain>(SingleGrain.Id);

            var ex = await Assert.ThrowsAsync <OrleansWrapperException>(() => grain.ThrowMongoAsync());

            Assert.Equal(typeof(MongoWriteException), ex.ExceptionType);
        }
Exemplo n.º 13
0
        public async Task Should_send_message_to_new_member()
        {
            var cluster =
                new TestClusterBuilder(3)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            cluster.StartAdditionalSilo();

            await WaitForClusterSizeAsync(cluster, 4);

            await PublishAsync(4);

            await cluster.DisposeAsync();
        }
Exemplo n.º 14
0
        public async Task Should_not_send_message_to_dead_member()
        {
            var cluster =
                new TestClusterBuilder(3)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            await cluster.StopSiloAsync(cluster.Silos[1]);

            await WaitForClusterSizeAsync(cluster, 2);

            await PublishAsync(2);

            await cluster.DisposeAsync();
        }
        protected override async Task <(DomainObject[], TestCluster)> CreateObjectsAsync()
        {
            var cluster =
                new TestClusterBuilder(3)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            var domainObjects =
                cluster.Silos.OfType <InProcessSiloHandle>()
                .Select(x =>
            {
                var pubSub = x.SiloHost.Services.GetRequiredService <IPubSub>();

                return(DomainObject.Create(pubSub));
            }).ToArray();

            return(domainObjects, cluster);
        }
        public async Task Should_distribute_and_cache_domain_objects(short numSilos, int numRuns, int expectedCounts, bool shouldBreak)
        {
            var env = new GrainEnvironment();

            var cluster =
                new TestClusterBuilder(numSilos)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            try
            {
                var indexes = GetIndexes(shouldBreak, env, cluster);

                var appId = env.AppId;

                var random = new Random();

                for (var i = 0; i < numRuns; i++)
                {
                    var contributorId      = Guid.NewGuid().ToString();
                    var contributorCommand = new AssignContributor {
                        ContributorId = contributorId, AppId = appId
                    };

                    var commandContext = new CommandContext(contributorCommand, A.Fake <ICommandBus>());

                    var randomIndex = indexes[random.Next(numSilos)];

                    await randomIndex.HandleAsync(commandContext, x =>
                    {
                        if (x.Command is AssignContributor command)
                        {
                            env.HandleCommand(command);
                        }

                        x.Complete(true);

                        return(Task.CompletedTask);
                    });

                    foreach (var index in indexes)
                    {
                        var appById = await index.GetAppAsync(appId.Id, true);

                        var appByName = await index.GetAppByNameAsync(appId.Name, true);

                        if (index == randomIndex || !shouldBreak || i == 0)
                        {
                            Assert.True(appById?.Contributors.ContainsKey(contributorId));
                            Assert.True(appByName?.Contributors.ContainsKey(contributorId));
                        }
                        else
                        {
                            Assert.False(appById?.Contributors.ContainsKey(contributorId));
                            Assert.False(appByName?.Contributors.ContainsKey(contributorId));
                        }
                    }
                }

                env.VerifyGrainAccess(expectedCounts);
            }
            finally
            {
                await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
            }
        }
        public async Task Should_retrieve_new_app(short numSilos, bool shouldBreak)
        {
            var env = new GrainEnvironment();

            var cluster =
                new TestClusterBuilder(numSilos)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            try
            {
                var indexes = GetIndexes(shouldBreak, env, cluster);

                var appId = env.AppId;

                foreach (var index in indexes)
                {
                    Assert.Null(await index.GetAppAsync(appId.Id, true));
                    Assert.Null(await index.GetAppByNameAsync(appId.Name, true));
                }

                var creatorId     = Guid.NewGuid().ToString();
                var creatorToken  = RefToken.User(creatorId);
                var createCommand = new CreateApp {
                    Actor = creatorToken, AppId = appId.Id
                };

                var commandContext = new CommandContext(createCommand, A.Fake <ICommandBus>());

                var randomIndex = indexes[new Random().Next(3)];

                await indexes[0].HandleAsync(commandContext, x =>
                {
                    if (x.Command is CreateApp command)
                    {
                        env.HandleCommand(command);
                    }

                    x.Complete(true);

                    return(Task.CompletedTask);
                });

                foreach (var index in indexes)
                {
                    var appById = await index.GetAppAsync(appId.Id, true);

                    var appByName = await index.GetAppByNameAsync(appId.Name, true);

                    if (index == randomIndex || !shouldBreak)
                    {
                        Assert.True(appById?.Contributors.ContainsKey(creatorId));
                        Assert.True(appByName?.Contributors.ContainsKey(creatorId));
                    }
                    else
                    {
                        Assert.False(appById?.Contributors.ContainsKey(creatorId));
                        Assert.False(appByName?.Contributors.ContainsKey(creatorId));
                    }
                }
            }
            finally
            {
                await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
            }
        }
        public async Task Should_distribute_and_cache_domain_objects(short numSilos, int numRuns, int expectedCounts, bool shouldBreak)
        {
            var env = new GrainEnvironment();

            var cluster =
                new TestClusterBuilder(numSilos)
                .AddSiloBuilderConfigurator <Configurator>()
                .Build();

            await cluster.DeployAsync();

            try
            {
                var indexes =
                    cluster.Silos.OfType <InProcessSiloHandle>()
                    .Select(x =>
                {
                    var pubSub =
                        shouldBreak ?
                        A.Fake <IPubSub>() :
                        x.SiloHost.Services.GetRequiredService <IPubSub>();

                    var cache =
                        new ReplicatedCache(
                            new MemoryCache(Options.Create(new MemoryCacheOptions())),
                            pubSub,
                            Options.Create(new ReplicatedCacheOptions {
                        Enable = true
                    }));

                    return(new SchemasIndex(env.GrainFactory, cache));
                }).ToArray();

                var appId = env.AppId;

                var random = new Random();

                for (var i = 0; i < numRuns; i++)
                {
                    var fieldName    = Guid.NewGuid().ToString();
                    var fieldCommand = new AddField {
                        Name = fieldName, SchemaId = env.SchemaId, AppId = env.AppId
                    };

                    var commandContext = new CommandContext(fieldCommand, A.Fake <ICommandBus>());

                    var randomIndex = indexes[random.Next(numSilos)];

                    await randomIndex.HandleAsync(commandContext, x =>
                    {
                        if (x.Command is AddField command)
                        {
                            env.HandleCommand(command);
                        }

                        x.Complete(true);

                        return(Task.CompletedTask);
                    });

                    foreach (var index in indexes)
                    {
                        var schemaById = await index.GetSchemaAsync(appId.Id, env.SchemaId.Id, true);

                        var schemaByName = await index.GetSchemaByNameAsync(appId.Id, env.SchemaId.Name, true);

                        if (index == randomIndex || !shouldBreak || i == 0)
                        {
                            Assert.True(schemaById?.SchemaDef.FieldsByName.ContainsKey(fieldName));
                            Assert.True(schemaByName?.SchemaDef.FieldsByName.ContainsKey(fieldName));
                        }
                        else
                        {
                            Assert.False(schemaById?.SchemaDef.FieldsByName.ContainsKey(fieldName));
                            Assert.False(schemaByName?.SchemaDef.FieldsByName.ContainsKey(fieldName));
                        }
                    }
                }

                env.VerifyGrainAccess(expectedCounts);
            }
            finally
            {
                await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
            }
        }