public async Task AddReplicaAsync(ReplicaRole role, long?replicaId = null, int activationDelayMs = 0)
        {
            var serviceContext = MockStatefulServiceContextFactory.Create(CodePackageActivationContext, ServiceTypeName, ServiceUri, Guid.NewGuid(), replicaId ?? _random.Next());
            var replica        = new MockStatefulServiceReplica <TStatefulService>(_serviceFactory, serviceContext, _stateManager);
            await replica.CreateAsync(role);

            _replicas.Add(replica);
        }
        public async Task AddReplicaAsync(ReplicaRole role, long?replicaId = null, int activationDelayMs = 0, byte[] initializationData = null)
        {
            if (!replicaId.HasValue)
            {
                replicaId = _random.Next();
            }
            var serviceContext = MockStatefulServiceContextFactory.Create(CodePackageActivationContext, ServiceTypeName, ServiceUri, Guid.NewGuid(), replicaId.Value, initializationData);
            var stateManager   = _stateManagerFactory(serviceContext, _reliableStates);
            var replica        = new MockStatefulServiceReplica <TStatefulService>(_serviceFactory, serviceContext, stateManager);
            await replica.CreateAsync(role);

            _replicaDictionary.TryAdd(replicaId.Value, replica);
        }
        public void TestCustomContext()
        {
            var newUri          = new Uri("fabric:/MockApp/OtherMockStatefulService");
            var serviceTypeName = "OtherMockServiceType";
            var partitionId     = Guid.NewGuid();
            var replicaId       = long.MaxValue;
            var context         = new MockCodePackageActivationContext("fabric:/MyApp", "MyAppType", "Code", "Ver", "Context", "Log", "Temp", "Work", "Man", "ManVer");

            var serviceContext = MockStatefulServiceContextFactory.Create(context, serviceTypeName, newUri, partitionId, replicaId);

            var instance = MockActorServiceFactory.CreateActorServiceForActor <MockActor>(context: serviceContext);

            Assert.IsInstanceOfType(instance, typeof(MockActorService <MockActor>));
            Assert.AreEqual(serviceContext, instance.Context);
        }
Esempio n. 4
0
        public Task RegisterAsync(
            string serviceTypeName,
            Func <StatefulServiceContext, StatefulService> serviceFactory,
            CancellationToken cancellationToken)
        {
            var context = MockStatefulServiceContextFactory.Create(
                MockCodePackageActivationContext.Default,
                serviceTypeName,
                new Uri(MockStatefulServiceContextFactory.ServiceName),
                Guid.Empty,
                default);

            this.serviceReplica = new MockStatefulServiceReplica(serviceFactory(context));
            return(this.serviceReplica.InitiateStartupSequenceAsync());
        }
Esempio n. 5
0
        public static StatefulServiceContext GetMockContext()
        {
            //build ConfigurationSectionCollection
            var configSections = new ConfigurationSectionCollection();

            //Build ConfigurationSettings
            var configSettings = CreateConfigurationSettings(configSections);

            ConfigurationSection configSection = CreateConfigurationSection("ClusterConfig");

            configSections.Add(configSection);

            ConfigurationProperty reverseProxyPort = CreateConfigurationSectionParameters("ReverseProxy_Port", "19081");

            configSection.Parameters.Add(reverseProxyPort);

            ConfigurationProperty maxTradesPending = CreateConfigurationSectionParameters("MaxTradesPending", "10");

            configSection.Parameters.Add(maxTradesPending);

            ConfigurationProperty appInsightsKey = CreateConfigurationSectionParameters("Admin_AppInsights_InstrumentationKey", "");

            configSection.Parameters.Add(appInsightsKey);

            ConfigurationProperty teamName = CreateConfigurationSectionParameters("TeamName", "");

            configSection.Parameters.Add(teamName);


            //Build ConfigurationPackage
            ConfigurationPackage configPackage = CreateConfigurationPackage(configSettings);
            var context = new MockCodePackageActivationContext(
                "fabric:/MockApp",
                "MockAppType",
                "Code",
                "1.0.0.0",
                Guid.NewGuid().ToString(),
                @"C:\logDirectory",
                @"C:\tempDirectory",
                @"C:\workDirectory",
                "ServiceManifestName",
                "1.0.0.0")
            {
                ConfigurationPackage = configPackage
            };

            return(MockStatefulServiceContextFactory.Create(context, "barry", new Uri("fabric:/barry/norman"), Guid.NewGuid(), 1));
        }
Esempio n. 6
0
        public void TestCustom_WithInitData()
        {
            var newUri          = new Uri("fabric:/MockApp/OtherMockStatefulService");
            var serviceTypeName = "OtherMockServiceType";
            var partitionId     = Guid.NewGuid();
            var replicaId       = long.MaxValue;
            var context         = new MockCodePackageActivationContext("fabric:/MyApp", "MyAppType", "Code", "Ver", "Context", "Log", "Temp", "Work", "Man", "ManVer");

            var instance = MockStatefulServiceContextFactory.Create(context, serviceTypeName, newUri, partitionId, replicaId, Encoding.UTF8.GetBytes("blah"));

            Assert.IsInstanceOfType(instance, typeof(StatefulServiceContext));
            Assert.AreEqual(context, instance.CodePackageActivationContext);
            Assert.AreEqual(newUri, instance.ServiceName);
            Assert.AreEqual(serviceTypeName, instance.ServiceTypeName);
            Assert.AreEqual(partitionId, instance.PartitionId);
            Assert.AreEqual(replicaId, instance.ReplicaId);
            Assert.AreEqual(replicaId, instance.ReplicaOrInstanceId);
            CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("blah"), instance.InitializationData);
        }
        public void TestDeadLock9()
        {
            Func <StatefulServiceContext, IReliableStateManagerReplica2, StatefulServiceWithReplicaListener> serviceFactory = (context, stateManagerReplica) =>
            {
                var partition = new MockStatefulServicePartition()
                {
                    PartitionInfo = MockQueryPartitionFactory.CreateSingletonPartitonInfo(Guid.NewGuid())
                };
                context = MockStatefulServiceContextFactory.Create(
                    context.CodePackageActivationContext,
                    context.ServiceTypeName,
                    context.ServiceName,
                    partition.PartitionInfo.Id,
                    context.ReplicaId);

                var service = new StatefulServiceWithReplicaListener(context);
                service.SetPartition(partition);
                return(service);
            };

            //shared instance, called from multiple threads
            MockStatefulServiceReplicaSet <StatefulServiceWithReplicaListener> replicaSet;

            Parallel.For(1, 100, async(i) =>
            {
                replicaSet = new MockStatefulServiceReplicaSet <StatefulServiceWithReplicaListener>(serviceFactory);
                await replicaSet.AddReplicaAsync(ReplicaRole.Primary, 1);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 2);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 3);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 4);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 5);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 6);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 7);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 8);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 9);
                await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 10);
            });
        }
        public static void Should_invoke_packages_events_On_stateful_service_package_activation_events()
        {
            // Arrange
            var actualCallStack = new ConcurrentQueue <string>();

            var mockCodePackageActivationContext = new Mock <ICodePackageActivationContext>();

            mockCodePackageActivationContext
            .Setup(instance => instance.ApplicationName)
            .Returns("Application");

            mockCodePackageActivationContext
            .Setup(instance => instance.ApplicationTypeName)
            .Returns("ApplicationType");

            var mockDelegateCodePackageAdded = new Mock <Action>();

            mockDelegateCodePackageAdded
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("codepackage.added"))
            .Verifiable();

            var mockDelegateCodePackageModified = new Mock <Action>();

            mockDelegateCodePackageModified
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("codepackage.modified"))
            .Verifiable();

            var mockDelegateCodePackageRemoved = new Mock <Action>();

            mockDelegateCodePackageRemoved
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("codepackage.removed"))
            .Verifiable();

            var mockDelegateConfigPackageAdded = new Mock <Action>();

            mockDelegateConfigPackageAdded
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("configpackage.added"))
            .Verifiable();

            var mockDelegateConfigPackageModified = new Mock <Action>();

            mockDelegateConfigPackageModified
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("configpackage.modified"))
            .Verifiable();

            var mockDelegateConfigPackageRemoved = new Mock <Action>();

            mockDelegateConfigPackageRemoved
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("configpackage.removed"))
            .Verifiable();

            var mockDelegateDataPackageAdded = new Mock <Action>();

            mockDelegateDataPackageAdded
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("datapackage.added"))
            .Verifiable();

            var mockDelegateDataPackageModified = new Mock <Action>();

            mockDelegateDataPackageModified
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("datapackage.modified"))
            .Verifiable();

            var mockDelegateDataPackageRemoved = new Mock <Action>();

            mockDelegateDataPackageRemoved
            .Setup(instance => instance())
            .Callback(() => actualCallStack.Enqueue("datapackage.removed"))
            .Verifiable();

            var mockEventSourceReplicator = MockEventSourceReplicator();
            var mockDelegateReplicators   = new[]
            {
                MockDelegateReplicatorForEvent(
                    mockDelegateCodePackageAdded.Object,
                    StatefulServiceLifecycleEvent.OnCodePackageAdded),
                MockDelegateReplicatorForEvent(
                    mockDelegateCodePackageModified.Object,
                    StatefulServiceLifecycleEvent.OnCodePackageModified),
                MockDelegateReplicatorForEvent(
                    mockDelegateCodePackageRemoved.Object,
                    StatefulServiceLifecycleEvent.OnCodePackageRemoved),
                MockDelegateReplicatorForEvent(
                    mockDelegateConfigPackageAdded.Object,
                    StatefulServiceLifecycleEvent.OnConfigPackageAdded),
                MockDelegateReplicatorForEvent(
                    mockDelegateConfigPackageModified.Object,
                    StatefulServiceLifecycleEvent.OnConfigPackageModified),
                MockDelegateReplicatorForEvent(
                    mockDelegateConfigPackageRemoved.Object,
                    StatefulServiceLifecycleEvent.OnConfigPackageRemoved),
                MockDelegateReplicatorForEvent(
                    mockDelegateDataPackageAdded.Object,
                    StatefulServiceLifecycleEvent.OnDataPackageAdded),
                MockDelegateReplicatorForEvent(
                    mockDelegateDataPackageModified.Object,
                    StatefulServiceLifecycleEvent.OnDataPackageModified),
                MockDelegateReplicatorForEvent(
                    mockDelegateDataPackageRemoved.Object,
                    StatefulServiceLifecycleEvent.OnDataPackageRemoved)
            };

            var statefulContext = MockStatefulServiceContextFactory.Create(
                mockCodePackageActivationContext.Object,
                "Mock",
                new Uri("fabric:/mock"),
                Guid.NewGuid(),
                0);

            var statefulInstance = new StatefulService(
                statefulContext,
                mockEventSourceReplicator,
                mockDelegateReplicators,
                Array.Empty <IStatefulServiceHostListenerReplicator>());

            // Act
            mockCodePackageActivationContext
            .Raise(instance => instance.CodePackageAddedEvent -= null, new PackageAddedEventArgs <CodePackage>());
            mockCodePackageActivationContext
            .Raise(instance => instance.CodePackageModifiedEvent -= null, new PackageModifiedEventArgs <CodePackage>());
            mockCodePackageActivationContext
            .Raise(instance => instance.CodePackageRemovedEvent -= null, new PackageRemovedEventArgs <CodePackage>());

            mockCodePackageActivationContext
            .Raise(instance => instance.ConfigurationPackageAddedEvent -= null, new PackageAddedEventArgs <ConfigurationPackage>());
            mockCodePackageActivationContext
            .Raise(instance => instance.ConfigurationPackageModifiedEvent -= null, new PackageModifiedEventArgs <ConfigurationPackage>());
            mockCodePackageActivationContext
            .Raise(instance => instance.ConfigurationPackageRemovedEvent -= null, new PackageRemovedEventArgs <ConfigurationPackage>());

            mockCodePackageActivationContext
            .Raise(instance => instance.DataPackageAddedEvent -= null, new PackageAddedEventArgs <DataPackage>());
            mockCodePackageActivationContext
            .Raise(instance => instance.DataPackageModifiedEvent -= null, new PackageModifiedEventArgs <DataPackage>());
            mockCodePackageActivationContext
            .Raise(instance => instance.DataPackageRemovedEvent -= null, new PackageRemovedEventArgs <DataPackage>());

            // Assert
            mockDelegateCodePackageAdded.Verify();
            mockDelegateCodePackageModified.Verify();
            mockDelegateCodePackageRemoved.Verify();
            mockDelegateConfigPackageAdded.Verify();
            mockDelegateConfigPackageModified.Verify();
            mockDelegateConfigPackageRemoved.Verify();
            mockDelegateDataPackageAdded.Verify();
            mockDelegateDataPackageModified.Verify();
            mockDelegateDataPackageRemoved.Verify();

            Assert.Equal(9, actualCallStack.Count);

            Assert.Equal(
                "codepackage.added",
                actualCallStack.TryDequeue(out var result)
                    ? result
                    : null);

            Assert.Equal(
                "codepackage.modified",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "codepackage.removed",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "configpackage.added",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "configpackage.modified",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "configpackage.removed",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "datapackage.added",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "datapackage.modified",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);

            Assert.Equal(
                "datapackage.removed",
                actualCallStack.TryDequeue(out result)
                    ? result
                    : null);
        }