Beispiel #1
0
        public static async Task RunPingPongAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running ping_pong");

            using (var call = client.FullDuplexCall())
            {
                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                .SetResponseType(PayloadType.COMPRESSABLE)
                .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
                .SetPayload(CreateZerosPayload(27182)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                            .SetResponseType(PayloadType.COMPRESSABLE)
                            .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
                            .SetPayload(CreateZerosPayload(8)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                            .SetResponseType(PayloadType.COMPRESSABLE)
                            .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
                            .SetPayload(CreateZerosPayload(1828)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                            .SetResponseType(PayloadType.COMPRESSABLE)
                            .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
                            .SetPayload(CreateZerosPayload(45904)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length);

                await call.RequestStream.CompleteAsync();

                Assert.IsFalse(await call.ResponseStream.MoveNext());
            }
            Console.WriteLine("Passed!");
        }
Beispiel #2
0
        public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running empty_stream");
            using (var call = client.FullDuplexCall())
            {
                await call.RequestStream.CompleteAsync();

                var responseList = await call.ResponseStream.ToListAsync();
                Assert.AreEqual(0, responseList.Count);
            }
            Console.WriteLine("Passed!");
        }
Beispiel #3
0
        public static async Task RunClientStreamingAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running client_streaming");

            var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build());

            using (var call = client.StreamingInputCall())
            {
                await call.RequestStream.WriteAllAsync(bodySizes);

                var response = await call.ResponseAsync;
                Assert.AreEqual(74922, response.AggregatedPayloadSize);
            }
            Console.WriteLine("Passed!");
        }
Beispiel #4
0
        public static async Task RunServerStreamingAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running server_streaming");

            var bodySizes = new List<int> { 31415, 9, 2653, 58979 };

            var request = StreamingOutputCallRequest.CreateBuilder()
            .SetResponseType(PayloadType.COMPRESSABLE)
            .AddRangeResponseParameters(bodySizes.ConvertAll(
                (size) => ResponseParameters.CreateBuilder().SetSize(size).Build()))
            .Build();

            using (var call = client.StreamingOutputCall(request))
            {
                var responseList = await call.ResponseStream.ToListAsync();
                foreach (var res in responseList)
                {
                    Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
                }
                CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
            }
            Console.WriteLine("Passed!");
        }
Beispiel #5
0
        public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running cancel_after_first_response");

            var cts = new CancellationTokenSource();
            using (var call = client.FullDuplexCall(cancellationToken: cts.Token))
            {
                await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                    .SetResponseType(PayloadType.COMPRESSABLE)
                    .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
                    .SetPayload(CreateZerosPayload(27182)).Build());

                Assert.IsTrue(await call.ResponseStream.MoveNext());
                Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type);
                Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length);

                cts.Cancel();

                var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext());
                Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
            }
            Console.WriteLine("Passed!");
        }
Beispiel #6
0
        public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client)
        {
            Console.WriteLine("running oauth2_auth_token");
            ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope });
            string oauth2Token = await credential.GetAccessTokenForRequestAsync();

            client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token);

            var request = SimpleRequest.CreateBuilder()
                .SetFillUsername(true)
                .SetFillOauthScope(true)
                .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #7
0
        public static void RunPerRpcCreds(TestService.TestServiceClient client)
        {
            Console.WriteLine("running per_rpc_creds");

            var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
            Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
            string oauth2Token = credential.Token.AccessToken;

            var request = SimpleRequest.CreateBuilder()
                .SetFillUsername(true)
                .SetFillOauthScope(true)
                .Build();

            var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) });

            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #8
0
        public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client)
        {
            Console.WriteLine("running per_rpc_creds");

            ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope });
            string oauth2Token = await credential.GetAccessTokenForRequestAsync();
            var headerInterceptor = AuthInterceptors.FromAccessToken(oauth2Token);

            var request = SimpleRequest.CreateBuilder()
                .SetFillUsername(true)
                .SetFillOauthScope(true)
                .Build();

            var headers = new Metadata();
            headerInterceptor(null, "", headers);
            var response = client.UnaryCall(request, headers: headers);

            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #9
0
        public static void RunComputeEngineCreds(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running compute_engine_creds");
            var request = SimpleRequest.CreateBuilder()
                .SetResponseType(PayloadType.COMPRESSABLE)
                    .SetResponseSize(314159)
                    .SetPayload(CreateZerosPayload(271828))
                    .SetFillUsername(true)
                    .SetFillOauthScope(true)
                    .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
            Assert.AreEqual(314159, response.Payload.Body.Length);
            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ComputeEngineUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #10
0
        public static void RunOAuth2AuthToken(TestService.TestServiceClient client)
        {
            Console.WriteLine("running oauth2_auth_token");
            var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
            Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
            string oauth2Token = credential.Token.AccessToken;

            // Intercept calls with an OAuth2 token obtained out-of-band.
            client.HeaderInterceptor = new MetadataInterceptorDelegate((metadata) =>
            {
                metadata.Add(new Metadata.Entry("Authorization", "Bearer " + oauth2Token));
            });

            var request = SimpleRequest.CreateBuilder()
                .SetFillUsername(true)
                .SetFillOauthScope(true)
                .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #11
0
        public static void RunCancelAfterBegin(TestService.ITestServiceClient client)
        {
            Task.Run(async () =>
            {
                Console.WriteLine("running cancel_after_begin");

                var cts = new CancellationTokenSource();
                using (var call = client.StreamingInputCall(cancellationToken: cts.Token))
                {
                    // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
                    await Task.Delay(1000);
                    cts.Cancel();

                    try
                    {
                        var response = await call.ResponseAsync;
                        Assert.Fail();
                    }
                    catch (RpcException e)
                    {
                        Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
                    }
                }
                Console.WriteLine("Passed!");
            }).Wait();
        }
        public static async Task RunTimeoutOnSleepingServerAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running timeout_on_sleeping_server");

            var deadline = DateTime.UtcNow.AddMilliseconds(1);
            using (var call = client.FullDuplexCall(deadline: deadline))
            {
                try
                {
                    await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder()
                        .SetPayload(CreateZerosPayload(27182)).Build());
                }
                catch (InvalidOperationException)
                {
                    // Deadline was reached before write has started. Eat the exception and continue.
                }

                var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext());
                Assert.AreEqual(StatusCode.DeadlineExceeded, ex.Status.StatusCode);
            }
            Console.WriteLine("Passed!");
        }
Beispiel #13
0
        public static void RunOAuth2AuthToken(TestService.TestServiceClient client)
        {
            Console.WriteLine("running oauth2_auth_token");
            var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
            Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
            string oauth2Token = credential.Token.AccessToken;

            client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token);

            var request = SimpleRequest.CreateBuilder()
                .SetFillUsername(true)
                .SetFillOauthScope(true)
                .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #14
0
        public static async Task RunComputeEngineCredsAsync(TestService.TestServiceClient client)
        {
            Console.WriteLine("running compute_engine_creds");
            var credential = await GoogleCredential.GetApplicationDefaultAsync();
            Assert.IsFalse(credential.IsCreateScopedRequired);
            client.HeaderInterceptor = AuthInterceptors.FromCredential(credential);
            
            var request = SimpleRequest.CreateBuilder()
                .SetResponseType(PayloadType.COMPRESSABLE)
                    .SetResponseSize(314159)
                    .SetPayload(CreateZerosPayload(271828))
                    .SetFillUsername(true)
                    .SetFillOauthScope(true)
                    .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
            Assert.AreEqual(314159, response.Payload.Body.Length);
            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ComputeEngineUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #15
0
 private async Task RunTestCaseAsync(string testCase, TestService.TestServiceClient client)
 {
     switch (testCase)
     {
         case "empty_unary":
             RunEmptyUnary(client);
             break;
         case "large_unary":
             RunLargeUnary(client);
             break;
         case "client_streaming":
             await RunClientStreamingAsync(client);
             break;
         case "server_streaming":
             await RunServerStreamingAsync(client);
             break;
         case "ping_pong":
             await RunPingPongAsync(client);
             break;
         case "empty_stream":
             await RunEmptyStreamAsync(client);
             break;
         case "service_account_creds":
             await RunServiceAccountCredsAsync(client);
             break;
         case "compute_engine_creds":
             await RunComputeEngineCredsAsync(client);
             break;
         case "jwt_token_creds":
             await RunJwtTokenCredsAsync(client);
             break;
         case "oauth2_auth_token":
             await RunOAuth2AuthTokenAsync(client);
             break;
         case "per_rpc_creds":
             await RunPerRpcCredsAsync(client);
             break;
         case "cancel_after_begin":
             await RunCancelAfterBeginAsync(client);
             break;
         case "cancel_after_first_response":
             await RunCancelAfterFirstResponseAsync(client);
             break;
         case "benchmark_empty_unary":
             RunBenchmarkEmptyUnary(client);
             break;
         default:
             throw new ArgumentException("Unknown test case " + testCase);
     }
 }
Beispiel #16
0
        public static async Task RunJwtTokenCredsAsync(TestService.TestServiceClient client)
        {
            Console.WriteLine("running jwt_token_creds");
            var credential = await GoogleCredential.GetApplicationDefaultAsync();
            // check this a credential with scope support, but don't add the scope.
            Assert.IsTrue(credential.IsCreateScopedRequired);
            client.HeaderInterceptor = AuthInterceptors.FromCredential(credential);

            var request = SimpleRequest.CreateBuilder()
                .SetResponseType(PayloadType.COMPRESSABLE)
                    .SetResponseSize(314159)
                    .SetPayload(CreateZerosPayload(271828))
                    .SetFillUsername(true)
                    .SetFillOauthScope(true)
                    .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
            Assert.AreEqual(314159, response.Payload.Body.Length);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
Beispiel #17
0
 public static void RunEmptyUnary(TestService.ITestServiceClient client)
 {
     Console.WriteLine("running empty_unary");
     var response = client.EmptyCall(Empty.DefaultInstance);
     Assert.IsNotNull(response);
     Console.WriteLine("Passed!");
 }
Beispiel #18
0
        public static async Task RunCancelAfterBeginAsync(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running cancel_after_begin");

            var cts = new CancellationTokenSource();
            using (var call = client.StreamingInputCall(cancellationToken: cts.Token))
            {
                // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
                await Task.Delay(1000);
                cts.Cancel();

                var ex = Assert.Throws<RpcException>(async () => await call.ResponseAsync);
                Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode);
            }
            Console.WriteLine("Passed!");
        }
Beispiel #19
0
        public static void RunLargeUnary(TestService.ITestServiceClient client)
        {
            Console.WriteLine("running large_unary");
            var request = SimpleRequest.CreateBuilder()
                    .SetResponseType(PayloadType.COMPRESSABLE)
                    .SetResponseSize(314159)
                    .SetPayload(CreateZerosPayload(271828))
                    .Build();

            var response = client.UnaryCall(request);

            Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
            Assert.AreEqual(314159, response.Payload.Body.Length);
            Console.WriteLine("Passed!");
        }
Beispiel #20
0
 // This is not an official interop test, but it's useful.
 public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client)
 {
     BenchmarkUtil.RunBenchmark(10000, 10000,
                                () => { client.EmptyCall(Empty.DefaultInstance); });
 }
Beispiel #21
0
 private void RunTestCase(string testCase, TestService.ITestServiceClient client)
 {
     switch (testCase)
     {
         case "empty_unary":
             RunEmptyUnary(client);
             break;
         case "large_unary":
             RunLargeUnary(client);
             break;
         case "client_streaming":
             RunClientStreaming(client);
             break;
         case "server_streaming":
             RunServerStreaming(client);
             break;
         case "ping_pong":
             RunPingPong(client);
             break;
         case "empty_stream":
             RunEmptyStream(client);
             break;
         case "service_account_creds":
             RunServiceAccountCreds(client);
             break;
         case "compute_engine_creds":
             RunComputeEngineCreds(client);
             break;
         case "cancel_after_begin":
             RunCancelAfterBegin(client);
             break;
         case "cancel_after_first_response":
             RunCancelAfterFirstResponse(client);
             break;
         case "benchmark_empty_unary":
             RunBenchmarkEmptyUnary(client);
             break;
         default:
             throw new ArgumentException("Unknown test case " + testCase);
     }
 }