コード例 #1
0
        public async Task Invoke_ThrowExceptionAwaitedRelease_ReleaseCalledAndErrorThrown()
        {
            // Arrange
            var releaseTcs       = new TaskCompletionSource <object?>(TaskCreationOptions.RunContinuationsAsynchronously);
            var serviceActivator = new TcsGrpcServiceActivator <TestService>(releaseTcs);
            var thrownException  = new Exception("Exception!");
            var invoker          = new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                (service, reader, context) => throw thrownException,
                new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller),
                HttpContextServerCallContextHelper.CreateMethodOptions(),
                serviceActivator);
            var httpContext = HttpContextHelpers.CreateContext();

            // Act
            var task = invoker.Invoke(httpContext, HttpContextServerCallContextHelper.CreateServerCallContext(), new TestMessage());

            Assert.False(task.IsCompleted);

            releaseTcs.SetResult(null);

            try
            {
                await task;
                Assert.Fail();
            }
            catch (Exception ex)
            {
                // Assert
                Assert.True(serviceActivator.Released);
                Assert.AreEqual(thrownException, ex);
            }
        }
コード例 #2
0
        public async Task Invoke_AwaitedSuccess_ReleaseCalled()
        {
            // Arrange
            var methodTcs        = new TaskCompletionSource <TestMessage>(TaskCreationOptions.RunContinuationsAsynchronously);
            var methodResult     = new TestMessage();
            var serviceActivator = new TestGrpcServiceActivator <TestService>();
            var invoker          = new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                (service, reader, context) => methodTcs.Task,
                new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller),
                HttpContextServerCallContextHelper.CreateMethodOptions(),
                serviceActivator);
            var httpContext = HttpContextHelpers.CreateContext();

            // Act
            var task = invoker.Invoke(httpContext, HttpContextServerCallContextHelper.CreateServerCallContext(), new TestMessage());

            Assert.False(task.IsCompleted);

            methodTcs.SetResult(methodResult);
            var awaitedResult = await task;

            // Assert
            Assert.AreEqual(methodResult, awaitedResult);
            Assert.True(serviceActivator.Released);
        }
コード例 #3
0
        private static ServerCallHandlerBase <TestService, TestMessage, TestMessage> CreateHandler(MethodType methodType, ILoggerFactory?loggerFactory = null)
        {
            var method = new Method <TestMessage, TestMessage>(methodType, "test", "test", _marshaller, _marshaller);

            switch (methodType)
            {
            case MethodType.Unary:
                return(new UnaryServerCallHandler <TestService, TestMessage, TestMessage>(
                           new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                               (service, reader, context) => Task.FromResult(new TestMessage()),
                               method,
                               HttpContextServerCallContextHelper.CreateMethodOptions(),
                               new TestGrpcServiceActivator <TestService>()),
                           loggerFactory ?? NullLoggerFactory.Instance));

            case MethodType.ClientStreaming:
                return(new ClientStreamingServerCallHandler <TestService, TestMessage, TestMessage>(
                           new ClientStreamingServerMethodInvoker <TestService, TestMessage, TestMessage>(
                               (service, reader, context) => Task.FromResult(new TestMessage()),
                               method,
                               HttpContextServerCallContextHelper.CreateMethodOptions(),
                               new TestGrpcServiceActivator <TestService>()),
                           loggerFactory ?? NullLoggerFactory.Instance));

            case MethodType.ServerStreaming:
                return(new ServerStreamingServerCallHandler <TestService, TestMessage, TestMessage>(
                           new ServerStreamingServerMethodInvoker <TestService, TestMessage, TestMessage>(
                               (service, request, writer, context) => Task.FromResult(new TestMessage()),
                               method,
                               HttpContextServerCallContextHelper.CreateMethodOptions(),
                               new TestGrpcServiceActivator <TestService>()),
                           loggerFactory ?? NullLoggerFactory.Instance));

            case MethodType.DuplexStreaming:
                return(new DuplexStreamingServerCallHandler <TestService, TestMessage, TestMessage>(
                           new DuplexStreamingServerMethodInvoker <TestService, TestMessage, TestMessage>(
                               (service, reader, writer, context) => Task.CompletedTask,
                               method,
                               HttpContextServerCallContextHelper.CreateMethodOptions(),
                               new TestGrpcServiceActivator <TestService>()),
                           loggerFactory ?? NullLoggerFactory.Instance));

            default:
                throw new ArgumentException();
            }
        }
コード例 #4
0
        public void Invoke_ThrowException_ReleaseCalledAndErrorThrown()
        {
            // Arrange
            var serviceActivator = new TestGrpcServiceActivator <TestService>();
            var ex      = new Exception("Exception!");
            var invoker = new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                (service, reader, context) => throw ex,
                new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller),
                HttpContextServerCallContextHelper.CreateMethodOptions(),
                serviceActivator);
            var httpContext = HttpContextHelpers.CreateContext();

            // Act
            var task = invoker.Invoke(httpContext, HttpContextServerCallContextHelper.CreateServerCallContext(), new TestMessage());

            // Assert
            Assert.True(serviceActivator.Released);
            Assert.True(task.IsFaulted);
            Assert.AreEqual(ex, task.Exception !.InnerException);
        }
コード例 #5
0
        public async Task Invoke_SuccessAwaitedRelease_ReleaseCalled()
        {
            // Arrange
            var releaseTcs       = new TaskCompletionSource <object?>(TaskCreationOptions.RunContinuationsAsynchronously);
            var serviceActivator = new TcsGrpcServiceActivator <TestService>(releaseTcs);
            var invoker          = new UnaryServerMethodInvoker <TestService, TestMessage, TestMessage>(
                (service, reader, context) => Task.FromResult(new TestMessage()),
                new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller),
                HttpContextServerCallContextHelper.CreateMethodOptions(),
                serviceActivator);
            var httpContext = HttpContextHelpers.CreateContext();

            // Act
            var task = invoker.Invoke(httpContext, HttpContextServerCallContextHelper.CreateServerCallContext(), new TestMessage());

            Assert.False(task.IsCompleted);

            releaseTcs.SetResult(null);
            await task;

            // Assert
            Assert.True(serviceActivator.Released);
        }