Search in sources :

Example 6 with PushQueryId

use of io.confluent.ksql.rest.entity.PushQueryId in project ksql by confluentinc.

the class ClientTest method shouldTerminatePushQueryIssuedViaExecuteQuery.

@Test
public void shouldTerminatePushQueryIssuedViaExecuteQuery() throws Exception {
    // Given
    // Issue non-terminating push query via executeQuery(). This is NOT an expected use case
    final BatchedQueryResult batchedQueryResult = javaClient.executeQuery(DEFAULT_PUSH_QUERY);
    final String queryId = batchedQueryResult.queryID().get();
    assertThat(queryId, is(notNullValue()));
    // Query is running on server, and BatchedQueryResult is not complete
    assertThat(server.getQueryIDs(), hasSize(1));
    assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
    assertThat(batchedQueryResult.isDone(), is(false));
    // When
    javaClient.terminatePushQuery(queryId).get();
    // Then: query is no longer running on server, and BatchedQueryResult is complete
    assertThat(server.getQueryIDs(), hasSize(0));
    assertThatEventually(batchedQueryResult::isDone, is(true));
    assertThat(batchedQueryResult.isCompletedExceptionally(), is(false));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) BaseApiTest(io.confluent.ksql.api.BaseApiTest) Test(org.junit.Test)

Example 7 with PushQueryId

use of io.confluent.ksql.rest.entity.PushQueryId in project ksql by confluentinc.

the class ApiTest method shouldExecuteMultiplePushQueries.

@Test
public void shouldExecuteMultiplePushQueries() throws Exception {
    int numQueries = 10;
    for (int i = 0; i < numQueries; i++) {
        // When
        QueryResponse queryResponse = executePushQueryAndWaitForRows(DEFAULT_PUSH_QUERY_REQUEST_BODY);
        // Then
        assertThat(server.getQueryIDs(), hasSize(i + 1));
        String queryId = queryResponse.responseObject.getString("queryId");
        assertThat(queryId, is(notNullValue()));
        assertThat(server.getQueryIDs(), hasItem(new PushQueryId(queryId)));
    }
}
Also used : QueryResponse(io.confluent.ksql.api.utils.QueryResponse) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) Test(org.junit.Test)

Example 8 with PushQueryId

use of io.confluent.ksql.rest.entity.PushQueryId in project ksql by confluentinc.

the class ApiTest method shouldCloseQuery.

@Test
@CoreApiTest
public void shouldCloseQuery() throws Exception {
    // Create a write stream to capture the incomplete response
    ReceiveStream writeStream = new ReceiveStream(vertx);
    VertxCompletableFuture<HttpResponse<Void>> responseFuture = new VertxCompletableFuture<>();
    // Make the request to stream a query
    sendPostRequest("/query-stream", (request) -> request.as(BodyCodec.pipe(writeStream)).sendJsonObject(DEFAULT_PUSH_QUERY_REQUEST_BODY, responseFuture));
    // Wait for all rows in the response to arrive
    assertThatEventually(() -> {
        try {
            Buffer buff = writeStream.getBody();
            QueryResponse queryResponse = new QueryResponse(buff.toString());
            return queryResponse.rows.size();
        } catch (Throwable t) {
            return Integer.MAX_VALUE;
        }
    }, is(DEFAULT_JSON_ROWS.size()));
    // The response shouldn't have ended yet
    assertThat(writeStream.isEnded(), is(false));
    // Assert the query is still live on the server
    QueryResponse queryResponse = new QueryResponse(writeStream.getBody().toString());
    String queryId = queryResponse.responseObject.getString("queryId");
    assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
    assertThat(server.getQueryIDs(), hasSize(1));
    assertThat(testEndpoints.getQueryPublishers(), hasSize(1));
    // Now send another request to close the query
    JsonObject closeQueryRequestBody = new JsonObject().put("queryId", queryId);
    HttpResponse<Buffer> closeQueryResponse = sendPostRequest("/close-query", closeQueryRequestBody.toBuffer());
    assertThat(closeQueryResponse.statusCode(), is(200));
    // Assert the query no longer exists on the server
    assertThat(server.getQueryIDs(), not(hasItem(new PushQueryId(queryId))));
    assertThat(server.getQueryIDs(), hasSize(0));
    // The response should now be ended
    assertThatEventually(writeStream::isEnded, is(true));
    HttpResponse<Void> response = responseFuture.get();
    assertThat(response.statusCode(), is(200));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) HttpResponse(io.vertx.ext.web.client.HttpResponse) JsonObject(io.vertx.core.json.JsonObject) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) Test(org.junit.Test)

Example 9 with PushQueryId

use of io.confluent.ksql.rest.entity.PushQueryId in project ksql by confluentinc.

the class ApiTest method shouldCloseMultipleQueriesOnDifferentConnectionsWhenConnectionsAreClosed.

@Test
public void shouldCloseMultipleQueriesOnDifferentConnectionsWhenConnectionsAreClosed() throws Exception {
    int numConnections = 5;
    int numQueries = 5;
    List<WebClient> clients = new ArrayList<>();
    for (int i = 0; i < numConnections; i++) {
        WebClient client = createClient();
        clients.add(client);
        for (int j = 0; j < numQueries; j++) {
            // When
            QueryResponse queryResponse = executePushQueryAndWaitForRows(client, DEFAULT_PUSH_QUERY_REQUEST_BODY);
            // Then
            String queryId = queryResponse.responseObject.getString("queryId");
            assertThat(queryId, is(notNullValue()));
            assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
            int queries = i * numQueries + j + 1;
            assertThat(server.getQueryIDs(), hasSize(queries));
            assertThat(server.queryConnectionCount(), is(i + 1));
        }
    }
    int count = 0;
    for (WebClient client : clients) {
        // When
        client.close();
        // Then
        int connections = numConnections - count - 1;
        assertThatEventually(server::queryConnectionCount, is(connections));
        assertThat(server.getQueryIDs(), hasSize(numQueries * connections));
        count++;
    }
}
Also used : QueryResponse(io.confluent.ksql.api.utils.QueryResponse) ArrayList(java.util.ArrayList) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) WebClient(io.vertx.ext.web.client.WebClient) Test(org.junit.Test)

Example 10 with PushQueryId

use of io.confluent.ksql.rest.entity.PushQueryId in project ksql by confluentinc.

the class ApiTest method shouldCloseQueriesOnDifferentConnectionsWhenConnectionsAreClosed.

@Test
public void shouldCloseQueriesOnDifferentConnectionsWhenConnectionsAreClosed() throws Exception {
    int numQueries = 10;
    List<WebClient> clients = new ArrayList<>();
    for (int i = 0; i < numQueries; i++) {
        // We use different clients to ensure requests are sent on different connections
        WebClient client = createClient();
        clients.add(client);
        // When
        QueryResponse queryResponse = executePushQueryAndWaitForRows(client, DEFAULT_PUSH_QUERY_REQUEST_BODY);
        // Then
        String queryId = queryResponse.responseObject.getString("queryId");
        assertThat(queryId, is(notNullValue()));
        assertThat(server.getQueryIDs().contains(new PushQueryId(queryId)), is(true));
        assertThat(server.getQueryIDs(), hasSize(i + 1));
        assertThat(server.queryConnectionCount(), is(i + 1));
    }
    // Now close them one by one and make sure queries are cleaned up
    int count = 0;
    for (WebClient client : clients) {
        // Given
        client.close();
        // Then
        int num = numQueries - count - 1;
        assertThatEventually(server::queryConnectionCount, is(num));
        assertThat(server.getQueryIDs(), hasSize(num));
        count++;
    }
}
Also used : QueryResponse(io.confluent.ksql.api.utils.QueryResponse) ArrayList(java.util.ArrayList) PushQueryId(io.confluent.ksql.rest.entity.PushQueryId) WebClient(io.vertx.ext.web.client.WebClient) Test(org.junit.Test)

Aggregations

PushQueryId (io.confluent.ksql.rest.entity.PushQueryId)10 Test (org.junit.Test)10 QueryResponse (io.confluent.ksql.api.utils.QueryResponse)8 BaseApiTest (io.confluent.ksql.api.BaseApiTest)2 Buffer (io.vertx.core.buffer.Buffer)2 JsonObject (io.vertx.core.json.JsonObject)2 WebClient (io.vertx.ext.web.client.WebClient)2 ArrayList (java.util.ArrayList)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ReceiveStream (io.confluent.ksql.api.utils.ReceiveStream)1 VertxCompletableFuture (io.confluent.ksql.util.VertxCompletableFuture)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1