Example #1
0
        public void CreateClient_MatchingConfigurationBasedOnCustomName_ReturnConfiguration()
        {
            // Arrange
            var address = new Uri("http://localhost");

            var services = new ServiceCollection();

            services.AddOptions();
            services
            .AddGrpcClient <TestGreeterClient>("Custom", o => o.Address = address)
            .AddHttpMessageHandler(() => ClientTestHelpers.CreateTestMessageHandler(new HelloReply()));

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>());

            // Act
            var client = clientFactory.CreateClient <TestGreeterClient>("Custom");

            // Assert
            Assert.IsNotNull(client);
            Assert.AreEqual(address, client.CallInvoker.Channel.Address);
        }
Example #2
0
        public void CreateClient_ServerCallContextHasValues_PropogatedDeadlineAndCancellation()
        {
            // Arrange
            var baseAddress       = new Uri("http://localhost");
            var deadline          = new DateTime(2000, 12, 12, 1, 1, 1, DateTimeKind.Utc);
            var cancellationToken = new CancellationTokenSource().Token;

            var services = new ServiceCollection();

            services.AddOptions();
            services.AddSingleton(CreateHttpContextAccessorWithServerCallContext(deadline, cancellationToken));
            services.AddGrpcClient <TestGreeterClient>(o =>
            {
                o.BaseAddress = baseAddress;
            }).EnableCallContextPropagation();

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientFactoryOptions> >());

            // Act
            var client = clientFactory.CreateClient <TestGreeterClient>(nameof(TestGreeterClient));

            // Assert
            Assert.IsNotNull(client);
            Assert.AreEqual(baseAddress, client.CallInvoker.BaseAddress);
            Assert.AreEqual(deadline, client.CallInvoker.Deadline);
            Assert.AreEqual(cancellationToken, client.CallInvoker.CancellationToken);
        }
Example #3
0
        public void CreateClient_NoServerCallContextOnHttpContext_ThrowError()
        {
            // Arrange
            var baseAddress = new Uri("http://localhost");

            var services = new ServiceCollection();

            services.AddOptions();
            services.AddSingleton(CreateHttpContextAccessor(new DefaultHttpContext()));
            services.AddGrpcClient <TestGreeterClient>(o =>
            {
                o.BaseAddress = baseAddress;
            }).EnableCallContextPropagation();

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientFactoryOptions> >());

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => clientFactory.CreateClient <TestGreeterClient>(nameof(TestGreeterClient)));

            // Assert
            Assert.AreEqual("Unable to propagate server context values to the client. Can't find the current gRPC ServerCallContext.", ex.Message);
        }
Example #4
0
        public void CreateClient_MatchingConfigurationBasedOnTypeName_ReturnConfiguration()
        {
            // Arrange
            var baseAddress = new Uri("http://localhost");

            var services = new ServiceCollection();

            services.AddOptions();
            services
            .AddGrpcClient <TestGreeterClient>(o => o.BaseAddress = baseAddress)
            .AddHttpMessageHandler(() => ClientTestHelpers.CreateTestMessageHandler(new HelloReply()));

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientFactoryOptions> >());

            // Act
            var client = clientFactory.CreateClient <TestGreeterClient>(nameof(TestGreeterClient));

            // Assert
            Assert.IsNotNull(client);
            Assert.AreEqual(baseAddress, client.CallInvoker.BaseAddress);
        }
        public async Task CreateClient_NoServerCallContextOnHttpContext_ThrowError()
        {
            // Arrange
            var baseAddress = new Uri("http://localhost");

            var services = new ServiceCollection();

            services.AddOptions();
            services.AddSingleton(CreateHttpContextAccessor(new DefaultHttpContext()));
            services
            .AddGrpcClient <Greeter.GreeterClient>(o =>
            {
                o.BaseAddress = baseAddress;
            })
            .EnableCallContextPropagation()
            .AddHttpMessageHandler(() => ClientTestHelpers.CreateTestMessageHandler(new HelloReply()));

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientFactoryOptions> >());
            var client = clientFactory.CreateClient <Greeter.GreeterClient>(nameof(Greeter.GreeterClient));

            // Act
            var ex = await ExceptionAssert.ThrowsAsync <InvalidOperationException>(() => client.SayHelloAsync(new HelloRequest(), new CallOptions()).ResponseAsync).DefaultTimeout();

            // Assert
            Assert.AreEqual("Unable to propagate server context values to the call. Can't find the current gRPC ServerCallContext.", ex.Message);
        }
Example #6
0
        public void CreateClient_MatchingConfigurationBasedOnCustomName_ReturnConfiguration()
        {
            // Arrange
            var baseAddress = new Uri("http://localhost");

            var services = new ServiceCollection();

            services.AddOptions();
            services.AddSingleton(CreateHttpContextAccessor());
            services.AddGrpcClient <TestGreeterClient>("Custom", o => o.BaseAddress = baseAddress);

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientOptions> >());

            // Act
            var client = clientFactory.CreateClient <TestGreeterClient>("Custom");

            // Assert
            Assert.IsNotNull(client);
            Assert.AreEqual(baseAddress, client.CallInvoker.BaseAddress);
        }
Example #7
0
        public void CreateClient_Default_InternalHttpClientHasInfiniteTimeout()
        {
            // Arrange
            var services = new ServiceCollection();

            services
            .AddGrpcClient <TestGreeterClient>(o => o.Address = new Uri("http://localhost"));

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>());

            // Act
            var client = clientFactory.CreateClient <TestGreeterClient>(nameof(TestGreeterClient));

            // Assert
            Assert.AreEqual(Timeout.InfiniteTimeSpan, client.CallInvoker.Channel.HttpClient.Timeout);
        }
Example #8
0
        public void CreateClient_NoAddress_ThrowError()
        {
            // Arrange
            var services = new ServiceCollection();

            services
            .AddGrpcClient <Greeter.GreeterClient>();

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>());

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => clientFactory.CreateClient <Greeter.GreeterClient>(nameof(Greeter.GreeterClient)));

            // Assert
            Assert.AreEqual("Could not resolve the address for gRPC client 'GreeterClient'.", ex.Message);
        }
Example #9
0
        public void CreateClient_NoMatchingConfiguration_ThrowError()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddOptions();
            services.AddGrpcClient <TestGreeterClient>(o => { });

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientFactoryOptions> >());

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => clientFactory.CreateClient <Greet.Greeter.GreeterClient>("Test"));

            // Assert
            Assert.AreEqual("No gRPC client configured with name 'Test'.", ex.Message);
        }
Example #10
0
        public void CreateClient_AddressSpecifiedOnHttpClientFactory_UseHttpClientFactoryAddress()
        {
            // Arrange
            var services = new ServiceCollection();

            services
            .AddGrpcClient <TestGreeterClient>()
            .ConfigureHttpClient(options => options.BaseAddress = new Uri("http://contoso"));

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>());

            // Act
            var client = clientFactory.CreateClient <TestGreeterClient>(nameof(TestGreeterClient));

            // Assert
            Assert.AreEqual("http://contoso", client.CallInvoker.Channel.Address.OriginalString);
        }
Example #11
0
        public void CreateClient_NoMatchingConfiguration_ThrowError()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddOptions();
            services
            .AddGrpcClient <TestGreeterClient>()
            .AddHttpMessageHandler(() => ClientTestHelpers.CreateTestMessageHandler(new HelloReply()));

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>());

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => clientFactory.CreateClient <Greeter.GreeterClient>("Test"));

            // Assert
            Assert.AreEqual("No gRPC client configured with name 'Test'.", ex.Message);
        }
        public async Task CreateClient_ServerCallContextHasValues_PropogatedDeadlineAndCancellation()
        {
            // Arrange
            var baseAddress       = new Uri("http://localhost");
            var deadline          = DateTime.UtcNow.AddDays(1);
            var cancellationToken = new CancellationTokenSource().Token;

            CallOptions options = default;

            var services = new ServiceCollection();

            services.AddOptions();
            services.AddSingleton(CreateHttpContextAccessorWithServerCallContext(deadline, cancellationToken));
            services
            .AddGrpcClient <Greeter.GreeterClient>(o =>
            {
                o.BaseAddress = baseAddress;
            })
            .EnableCallContextPropagation()
            .AddInterceptor(() => new CallbackInterceptor(o => options = o))
            .AddHttpMessageHandler(() => ClientTestHelpers.CreateTestMessageHandler(new HelloReply()));

            var serviceProvider = services.BuildServiceProvider();

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>(),
                serviceProvider.GetRequiredService <IOptionsMonitor <GrpcClientFactoryOptions> >());
            var client = clientFactory.CreateClient <Greeter.GreeterClient>(nameof(Greeter.GreeterClient));

            // Act
            await client.SayHelloAsync(new HelloRequest()).ResponseAsync.DefaultTimeout();

            // Assert
            Assert.AreEqual(deadline, options.Deadline);
            Assert.AreEqual(cancellationToken, options.CancellationToken);
        }
        public async Task CreateClient_NoServerCallContextOnHttpContextIgnoreError_Success()
        {
            // Arrange
            var testSink     = new TestSink();
            var testProvider = new TestLoggerProvider(testSink);

            var baseAddress = new Uri("http://localhost");

            var services = new ServiceCollection();

            services.AddLogging(o => o.AddProvider(testProvider).SetMinimumLevel(LogLevel.Debug));
            services.AddOptions();
            services.AddSingleton(CreateHttpContextAccessor(new DefaultHttpContext()));
            services
            .AddGrpcClient <Greeter.GreeterClient>(o =>
            {
                o.Address = baseAddress;
            })
            .EnableCallContextPropagation(o => o.SuppressContextNotFoundErrors = true)
            .AddHttpMessageHandler(() => ClientTestHelpers.CreateTestMessageHandler(new HelloReply()));

            var serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var clientFactory = new DefaultGrpcClientFactory(
                serviceProvider,
                serviceProvider.GetRequiredService <IHttpClientFactory>());
            var client = clientFactory.CreateClient <Greeter.GreeterClient>(nameof(Greeter.GreeterClient));

            // Act
            await client.SayHelloAsync(new HelloRequest(), new CallOptions()).ResponseAsync.DefaultTimeout();

            // Assert
            var log = testSink.Writes.Single(w => w.EventId.Name == "PropagateServerCallContextFailure");

            Assert.AreEqual("Unable to propagate server context values to the call. Can't find the gRPC ServerCallContext on the current HttpContext.", log.Message);
        }