Example #1
0
        private async Task BasicScenario(bool enabled)
        {
            var options = new ClusterMembershipOptions {
                DefunctSiloCleanupPeriod = enabled ? new TimeSpan?(TimeSpan.FromMinutes(90)) : null, DefunctSiloExpiration = TimeSpan.FromDays(1)
            };
            var timers       = new List <DelegateAsyncTimer>();
            var timerCalls   = new ConcurrentQueue <(TimeSpan?DelayOverride, TaskCompletionSource <bool> Completion)>();
            var timerFactory = new DelegateAsyncTimerFactory(
                (period, name) =>
            {
                Assert.Equal(options.DefunctSiloCleanupPeriod.Value, period);
                var t = new DelegateAsyncTimer(
                    overridePeriod =>
                {
                    var task = new TaskCompletionSource <bool>();
                    timerCalls.Enqueue((overridePeriod, task));
                    return(task.Task);
                });
                timers.Add(t);
                return(t);
            });

            var table        = new InMemoryMembershipTable();
            var cleanupAgent = new MembershipTableCleanupAgent(
                Options.Create(options),
                table,
                this.loggerFactory.CreateLogger <MembershipTableCleanupAgent>(),
                timerFactory);
            var lifecycle = new SiloLifecycleSubject(this.loggerFactory.CreateLogger <SiloLifecycleSubject>());

            ((ILifecycleParticipant <ISiloLifecycle>)cleanupAgent).Participate(lifecycle);

            await lifecycle.OnStart();

            Assert.DoesNotContain(table.Calls, c => c.Method.Equals(nameof(IMembershipTable.CleanupDefunctSiloEntries)));

            Assert.Equal(enabled, timerCalls.TryDequeue(out var timer));
            timer.Completion?.TrySetResult(true);

            var stopped = lifecycle.OnStop();

            while (timerCalls.TryDequeue(out timer))
            {
                timer.Completion.TrySetResult(false);
            }
            if (enabled)
            {
                Assert.Contains(table.Calls, c => c.Method.Equals(nameof(IMembershipTable.CleanupDefunctSiloEntries)));
            }
            else
            {
                Assert.DoesNotContain(table.Calls, c => c.Method.Equals(nameof(IMembershipTable.CleanupDefunctSiloEntries)));
            }
            await stopped;
        }
Example #2
0
        public MembershipTableManagerTests(ITestOutputHelper output)
        {
            this.output        = output;
            this.loggerFactory = new LoggerFactory(new[] { new XunitLoggerProvider(this.output) });

            this.localSiloDetails = Substitute.For <ILocalSiloDetails>();
            this.localSilo        = Silo("127.0.0.1:100@100");
            this.localSiloDetails.SiloAddress.Returns(this.localSilo);
            this.localSiloDetails.DnsHostName.Returns("MyServer11");
            this.localSiloDetails.Name.Returns(Guid.NewGuid().ToString("N"));

            this.fatalErrorHandler  = Substitute.For <IFatalErrorHandler>();
            this.membershipGossiper = Substitute.For <IMembershipGossiper>();
            this.lifecycle          = new SiloLifecycleSubject(this.loggerFactory.CreateLogger <SiloLifecycleSubject>());
        }
Example #3
0
        public GrainLocatorTests(ITestOutputHelper output)
        {
            this.loggerFactory = new LoggerFactory(new[] { new XunitLoggerProvider(output) });
            this.lifecycle     = new SiloLifecycleSubject(this.loggerFactory.CreateLogger <SiloLifecycleSubject>());

            this.grainDirectory        = Substitute.For <IGrainDirectory>();
            this.localGrainDirectory   = Substitute.For <ILocalGrainDirectory>();
            this.mockMembershipService = new MockClusterMembershipService();

            this.grainLocator = new GrainLocator(
                this.grainDirectory,
                new DhtGrainLocator(this.localGrainDirectory),
                this.mockMembershipService.Target);

            this.grainLocator.Participate(this.lifecycle);
        }
Example #4
0
        public CachedGrainLocatorTests(ITestOutputHelper output)
        {
            this.loggerFactory = new LoggerFactory(new[] { new XunitLoggerProvider(output) });
            this.lifecycle     = new SiloLifecycleSubject(this.loggerFactory.CreateLogger <SiloLifecycleSubject>());

            this.grainDirectory         = Substitute.For <IGrainDirectory>();
            this.grainDirectoryResolver = Substitute.For <IGrainDirectoryResolver>();
            this.grainDirectoryResolver.Resolve(Arg.Any <LegacyGrainId>()).Returns(this.grainDirectory);
            this.grainDirectoryResolver.Directories.Returns(new[] { this.grainDirectory });
            this.localGrainDirectory   = Substitute.For <ILocalGrainDirectory>();
            this.mockMembershipService = new MockClusterMembershipService();

            this.grainLocator = new CachedGrainLocator(
                this.grainDirectoryResolver,
                new DhtGrainLocator(this.localGrainDirectory),
                this.mockMembershipService.Target);

            this.grainLocator.Participate(this.lifecycle);
        }
Example #5
0
        /// <summary>
        /// Returns a correct implementation of the persistence provider according to environment variables.
        /// </summary>
        /// <remarks>If the environment invariants have failed to hold upon creation of the storage provider,
        /// a <em>null</em> value will be provided.</remarks>
        public async Task <IGrainStorage> GetStorageProvider(string storageInvariant)
        {
            //Make sure the environment invariants hold before trying to give a functioning SUT instantiation.
            //This is done instead of the constructor to have more granularity on how the environment should be initialized.
            try
            {
                using (await StorageLock.LockAsync())
                {
                    if (AdoNetInvariants.Invariants.Contains(storageInvariant))
                    {
                        if (!StorageProviders.ContainsKey(storageInvariant))
                        {
                            Storage = Invariants.EnsureStorageForTesting(Invariants.ActiveSettings.ConnectionStrings.First(i => i.StorageInvariant == storageInvariant));

                            var options = new AdoNetGrainStorageOptions()
                            {
                                ConnectionString = Storage.Storage.ConnectionString,
                                Invariant        = storageInvariant
                            };
                            var clusterOptions = new ClusterOptions()
                            {
                                ServiceId = Guid.NewGuid().ToString()
                            };
                            var storageProvider = new AdoNetGrainStorage(DefaultProviderRuntime.ServiceProvider.GetService <ILogger <AdoNetGrainStorage> >(), DefaultProviderRuntime, Options.Create(options), Options.Create(clusterOptions), storageInvariant + "_StorageProvider");
                            ISiloLifecycleSubject siloLifeCycle = new SiloLifecycleSubject(NullLoggerFactory.Instance.CreateLogger <SiloLifecycleSubject>());
                            storageProvider.Participate(siloLifeCycle);
                            await siloLifeCycle.OnStart(CancellationToken.None);

                            StorageProviders[storageInvariant] = storageProvider;
                        }
                    }
                }
            }
            catch
            {
                StorageProviders.Add(storageInvariant, null);
            }

            return(StorageProviders[storageInvariant]);
        }
        public CachedGrainLocatorTests(ITestOutputHelper output)
        {
            this.loggerFactory = new LoggerFactory(new[] { new XunitLoggerProvider(output) });
            this.lifecycle     = new SiloLifecycleSubject(this.loggerFactory.CreateLogger <SiloLifecycleSubject>());

            this.grainDirectory = Substitute.For <IGrainDirectory>();
            var services = new ServiceCollection()
                           .AddSingleton(typeof(IKeyedServiceCollection <,>), typeof(KeyedServiceCollection <,>))
                           .AddSingletonKeyedService <string, IGrainDirectory>(GrainDirectoryAttribute.DEFAULT_GRAIN_DIRECTORY, (sp, name) => this.grainDirectory)
                           .BuildServiceProvider();

            this.grainDirectoryResolver = new GrainDirectoryResolver(
                services,
                new GrainPropertiesResolver(new NoOpClusterManifestProvider()),
                Array.Empty <IGrainDirectoryResolver>());
            this.mockMembershipService = new MockClusterMembershipService();

            this.grainLocator = new CachedGrainLocator(
                this.grainDirectoryResolver,
                this.mockMembershipService.Target);

            this.grainLocator.Participate(this.lifecycle);
        }