public void Test_ExecuteAsync() { var query = new ViewQuery(). From("docs", "all_docs"). Bucket("default"). BaseUri(_baseUri); var client = new ViewClient(new HttpClient(), new JsonDataMapper(new ClientConfiguration()), new BucketConfig { Name = "beer-sample" }, new ClientConfiguration()); int n = 10000; var options = new ParallelOptions { MaxDegreeOfParallelism = 4 }; Parallel.For(0, n, options, async i => { var result = await client.ExecuteAsync <dynamic>(query); Console.WriteLine("{0} {1} {2}", i, result.Success, result.Message); }); }
/// <summary> /// Sends a request for a View to the server asynchronously. /// </summary> /// <typeparam name="T">The <see cref="Type" /> T of the body for each row result.</typeparam> /// <param name="query">The <see cref="IViewQuery" /> representing the query.</param> /// <returns> /// An <see cref="Task{IViewResult}" /> object representing the asynchronous operation. /// </returns> public Task <IViewResult <T> > SendAsync <T>(IViewQueryable query) { Task <IViewResult <T> > result; try { query.BaseUri(CachedViewBaseUri); result = query.IsStreaming ? _streamingViewClient.ExecuteAsync <T>(query) : ViewClient.ExecuteAsync <T>(query); } catch (Exception e) { var tcs = new TaskCompletionSource <IViewResult <T> >(); tcs.SetResult(new ViewResult <T> { Exception = e, Message = e.Message, Error = e.Message, Success = false, StatusCode = HttpStatusCode.BadRequest, Rows = new List <ViewRow <T> >() }); result = tcs.Task; } return(result); }
public async Task ViewClient_Submits_ViewQuery_Using_Post() { var keys = Enumerable.Range(1, 10).Select(i => $"key-{i}").ToList(); var expectedJson = JsonConvert.SerializeObject(new { keys }, Formatting.None); var handler = FakeHttpMessageHandler.Create(request => { // verify request was a post Assert.Equal(HttpMethod.Post, request.Method); // get the post body and verify content var content = request.Content.ReadAsStringAsync().Result; Assert.Equal(expectedJson, content); // return empty json response return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{ }") }); }); var httpClient = new CouchbaseHttpClient(handler); var serializer = new DefaultSerializer(); var queryClient = new ViewClient(httpClient, serializer, new Mock <ILogger <ViewClient> >().Object, new Mock <IRedactor>().Object); var query = new ViewQuery("bucket-name", "http://localhost"); query.Keys(keys); await queryClient.ExecuteAsync <dynamic, dynamic>(query).ConfigureAwait(false); }
public async Task ExecuteAsync_SerializerOverride_UsesOverride() { var handlerMock = new Mock <HttpMessageHandler>(); handlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new ByteArrayContent(Array.Empty <byte>()) }); var httpClient = new HttpClient(handlerMock.Object) { BaseAddress = new Uri("http://localhost:8091") }; var httpClientFactory = new MockHttpClientFactory(httpClient); var mockServiceUriProvider = new Mock <IServiceUriProvider>(); mockServiceUriProvider .Setup(m => m.GetRandomViewsUri(It.IsAny <string>())) .Returns(new Uri("http://localhost:8092")); var primarySerializer = new Mock <ITypeSerializer> { DefaultValue = DefaultValue.Mock }; var overrideSerializer = new Mock <ITypeSerializer> { DefaultValue = DefaultValue.Mock }; var client = new ViewClient(httpClientFactory, primarySerializer.Object, new Mock <ILogger <ViewClient> >().Object, new Mock <IRedactor>().Object, NoopRequestTracer.Instance); await client.ExecuteAsync <object, object>(new ViewQuery("default", "doc", "view") { Serializer = overrideSerializer.Object }).ConfigureAwait(false); primarySerializer.Verify( m => m.DeserializeAsync <BlockViewResult <object, object> .ViewResultData>(It.IsAny <Stream>(), It.IsAny <CancellationToken>()), Times.Never); overrideSerializer.Verify( m => m.DeserializeAsync <BlockViewResult <object, object> .ViewResultData>(It.IsAny <Stream>(), It.IsAny <CancellationToken>()), Times.AtLeastOnce); }
public async Task ExecuteAsync_Sets_LastActivity() { var handler = FakeHttpMessageHandler.Create(request => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{ }") }); var httpClient = new CouchbaseHttpClient(handler); var serializer = new DefaultSerializer(); var queryClient = new ViewClient(httpClient, serializer, new Mock <ILogger <ViewClient> >().Object, new Mock <IRedactor>().Object); Assert.Null(queryClient.LastActivity); var query = new ViewQuery("bucket-name", "http://localhost"); query.Keys("test-key"); await queryClient.ExecuteAsync <dynamic, dynamic>(query); Assert.NotNull(queryClient.LastActivity); }
public async Task ExecuteAsync_Sets_LastActivity() { var handler = FakeHttpMessageHandler.Create(request => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{ }") }); var httpClient = new HttpClient(handler); var queryClient = new ViewClient(httpClient, new JsonDataMapper(new DefaultSerializer()), new ClusterContext(null, new ClusterOptions())); Assert.Null(queryClient.LastActivity); var query = new ViewQuery("bucket-name", "http://localhost"); query.Keys("test-key"); query.UseStreaming(true); await queryClient.ExecuteAsync(query); Assert.NotNull(queryClient.LastActivity); }
public void Test_ExecuteAsync() { var query = new ViewQuery(). From("docs", "all_docs"). Bucket("default"); var client = new ViewClient(new HttpClient(), new JsonDataMapper(new ClientConfiguration()), new BucketConfig { Name = "beer-sample" }, new ClientConfiguration()); int n = 10000; var options = new ParallelOptions { MaxDegreeOfParallelism = 4}; Parallel.For(0, n, options, async i => { var result = await client.ExecuteAsync<dynamic>(query); Console.WriteLine("{0} {1} {2}", i, result.Success, result.Message); }); }