Example #1
0
        public void Can_set_priority()
        {
            var request = new AnalyticsRequest(Statement);

            Assert.Equal(0, request.PriorityValue);

            request.Priority(true);
            Assert.Equal(-1, request.PriorityValue);

            request.Priority(false);
            Assert.Equal(0, request.PriorityValue);

            request.Priority(5);
            Assert.Equal(5, request.PriorityValue);
        }
Example #2
0
        public async Task <IAnalyticsResult <T> > AnalyticsQueryAsync <T>(string statement, AnalyticsOptions?options = default)
        {
            options ??= new AnalyticsOptions();
            options.TimeoutValue ??= _context.ClusterOptions.AnalyticsTimeout;

            ThrowIfNotBootstrapped();

            var query = new AnalyticsRequest(statement)
            {
                ClientContextId     = options.ClientContextIdValue,
                NamedParameters     = options.NamedParameters,
                PositionalArguments = options.PositionalParameters,
                Timeout             = options.TimeoutValue.Value
            };

            query.Priority(options.PriorityValue);
            query.ScanConsistency(options.ScanConsistencyValue);

            async Task <IAnalyticsResult <T> > Func()
            {
                var client1  = LazyAnalyticsClient.Value;
                var query1   = query;
                var options1 = options ?? new AnalyticsOptions();

                return(await client1.QueryAsync <T>(query1, options1.Token).ConfigureAwait(false));
            }

            return(await _retryOrchestrator.RetryAsync(Func, query).ConfigureAwait(false));
        }
Example #3
0
        public void Client_sets_AnalyticsPriority_Header(bool priority)
        {
            var context = ContextFactory.GetCouchbaseContext();

            context.AnalyticsUris.Add(new FailureCountingUri("http://localhost"));

            var httpClient = new HttpClient(
                FakeHttpMessageHandler.Create(request =>
            {
                if (priority)
                {
                    Assert.IsTrue(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out var values));
                    Assert.AreEqual("-1", values.First());
                }
                else
                {
                    Assert.IsFalse(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out _));
                }

                return(new HttpResponseMessage(HttpStatusCode.OK));
            })
                );

            var client = new AnalyticsClient(httpClient, new JsonDataMapper(context.ClientConfig), context);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");

            queryRequest.Priority(priority);

            client.Query <dynamic>(queryRequest);
        }
Example #4
0
        public Task <IAnalyticsResult <T> > AnalyticsQueryAsync <T>(string statement, AnalyticsOptions options = default)
        {
            if (options == default)
            {
                options = new AnalyticsOptions();
            }

            var query = new AnalyticsRequest(statement);

            query.ClientContextId(options.ClientContextId);
            query.Pretty(options.Pretty);
            query.IncludeMetrics(options.IncludeMetrics);
            query.NamedParameters     = options.NamedParameters;
            query.PositionalArguments = options.PositionalParameters;

            if (options.Timeout.HasValue)
            {
                query.Timeout(options.Timeout.Value);
            }

            query.Priority(options.Priority);
            query.Deferred(options.Deferred);

            query.ConfigureLifespan(30); //TODO: use configuration.AnalyticsTimeout

            if (_analyticsClient == null)
            {
                _analyticsClient = new AnalyticsClient(_configuration);
            }
            return(_analyticsClient.QueryAsync <T>(query, options.CancellationToken));
        }
        public async Task <IAnalyticsResult <T> > AnalyticsQueryAsync <T>(string statement, AnalyticsOptions options = default)
        {
            await EnsureBootstrapped();

            if (options == default)
            {
                options = new AnalyticsOptions();
            }

            var query = new AnalyticsRequest(statement);

            query.ClientContextId(options.ClientContextId);
            query.Pretty(options.Pretty);
            query.IncludeMetrics(options.IncludeMetrics);
            query.NamedParameters     = options.NamedParameters;
            query.PositionalArguments = options.PositionalParameters;

            if (options.Timeout.HasValue)
            {
                query.Timeout(options.Timeout.Value);
            }

            query.Priority(options.Priority);
            query.Deferred(options.Deferred);

            query.ConfigureLifespan(30); //TODO: use clusterOptions.AnalyticsTimeout

            return(await _lazyAnalyticsClient.Value.QueryAsync <T>(query, options.CancellationToken));
        }
Example #6
0
        public void Client_sets_AnalyticsPriority_Header(bool priority)
        {
            var configuration = new Configuration().WithServers("http://localhost");

            var httpClient = new HttpClient(
                FakeHttpMessageHandler.Create(request =>
            {
                if (priority)
                {
                    Assert.True(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out var values));
                    Assert.Equal("-1", values.First());
                }
                else
                {
                    Assert.False(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out _));
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("{}")
                });
            })
                );

            var client = new AnalyticsClient(httpClient, new JsonDataMapper(new DefaultSerializer()), configuration);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");

            queryRequest.Priority(priority);

            client.Query <dynamic>(queryRequest);
        }
Example #7
0
        public void Client_sets_AnalyticsPriority_Header(bool priority)
        {
            var options = new ClusterOptions().WithServers("http://localhost");
            var context = new ClusterContext(null, options);

            context.AddNode(new ClusterNode(context)
            {
                AnalyticsUri = new Uri("http://localhost:8094/query"),
                EndPoint     = new IPEndPoint(IPAddress.Loopback, 8091),
                NodesAdapter = new NodeAdapter {
                    Analytics = 8094
                }
            });

            var httpClient = new HttpClient(
                FakeHttpMessageHandler.Create(request =>
            {
                if (priority)
                {
                    Assert.True(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName,
                                                             out var values));
                    Assert.Equal("-1", values.First());
                }
                else
                {
                    Assert.False(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out _));
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("{}")
                });
            })
                );

            var client = new AnalyticsClient(httpClient, new JsonDataMapper(new DefaultSerializer()), context);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");

            queryRequest.Priority(priority);

            client.Query <dynamic>(queryRequest);
        }
        public async Task Test_Analytics(string file, HttpStatusCode httpStatusCode, Type errorType, bool readOnly)
        {
            var retryOrchestrator = CreateRetryOrchestrator();

            using (var response = ResourceHelper.ReadResourceAsStream(@"Documents\Analytics\" + file))
            {
                var buffer = new byte[response.Length];
                response.Read(buffer, 0, buffer.Length);

                var responses = GetResponses(20, buffer, httpStatusCode);
                var client    = MockedHttpClients.AnalyticsClient(responses);

                using var cts = new CancellationTokenSource();
                cts.CancelAfter(1000);

                var options = new AnalyticsOptions().
                              Timeout(TimeSpan.FromSeconds(1000)).CancellationToken(cts.Token).Readonly(readOnly);

                var query = new AnalyticsRequest("SELECT * FROM `bar`;")
                {
                    ClientContextId     = options.ClientContextIdValue,
                    NamedParameters     = options.NamedParameters,
                    PositionalArguments = options.PositionalParameters,
                    ReadOnly            = options.ReadonlyValue,
                    Idempotent          = options.ReadonlyValue
                };
                // ReSharper disable once PossibleInvalidOperationException
                query.WithTimeout(options.TimeoutValue.Value);
                query.Priority(options.PriorityValue);
                query.Token = options.Token;

                async Task <IAnalyticsResult <dynamic> > Send()
                {
                    var client1 = client;

                    return(await client1.QueryAsync <dynamic>(query, options.Token));
                }

                await AssertThrowsIfExpectedAsync(errorType, () => retryOrchestrator.RetryAsync(Send, query));
            }
        }
        public async Task Client_sets_AnalyticsPriority_Header(bool priority)
        {
            var httpClient = new CouchbaseHttpClient(
                FakeHttpMessageHandler.Create(request =>
            {
                if (priority)
                {
                    Assert.True(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName,
                                                             out var values));
                    Assert.Equal("-1", values.First());
                }
                else
                {
                    Assert.False(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out _));
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("{}")
                });
            })
                );

            var mockServiceUriProvider = new Mock <IServiceUriProvider>();

            mockServiceUriProvider
            .Setup(m => m.GetRandomAnalyticsUri())
            .Returns(new Uri("http://localhost:8096"));

            var serializer = new DefaultSerializer();
            var client     = new AnalyticsClient(httpClient, mockServiceUriProvider.Object, serializer,
                                                 new Mock <ILogger <AnalyticsClient> >().Object, NoopRequestTracer.Instance);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");

            queryRequest.Priority(priority);

            await client.QueryAsync <dynamic>(queryRequest);
        }