Example #1
0
        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();
        }
Example #2
0
        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);
        }
Example #3
0
        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());
        }
Example #4
0
        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();
        }
Example #5
0
        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());
        }