Search in sources :

Example 6 with Frame

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

the class InFlightHandlerTest method should_release_stream_id_when_orphaned_callback_receives_response.

@Test
public void should_release_stream_id_when_orphaned_callback_receives_response() {
    // Given
    addToPipeline();
    when(streamIds.acquire()).thenReturn(42);
    MockResponseCallback responseCallback = new MockResponseCallback();
    channel.writeAndFlush(new DriverChannel.RequestMessage(QUERY, false, Frame.NO_PAYLOAD, responseCallback));
    Frame requestFrame = readOutboundFrame();
    // When
    // means cancellation (see DriverChannel#cancel)
    channel.writeAndFlush(responseCallback);
    Frame responseFrame = buildInboundFrame(requestFrame, Void.INSTANCE);
    writeInboundFrame(responseFrame);
    // Then
    verify(streamIds).release(42);
    // The response is not propagated, because we assume a callback that cancelled managed its own
    // termination
    assertThat(responseCallback.getLastResponse()).isNull();
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Test(org.junit.Test)

Example 7 with Frame

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

the class ChannelFactoryTestBase method completeSimpleChannelInit.

/**
 * Simulate the sequence of roundtrips to initialize a simple channel without authentication or
 * keyspace (avoids repeating it in subclasses).
 */
protected void completeSimpleChannelInit() {
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Startup.class);
    writeInboundFrame(requestFrame, new Ready());
    requestFrame = readOutboundFrame();
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("mockClusterName"));
}
Also used : Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame)

Example 8 with Frame

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

the class ProtocolInitHandlerTest method should_query_supported_options.

@Test
public void should_query_supported_options() {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, true));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    // It should send an OPTIONS message
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    assertThat(connectFuture).isNotDone();
    // Simulate the SUPPORTED response
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    Map<String, List<String>> supportedOptions = channel.attr(DriverChannel.OPTIONS_KEY).get();
    assertThat(supportedOptions).containsKey("mock_key");
    assertThat(supportedOptions.get("mock_key")).containsOnly("mock_value");
    // It should send a STARTUP message
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Startup.class);
    assertThat(connectFuture).isNotDone();
    // Simulate a READY response
    writeInboundFrame(buildInboundFrame(requestFrame, new Ready()));
    // Simulate the cluster name check
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("someClusterName"));
    // Init should complete
    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) InetSocketAddress(java.net.InetSocketAddress) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 9 with Frame

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

the class ProtocolInitHandlerTest method should_fail_to_initialize_if_keyspace_is_invalid.

@Test
public void should_fail_to_initialize_if_keyspace_is_invalid() {
    DriverChannelOptions driverChannelOptions = DriverChannelOptions.builder().withKeyspace(CqlIdentifier.fromCql("ks")).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 Error(ProtocolConstants.ErrorCode.INVALID, "invalid keyspace"));
    assertThat(connectFuture).isFailed(error -> assertThat(error).isInstanceOf(InvalidKeyspaceException.class).hasMessage("invalid keyspace"));
}
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) Error(com.datastax.oss.protocol.internal.response.Error) Test(org.junit.Test)

Example 10 with Frame

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

the class ChannelFactoryProtocolNegotiationTest method should_fail_if_version_specified_and_not_supported_by_server.

@Test
@UseDataProvider("unsupportedProtocolCodes")
public void should_fail_if_version_specified_and_not_supported_by_server(int errorCode) {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(true);
    when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn("V4");
    when(protocolVersionRegistry.fromName("V4")).thenReturn(DefaultProtocolVersion.V4);
    ChannelFactory factory = newChannelFactory();
    // When
    CompletionStage<DriverChannel> channelFuture = factory.connect(SERVER_ADDRESS, DriverChannelOptions.DEFAULT, NoopNodeMetricUpdater.INSTANCE);
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V4.getCode());
    // Server does not support v4
    writeInboundFrame(requestFrame, new Error(errorCode, "Invalid or unsupported protocol version"));
    // Then
    assertThatStage(channelFuture).isFailed(e -> {
        assertThat(e).isInstanceOf(UnsupportedProtocolVersionException.class).hasMessageContaining("Host does not support protocol version V4");
        assertThat(((UnsupportedProtocolVersionException) e).getAttemptedVersions()).containsExactly(DefaultProtocolVersion.V4);
    });
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) UnsupportedProtocolVersionException(com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

Frame (com.datastax.oss.protocol.internal.Frame)34 Test (org.junit.Test)29 ChannelFuture (io.netty.channel.ChannelFuture)13 Ready (com.datastax.oss.protocol.internal.response.Ready)12 InetSocketAddress (java.net.InetSocketAddress)12 Error (com.datastax.oss.protocol.internal.response.Error)8 Query (com.datastax.oss.protocol.internal.request.Query)6 ByteBuf (io.netty.buffer.ByteBuf)4 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)3 AuthProvider (com.datastax.oss.driver.api.core.auth.AuthProvider)3 AuthResponse (com.datastax.oss.protocol.internal.request.AuthResponse)3 SetKeyspace (com.datastax.oss.protocol.internal.response.result.SetKeyspace)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)3 Register (com.datastax.oss.protocol.internal.request.Register)2 Authenticate (com.datastax.oss.protocol.internal.response.Authenticate)2 Void (com.datastax.oss.protocol.internal.response.result.Void)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 BusyConnectionException (com.datastax.oss.driver.api.core.connection.BusyConnectionException)1 ClosedConnectionException (com.datastax.oss.driver.api.core.connection.ClosedConnectionException)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1