Search in sources :

Example 21 with Query

use of com.datastax.oss.protocol.internal.request.Query in project java-driver by datastax.

the class DriverChannelTest method should_wait_for_coalesced_writes_when_closing_forcefully.

/**
 * Ensures that the potential delay introduced by the write coalescer does not mess with the
 * forceful shutdown sequence: any write submitted before {@link DriverChannel#forceClose()}
 * should get the "Channel was force-closed" error, whether it had been flushed or not.
 */
@Test
public void should_wait_for_coalesced_writes_when_closing_forcefully() {
    // Given
    MockResponseCallback responseCallback = new MockResponseCallback();
    driverChannel.write(new Query("test"), false, Frame.NO_PAYLOAD, responseCallback);
    // nothing written yet because the coalescer hasn't flushed
    assertNoOutboundFrame();
    // When
    Future<java.lang.Void> closeFuture = driverChannel.forceClose();
    // Then
    // not closed yet because there is still a pending write
    assertThat(closeFuture).isNotDone();
    assertNoOutboundFrame();
    // When
    // the coalescer finally runs
    writeCoalescer.triggerFlush();
    // and the pending write goes through
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame).isNotNull();
    // Then
    assertThat(closeFuture).isSuccess();
    assertThat(responseCallback.getFailure()).isInstanceOf(ClosedConnectionException.class).hasMessageContaining("Channel was force-closed");
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) ClosedConnectionException(com.datastax.oss.driver.api.core.connection.ClosedConnectionException) Void(com.datastax.oss.protocol.internal.response.result.Void) Test(org.junit.Test)

Example 22 with Query

use of com.datastax.oss.protocol.internal.request.Query in project java-driver by datastax.

the class ChannelFactoryAvailableIdsTest method should_report_available_ids.

@Test
public void should_report_available_ids() {
    // Given
    ChannelFactory factory = newChannelFactory();
    // When
    CompletionStage<DriverChannel> channelFuture = factory.connect(SERVER_ADDRESS, DriverChannelOptions.builder().build(), NoopNodeMetricUpdater.INSTANCE);
    completeSimpleChannelInit();
    // Then
    assertThatStage(channelFuture).isSuccess(channel -> {
        assertThat(channel.getAvailableIds()).isEqualTo(128);
        // Write a request, should decrease the count
        assertThat(channel.preAcquireId()).isTrue();
        Future<java.lang.Void> writeFuture = channel.write(new Query("test"), false, Frame.NO_PAYLOAD, responseCallback);
        assertThat(writeFuture).isSuccess(v -> {
            assertThat(channel.getAvailableIds()).isEqualTo(127);
            // Complete the request, should increase again
            writeInboundFrame(readOutboundFrame(), Void.INSTANCE);
            verify(responseCallback, timeout(500)).onResponse(any(Frame.class));
            assertThat(channel.getAvailableIds()).isEqualTo(128);
        });
    });
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) Void(com.datastax.oss.protocol.internal.response.result.Void) Test(org.junit.Test)

Example 23 with Query

use of com.datastax.oss.protocol.internal.request.Query in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_check_cluster_name_if_provided.

@Test
public void should_check_cluster_name_if_provided() {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, "expectedClusterName", END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    Frame requestFrame = readOutboundFrame();
    writeInboundFrame(requestFrame, new Ready());
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    Query query = (Query) requestFrame.message;
    assertThat(query.query).isEqualTo("SELECT cluster_name FROM system.local");
    assertThat(connectFuture).isNotDone();
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("expectedClusterName"));
    assertThat(connectFuture).isSuccess();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 24 with Query

use of com.datastax.oss.protocol.internal.request.Query in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_initialize_with_keyspace_and_events.

@Test
public void should_initialize_with_keyspace_and_events() {
    List<String> eventTypes = ImmutableList.of("foo", "bar");
    EventCallback eventCallback = mock(EventCallback.class);
    DriverChannelOptions driverChannelOptions = DriverChannelOptions.builder().withKeyspace(CqlIdentifier.fromCql("ks")).withEvents(eventTypes, eventCallback).build();
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, driverChannelOptions, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    writeInboundFrame(readOutboundFrame(), new Ready());
    writeInboundFrame(readOutboundFrame(), TestResponses.clusterNameResponse("someClusterName"));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    assertThat(((Query) requestFrame.message).query).isEqualTo("USE \"ks\"");
    writeInboundFrame(requestFrame, new SetKeyspace("ks"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Register.class);
    assertThat(((Register) requestFrame.message).eventTypes).containsExactly("foo", "bar");
    writeInboundFrame(requestFrame, new Ready());
    assertThat(connectFuture).isSuccess();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) Register(com.datastax.oss.protocol.internal.request.Register) InetSocketAddress(java.net.InetSocketAddress) SetKeyspace(com.datastax.oss.protocol.internal.response.result.SetKeyspace) Test(org.junit.Test)

Aggregations

Query (com.datastax.oss.protocol.internal.request.Query)24 Test (org.junit.Test)17 Frame (com.datastax.oss.protocol.internal.Frame)7 Prepare (com.datastax.oss.protocol.internal.request.Prepare)6 RawBytesQuery (com.datastax.dse.protocol.internal.request.RawBytesQuery)5 ScriptGraphStatement (com.datastax.dse.driver.api.core.graph.ScriptGraphStatement)4 DseQueryOptions (com.datastax.dse.protocol.internal.request.query.DseQueryOptions)4 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)4 Message (com.datastax.oss.protocol.internal.Message)4 Ready (com.datastax.oss.protocol.internal.response.Ready)4 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)4 ChannelFuture (io.netty.channel.ChannelFuture)4 InetSocketAddress (java.net.InetSocketAddress)4 ByteBuffer (java.nio.ByteBuffer)4 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)3 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)3 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)3 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 QueryOptions (com.datastax.oss.protocol.internal.request.query.QueryOptions)3