public void CancellationDuringWriteStatus() { Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: CancellationDuringWriteStatus()"); var serviceContext = TestMocksRepository.GetMockStatefulServiceContext(); IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, new StatefulBaseTestService(serviceContext)); var partition = new Mock <IStatefulServicePartition>(); // This will make WaitForWriteStatusAsync to keep retrying in loop. partition.SetupGet(p => p.WriteStatus).Returns(PartitionAccessStatus.ReconfigurationPending); testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult(); Console.WriteLine(@"// U -> P"); testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult(); // This will throw if canceling during write status propagates out, as canceling of executeRunAsyncTask // (which includes waiting for write status) is awaited during change role away from primary. Console.WriteLine(@"// P -> S"); testServiceReplica.ChangeRoleAsync(ReplicaRole.ActiveSecondary, CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsAny <FaultType>()), Times.Never()); }
public void RunAsyncCancellation() { Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: RunAsyncCancellation()"); var serviceContext = TestMocksRepository.GetMockStatefulServiceContext(); var testService = new RunAsyncCancellationTestService(serviceContext); IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, testService); var partition = new Mock <IStatefulServicePartition>(); partition.SetupGet(p => p.WriteStatus).Returns(PartitionAccessStatus.Granted); testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult(); Console.WriteLine(@"// U -> P"); testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult(); CancellationTokenSource source = new CancellationTokenSource(10000); while (!testService.StartedWaiting) { Task.Delay(100, source.Token).GetAwaiter().GetResult(); } // This will throw if cancellation propagates out, as canceling of RunAsync is awaited during // change role away from primary. Console.WriteLine(@"// P -> S"); testServiceReplica.ChangeRoleAsync(ReplicaRole.ActiveSecondary, CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsAny <FaultType>()), Times.Never()); }
public void RunAsyncBlockingCall() { Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: RunAsyncBlockingCall()"); var serviceContext = TestMocksRepository.GetMockStatefulServiceContext(); var testService = new RunAsyncBlockingCallTestService(serviceContext); IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, testService); var partition = new Mock <IStatefulServicePartition>(); partition.SetupGet(p => p.WriteStatus).Returns(PartitionAccessStatus.Granted); testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult(); Console.WriteLine(@"// U -> P"); var changeRoleTask = testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None); CancellationTokenSource source = new CancellationTokenSource(10000); while (!testService.RunAsyncInvoked) { Task.Delay(100, source.Token).GetAwaiter().GetResult(); } Assert.True(changeRoleTask.IsCompleted && !changeRoleTask.IsCanceled && !changeRoleTask.IsFaulted); ((StatefulServiceReplicaAdapter)testServiceReplica).Test_IsRunAsyncTaskRunning().Should().BeTrue(); testServiceReplica.CloseAsync(CancellationToken.None).GetAwaiter().GetResult(); }
public void RunAsyncSlowCancellation() { Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: RunAsyncSlowCancellation()"); var serviceContext = TestMocksRepository.GetMockStatefulServiceContext(); var testService = new RunAsyncSlowCancellationTestService(serviceContext); IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, testService); var partition = new Mock <IStatefulServicePartition>(); partition.SetupGet(p => p.WriteStatus).Returns(PartitionAccessStatus.Granted); partition.Setup(p => p.ReportPartitionHealth(It.IsAny <HealthInformation>())); testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult(); Console.WriteLine(@"// U -> P"); testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult(); CancellationTokenSource source = new CancellationTokenSource(10000); while (!testService.RunAsyncInvoked) { Task.Delay(100, source.Token).GetAwaiter().GetResult(); } Console.WriteLine(@"// P -> S"); testServiceReplica.ChangeRoleAsync(ReplicaRole.ActiveSecondary, CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsAny <FaultType>()), Times.Never()); partition.Verify(p => p.ReportPartitionHealth(It.Is <HealthInformation>(hinfo => Utility.IsRunAsyncSlowCancellationHealthInformation(hinfo))), Times.AtLeastOnce); }
public void RunAsyncBlockingCall() { Console.WriteLine("StatelessServiceLifeCycleTests - Test Method: RunAsyncBlockingCall()"); var serviceContext = TestMocksRepository.GetMockStatelessServiceContext(); var testService = new RunAsyncBlockingCallTestService(serviceContext); IStatelessServiceInstance testServiceReplica = new StatelessServiceInstanceAdapter(serviceContext, testService); var partition = new Mock <IStatelessServicePartition>(); var openTask = testServiceReplica.OpenAsync(partition.Object, CancellationToken.None); CancellationTokenSource source = new CancellationTokenSource(10000); while (!testService.RunAsyncInvoked) { Task.Delay(100, source.Token).GetAwaiter().GetResult(); } Assert.True(openTask.IsCompleted && !openTask.IsCanceled && !openTask.IsFaulted); ((StatelessServiceInstanceAdapter)testServiceReplica).Test_IsRunAsyncTaskRunning().Should().BeTrue(); testServiceReplica.CloseAsync(CancellationToken.None).GetAwaiter().GetResult(); }
public void RunAsyncSlowCancellation() { Console.WriteLine("StatelessServiceLifeCycleTests - Test Method: RunAsyncSlowCancellation()"); var serviceContext = TestMocksRepository.GetMockStatelessServiceContext(); var testService = new RunAsyncSlowCancellationTestService(serviceContext); IStatelessServiceInstance testServiceReplica = new StatelessServiceInstanceAdapter(serviceContext, testService); var partition = new Mock <IStatelessServicePartition>(); partition.Setup(p => p.ReportPartitionHealth(It.IsAny <HealthInformation>())); testServiceReplica.OpenAsync(partition.Object, CancellationToken.None).GetAwaiter().GetResult(); CancellationTokenSource source = new CancellationTokenSource(10000); while (!testService.RunAsyncInvoked) { Task.Delay(100, source.Token).GetAwaiter().GetResult(); } testServiceReplica.CloseAsync(CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsAny <FaultType>()), Times.Never()); partition.Verify(p => p.ReportPartitionHealth(It.Is <HealthInformation>(hinfo => Utility.IsRunAsyncSlowCancellationHealthInformation(hinfo))), Times.AtLeastOnce); }
public void RunAsyncCancellation() { Console.WriteLine("StatelessServiceLifeCycleTests - Test Method: RunAsyncCancellation()"); var serviceContext = TestMocksRepository.GetMockStatelessServiceContext(); var testService = new RunAsyncCancellationTestService(serviceContext); IStatelessServiceInstance testServiceReplica = new StatelessServiceInstanceAdapter(serviceContext, testService); var partition = new Mock <IStatelessServicePartition>(); testServiceReplica.OpenAsync(partition.Object, CancellationToken.None).GetAwaiter().GetResult(); CancellationTokenSource source = new CancellationTokenSource(10000); while (!testService.StartedWaiting) { Task.Delay(100, source.Token).GetAwaiter().GetResult(); } // This will throw if cancellation propagates out, as canceling of RunAsync is // awaited during close of stateless serice instance. testServiceReplica.CloseAsync(CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsAny <FaultType>()), Times.Never()); }
public void ListenerExceptionOnAbort() { Console.WriteLine("StatelessServiceLifeCycleTests - Test Method: RunAsyncFail()"); var serviceContext = TestMocksRepository.GetMockStatelessServiceContext(); IStatelessServiceInstance testServiceInstance = new StatelessServiceInstanceAdapter(serviceContext, new ListenerExceptionOnAbortService(serviceContext)); var partition = new Mock <IStatelessServicePartition>(); testServiceInstance.OpenAsync(partition.Object, CancellationToken.None).GetAwaiter().GetResult(); // This will throw if listener exception propagates out testServiceInstance.Abort(); }
public void RunAsyncFail() { Console.WriteLine("StatefulServiceLifeCycleTests - Test Method: RunAsyncFail()"); var serviceContext = TestMocksRepository.GetMockStatefulServiceContext(); IStatefulServiceReplica testServiceReplica = new StatefulServiceReplicaAdapter(serviceContext, new RunAsyncFailTestService(serviceContext)); var tcs = new TaskCompletionSource <bool>(); var partition = new Mock <IStatefulServicePartition>(); partition.SetupGet(p => p.WriteStatus).Returns(PartitionAccessStatus.Granted); partition.Setup(p => p.ReportFault(It.IsAny <FaultType>())).Callback( () => { tcs.SetResult(true); }); partition.Setup(p => p.ReportPartitionHealth(It.IsAny <HealthInformation>())); testServiceReplica.OpenAsync(ReplicaOpenMode.New, partition.Object, CancellationToken.None).GetAwaiter().GetResult(); Console.WriteLine(@"// U -> P"); testServiceReplica.ChangeRoleAsync(ReplicaRole.Primary, CancellationToken.None).GetAwaiter().GetResult(); Task.Run( () => { Task.Delay(10000).GetAwaiter().GetResult(); tcs.SetCanceled(); }); tcs.Task.GetAwaiter().GetResult().Should().BeTrue(); // This will throw if RunAsync exception propagates out Console.WriteLine(@"// P -> S"); testServiceReplica.ChangeRoleAsync(ReplicaRole.ActiveSecondary, CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsNotIn(FaultType.Transient)), Times.Never()); partition.Verify(p => p.ReportFault(FaultType.Transient), Times.Once()); partition.Verify(p => p.ReportPartitionHealth(It.Is <HealthInformation>(hinfo => Utility.IsRunAsyncUnhandledExceptionHealthInformation(hinfo))), Times.Once()); }
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(); }
public void RunAsyncFail() { Console.WriteLine("StatelessServiceLifeCycleTests - Test Method: RunAsyncFail()"); var serviceContext = TestMocksRepository.GetMockStatelessServiceContext(); IStatelessServiceInstance testServiceReplica = new StatelessServiceInstanceAdapter(serviceContext, new RunAsyncFailTestService(serviceContext)); var tcs = new TaskCompletionSource <bool>(); var partition = new Mock <IStatelessServicePartition>(); partition.Setup(p => p.ReportFault(It.IsAny <FaultType>())).Callback( () => { tcs.SetResult(true); }); partition.Setup(p => p.ReportPartitionHealth(It.IsAny <HealthInformation>())); testServiceReplica.OpenAsync(partition.Object, CancellationToken.None).GetAwaiter().GetResult(); Task.Run( () => { Task.Delay(10000).GetAwaiter().GetResult(); tcs.SetCanceled(); }); tcs.Task.GetAwaiter().GetResult().Should().BeTrue(); // This will throw if RunAsync exception propagates out testServiceReplica.CloseAsync(CancellationToken.None).GetAwaiter().GetResult(); partition.Verify(p => p.ReportFault(It.IsNotIn(FaultType.Transient)), Times.Never()); partition.Verify(p => p.ReportFault(FaultType.Transient), Times.Once()); partition.Verify(p => p.ReportPartitionHealth(It.Is <HealthInformation>(hinfo => Utility.IsRunAsyncUnhandledExceptionHealthInformation(hinfo))), Times.Once()); }
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()); }