Search in sources :

Example 1 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class ClusterLevelQueryIntegrationTest method performsClusterLevelQueryWithoutOpenBucket.

@Test
void performsClusterLevelQueryWithoutOpenBucket() throws Exception {
    String query = "{\"statement\": \"select 1=1\"}";
    QueryRequest request = new QueryRequest(env.timeoutConfig().queryTimeout(), core.context(), env.retryStrategy(), core.context().authenticator(), "select 1=1", query.getBytes(StandardCharsets.UTF_8), true, null, null, null, null, null);
    core.send(request);
    QueryResponse response = request.response().get();
    assertNotNull(response.header().requestId());
    List<QueryChunkRow> rows = response.rows().collectList().block();
    assertNotNull(rows);
    assertEquals(1, rows.size());
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) QueryResponse(com.couchbase.client.core.msg.query.QueryResponse) QueryChunkRow(com.couchbase.client.core.msg.query.QueryChunkRow) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class QueryMessageHandlerTest method reportsCompletionOnlyOnceStreamingEnds.

/**
 * To avoid accidentally pipelining requests, we need to make sure that the handler only reports completion
 * once the streaming is fully completed, and not just the response completable future initially completed.
 *
 * <p>We send a request, make sure it is encoded correctly and then stream back the chunks, making sure the
 * final completion is only signaled once the last chunk arrived.</p>
 */
@Test
void reportsCompletionOnlyOnceStreamingEnds() {
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    EmbeddedChannel channel = new EmbeddedChannel(new QueryMessageHandler(endpoint, ENDPOINT_CTX));
    byte[] query = "doesn'tmatter".getBytes(CharsetUtil.UTF_8);
    QueryRequest request = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, ENV.retryStrategy(), CORE_CTX.authenticator(), "statement", query, false, null, null, null, null, null);
    channel.writeAndFlush(request);
    FullHttpRequest encodedRequest = channel.readOutbound();
    assertEquals("doesn'tmatter", encodedRequest.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.release(encodedRequest);
    verify(endpoint, never()).markRequestCompletion();
    assertFalse(request.response().isDone());
    ByteBuf fullResponse = Unpooled.copiedBuffer(readResource("success_response.json", QueryMessageHandlerTest.class), CharsetUtil.UTF_8);
    // send the header back first
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    channel.writeInbound(response);
    assertFalse(request.response().isDone());
    // send first chunk, should complete the future but not mark the endpoint completion
    HttpContent firstChunk = new DefaultHttpContent(fullResponse.readBytes(500));
    channel.writeInbound(firstChunk);
    assertTrue(request.response().isDone());
    verify(endpoint, never()).markRequestCompletion();
    // send the second chunk, no change
    HttpContent secondChunk = new DefaultHttpContent(fullResponse.readBytes(500));
    channel.writeInbound(secondChunk);
    verify(endpoint, never()).markRequestCompletion();
    // send the last chunk, mark for completion finally
    LastHttpContent lastChunk = new DefaultLastHttpContent(fullResponse.readBytes(fullResponse.readableBytes()));
    channel.writeInbound(lastChunk);
    verify(endpoint, times(1)).markRequestCompletion();
    channel.finishAndReleaseAll();
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) LastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) Test(org.junit.jupiter.api.Test)

Example 3 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class NodeTest method sendsToFoundGlobalService.

@Test
void sendsToFoundGlobalService() {
    final Service s = mock(Service.class);
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.type()).thenReturn(serviceType);
            when(s.states()).thenReturn(DirectProcessor.create());
            return s;
        }
    };
    node.addService(ServiceType.QUERY, 8091, Optional.empty()).block();
    QueryRequest r = mock(QueryRequest.class);
    when(r.serviceType()).thenReturn(ServiceType.QUERY);
    when(r.context()).thenReturn(new RequestContext(CTX, r));
    node.send(r);
    verify(s, times(1)).send(eq(r));
}
Also used : Optional(java.util.Optional) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ServiceType(com.couchbase.client.core.service.ServiceType) Service(com.couchbase.client.core.service.Service) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Example 4 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class NodeTest method retriesIfGlobalServiceNotFound.

@Test
void retriesIfGlobalServiceNotFound() {
    final Service s = mock(Service.class);
    final AtomicReference<Request<?>> retried = new AtomicReference<>();
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }

        @Override
        protected <R extends Request<? extends Response>> void sendIntoRetry(R request) {
            retried.set(request);
        }
    };
    QueryRequest r = mock(QueryRequest.class);
    when(r.serviceType()).thenReturn(ServiceType.QUERY);
    node.send(r);
    verify(s, never()).send(eq(r));
    assertEquals(r, retried.get());
}
Also used : Response(com.couchbase.client.core.msg.Response) Optional(java.util.Optional) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ServiceType(com.couchbase.client.core.service.ServiceType) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) Request(com.couchbase.client.core.msg.Request) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Service(com.couchbase.client.core.service.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 5 with QueryRequest

use of com.couchbase.client.core.msg.query.QueryRequest in project couchbase-jvm-clients by couchbase.

the class RoundRobinLocatorTest method skipNodeWithoutServiceEnabled.

@Test
void skipNodeWithoutServiceEnabled() {
    Locator locator = new RoundRobinLocator(ServiceType.QUERY, 0);
    QueryRequest request = mock(QueryRequest.class);
    ClusterConfig configMock = mock(ClusterConfig.class);
    when(configMock.hasClusterOrBucketConfig()).thenReturn(true);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    when(node1Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    when(node2Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(true);
    Node node3Mock = mock(Node.class);
    when(node3Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.103", 8091));
    when(node3Mock.serviceEnabled(ServiceType.QUERY)).thenReturn(false);
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock, node3Mock));
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(1)).send(request);
    verify(node3Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(2)).send(request);
    verify(node3Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(3)).send(request);
    verify(node3Mock, never()).send(request);
    locator.dispatch(request, nodes, configMock, null);
    verify(node1Mock, never()).send(request);
    verify(node2Mock, times(4)).send(request);
    verify(node3Mock, never()).send(request);
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) ArrayList(java.util.ArrayList) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

QueryRequest (com.couchbase.client.core.msg.query.QueryRequest)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)4 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)3 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)3 Duration (java.time.Duration)3 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)2 DefaultHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent)2 DefaultHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse)2 DefaultLastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent)2 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)2 ReducedQueryErrorContext (com.couchbase.client.core.error.context.ReducedQueryErrorContext)2 QueryResponse (com.couchbase.client.core.msg.query.QueryResponse)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 Service (com.couchbase.client.core.service.Service)2 ServiceType (com.couchbase.client.core.service.ServiceType)2 JsonObject (com.couchbase.client.java.json.JsonObject)2 Optional (java.util.Optional)2 Core (com.couchbase.client.core.Core)1