示例#1
0
        public void ListenerExceptionOnAbort()
        {
            Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: ListenerExceptionOnAbort");

            var serviceContext = TestMocksRepository.GetMockStatefulServiceContext();

            var testService = new StatefulBaseTestService(serviceContext)
            {
                EnableListenerExceptionOnAbort = true
            };

            IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, testService);

            var partition = new Mock <IStatefulServicePartition>();

            testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult();

            testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult();

            testServiceReplica.Abort();
        }
示例#2
0
        public void CommunicationListenerLifeCycle_P_S_P_N_ListenOnSecondary()
        {
            Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: CommunicationListenerLifeCycle_P_S_P_N_ListenOnSecondary");

            var serviceContext = TestMocksRepository.GetMockStatefulServiceContext();

            var testService = new StatefulBaseTestService(serviceContext)
            {
                ListenOnSecondary = true
            };

            IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, testService);

            var partition = new Mock <IStatefulServicePartition>();

            testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult();

            Console.WriteLine(@"// U -> P");
            testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult();
            {
                const int expectedCount = 1;
                int       actualCount   = testService.Listeners.Count;
                actualCount.Should().Be(expectedCount, "listener has been opened only once(U->P)");
                testService.Listeners.Last().Should().Be(testService.CurrentListener);
                ((StatefulServiceReplicaAdapter)testServiceReplica).Test_CommunicationListeners.First().Should().Be(testService.CurrentListener.Object);

                testService.CurrentListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                testService.CurrentListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Never());
                testService.CurrentListener.Verify(l => l.Abort(), Times.Never());
            }

            Console.WriteLine(@"// P -> S");
            testServiceReplica.ChangeRoleAsync(ReplicaRole.ActiveSecondary, CancellationToken.None).GetAwaiter().GetResult();
            {
                const int expectedCount = 2;
                int       actualCount   = testService.Listeners.Count;
                actualCount.Should().Be(expectedCount, "listener has been opened twice(U->P->S)");
                testService.Listeners.Last().Should().Be(testService.CurrentListener);
                ((StatefulServiceReplicaAdapter)testServiceReplica).Test_CommunicationListeners.First().Should().Be(testService.CurrentListener.Object);

                var firstListener = testService.Listeners[0];
                firstListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                firstListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Once());
                firstListener.Verify(l => l.Abort(), Times.Never());

                testService.CurrentListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                testService.CurrentListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Never());
                testService.CurrentListener.Verify(l => l.Abort(), Times.Never());
            }

            Console.WriteLine(@"// S -> P");
            testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult();
            {
                const int expectedCount = 3;
                int       actualCount   = testService.Listeners.Count;
                actualCount.Should().Be(expectedCount, "listener has been opened three times(U->P->S->P)");
                testService.Listeners.Last().Should().Be(testService.CurrentListener);
                ((StatefulServiceReplicaAdapter)testServiceReplica).Test_CommunicationListeners.First().Should().Be(testService.CurrentListener.Object);

                var firstListener = testService.Listeners[0];
                firstListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                firstListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Once());
                firstListener.Verify(l => l.Abort(), Times.Never());

                var secondListener = testService.Listeners[1];
                secondListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                secondListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Once());
                secondListener.Verify(l => l.Abort(), Times.Never());

                testService.CurrentListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                testService.CurrentListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Never());
                testService.CurrentListener.Verify(l => l.Abort(), Times.Never());
            }

            Console.WriteLine(@"// P -> N");
            testServiceReplica.ChangeRoleAsync(ReplicaRole.None, CancellationToken.None).GetAwaiter().GetResult();
            {
                const int expectedCount = 3;
                int       actualCount   = testService.Listeners.Count;
                actualCount.Should().Be(expectedCount, "listener has been opened three times(U->P->S->P->N)");
                testService.Listeners.Last().Should().Be(testService.CurrentListener);
                ((StatefulServiceReplicaAdapter)testServiceReplica).Test_CommunicationListeners.Should().BeNull();

                var firstListener = testService.Listeners[0];
                firstListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                firstListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Once());
                firstListener.Verify(l => l.Abort(), Times.Never());

                var secondListener = testService.Listeners[1];
                secondListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                secondListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Once());
                secondListener.Verify(l => l.Abort(), Times.Never());

                testService.CurrentListener.Verify(l => l.OpenAsync(It.IsAny <CancellationToken>()), Times.Once());
                testService.CurrentListener.Verify(l => l.CloseAsync(It.IsAny <CancellationToken>()), Times.Once());
                testService.CurrentListener.Verify(l => l.Abort(), Times.Never());
            }

            partition.Verify(p => p.ReportFault(It.IsAny <FaultType>()), Times.Never());
        }