public void SucceedWithExpiration() { var apiCall = ApiServerStreamingCall.Create <int, int>( (request, callOptions) => null, CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(TimeSpan.FromSeconds(100)))), new FakeClock()); Assert.Null(apiCall.Call(0, null)); }
public async Task FailWithRetry() { var apiCall = ApiServerStreamingCall.Create <int, int>( (request, callOptions) => null, CallSettings.FromRetry(new RetrySettings(5, TimeSpan.Zero, TimeSpan.Zero, 1.0, e => false, RetrySettings.RandomJitter)), new FakeClock()); await Assert.ThrowsAsync <InvalidOperationException>(() => apiCall.CallAsync(0, null)); Assert.Throws <InvalidOperationException>(() => apiCall.Call(0, null)); }
public async Task SucceedWithExpiration() { var apiCall = ApiServerStreamingCall.Create <int, int>( "Method", (request, callOptions) => null, CallSettings.FromExpiration(Expiration.FromTimeout(TimeSpan.FromSeconds(100))), new FakeClock()); Assert.Null(await apiCall.CallAsync(0, null)); Assert.Null(apiCall.Call(0, null)); }
private static async Task <TResponse> Call <TRequest, TResponse>( bool async, ApiServerStreamingCall <TRequest, TResponse> call, TRequest request, CallSettings callSettings) where TRequest : class, IMessage <TRequest> where TResponse : class, IMessage <TResponse> { var streamingCall = async ? await call.CallAsync(request, callSettings) : call.Call(request, callSettings); // return the first (and only) response message. await streamingCall.ResponseStream.MoveNext(); return(streamingCall.ResponseStream.Current); }
public void FailWithRetry() { var apiCall = ApiServerStreamingCall.Create <int, int>( (request, callOptions) => null, CallSettings.FromCallTiming(CallTiming.FromRetry(new RetrySettings( new BackoffSettings(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(100), 2.0), new BackoffSettings(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(100), 2.0), Expiration.FromTimeout(TimeSpan.FromSeconds(100))))), new FakeClock()); Assert.Throws <InvalidOperationException>(() => apiCall.Call(0, null)); }
public async Task WithLogging_Async() { var logger = new MemoryLogger("category"); var call = new ApiServerStreamingCall <SimpleRequest, SimpleResponse>( "SimpleMethod", (req, cs) => Task.FromResult(default(AsyncServerStreamingCall <SimpleResponse>)), (req, cs) => null, null).WithLogging(logger); await call.CallAsync(new SimpleRequest(), null); var entries = logger.ListLogEntries(); Assert.Equal(2, entries.Count); Assert.All(entries, entry => Assert.Contains("SimpleMethod", entry.Message)); }
public void WithGoogleRequestParam() { CallSettings syncCallSettings = null; CallSettings asyncCallSettings = null; var call0 = new ApiServerStreamingCall <SimpleRequest, SimpleResponse>( (req, cs) => { asyncCallSettings = cs; return(null); }, (req, cs) => { syncCallSettings = cs; return(null); }, null); var call1 = call0.WithGoogleRequestParam("parent", request => request.Name); call1.Call(new SimpleRequest { Name = "test" }, null); call1.CallAsync(new SimpleRequest { Name = "test" }, null); CallSettingsTest.AssertSingleHeader(syncCallSettings, CallSettings.RequestParamsHeader, "parent=test"); CallSettingsTest.AssertSingleHeader(asyncCallSettings, CallSettings.RequestParamsHeader, "parent=test"); }
public void WithCallSettingsOverlay() { var ctBase = new CancellationTokenSource().Token; var ctPerCall = new CancellationTokenSource().Token; var ctOverlay = new CancellationTokenSource().Token; CallSettings syncCallSettings = null; CallSettings asyncCallSettings = null; var call0 = new ApiServerStreamingCall <SimpleRequest, SimpleResponse>( "Method", (req, cs) => { asyncCallSettings = cs; return(null); }, (req, cs) => { syncCallSettings = cs; return(null); }, CallSettings.FromCancellationToken(ctBase)); // Verify a null overlay has no effect. // CallAsync call runs synchonously due to 'call0' definition above. var call1 = call0.WithCallSettingsOverlay(req => null); call1.Call(null, CallSettings.FromCancellationToken(ctPerCall)); call1.CallAsync(null, CallSettings.FromCancellationToken(ctPerCall)); Assert.Equal(ctPerCall, syncCallSettings.CancellationToken.Value); Assert.NotEqual(ctBase, syncCallSettings.CancellationToken.Value); Assert.Equal(ctPerCall, asyncCallSettings.CancellationToken.Value); Assert.NotEqual(ctBase, asyncCallSettings.CancellationToken.Value); // Verify an overlay overwrites all else. var call2 = call0.WithCallSettingsOverlay(req => CallSettings.FromCancellationToken(ctOverlay)); call2.Call(null, CallSettings.FromCancellationToken(ctPerCall)); call2.CallAsync(null, CallSettings.FromCancellationToken(ctPerCall)); Assert.Equal(ctOverlay, syncCallSettings.CancellationToken.Value); Assert.NotEqual(ctBase, syncCallSettings.CancellationToken.Value); Assert.NotEqual(ctPerCall, syncCallSettings.CancellationToken.Value); Assert.Equal(ctOverlay, asyncCallSettings.CancellationToken.Value); Assert.NotEqual(ctBase, asyncCallSettings.CancellationToken.Value); Assert.NotEqual(ctPerCall, asyncCallSettings.CancellationToken.Value); }
public void WithExtractedGoogleRequestParam_MultipleFields(string tableNameValue, string appProfileIdValue, string subName, string expectedHeader) { CallSettings syncCallSettings = null; CallSettings asyncCallSettings = null; var call0 = new ApiServerStreamingCall <ApiCallTest.ExtractedRequestParamRequest, SimpleResponse>( "Method", (req, cs) => { asyncCallSettings = cs; return(null); }, (req, cs) => { syncCallSettings = cs; return(null); }, null); // call corresponding to the following routing parameters: // { field: "table_name", path_template: "{project_id=projects/*}/**" } // { field: "sub_name", path_template: "subs/{sub.sub_name}" } // { field: "app_profile_id", path_template: "{legacy.routing_id=**}" } var call1 = call0 .WithExtractedGoogleRequestParam( new RoutingHeaderExtractor <ApiCallTest.ExtractedRequestParamRequest>() .WithExtractedParameter("project_id", "^(projects/[^/]+)(?:/.*)?$", request => request.TableName) .WithExtractedParameter("sub_name", "^subs/([^/]+)/?$", request => request.Sub.TableName) .WithExtractedParameter("legacy.routing_id", "^(.*)$", request => request.AppProfileId)); var request = new ApiCallTest.ExtractedRequestParamRequest { TableName = tableNameValue, AppProfileId = appProfileIdValue, Sub = new ApiCallTest.ExtractedRequestParamRequest { TableName = subName } }; call1.Call(request, null); call1.CallAsync(request, null); CallSettingsTest.AssertRoutingHeader(syncCallSettings, expectedHeader); CallSettingsTest.AssertRoutingHeader(asyncCallSettings, expectedHeader); }
partial void OnConstruction(Spanner.SpannerClient grpcClient, SpannerSettings effectiveSettings, ClientHelper clientHelper) { _callExecuteSqlStream = clientHelper.BuildApiCall <ExecuteSqlRequest, PartialResultSet>( GrpcClient.ExecuteStreamingSql, null); }