Search in sources :

Example 21 with Frame

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

the class InFlightHandlerTest method should_set_keyspace.

@Test
public void should_set_keyspace() {
    // Given
    addToPipeline();
    ChannelPromise setKeyspacePromise = channel.newPromise();
    DriverChannel.SetKeyspaceEvent setKeyspaceEvent = new DriverChannel.SetKeyspaceEvent(CqlIdentifier.fromCql("ks"), setKeyspacePromise);
    // When
    channel.pipeline().fireUserEventTriggered(setKeyspaceEvent);
    Frame requestFrame = readOutboundFrame();
    // Then
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    writeInboundFrame(requestFrame, new SetKeyspace("ks"));
    assertThat(setKeyspacePromise).isSuccess();
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) ChannelPromise(io.netty.channel.ChannelPromise) SetKeyspace(com.datastax.oss.protocol.internal.response.result.SetKeyspace) Test(org.junit.Test)

Example 22 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_multi_response_callback_receives_last_response.

@Test
public void should_release_stream_id_when_orphaned_multi_response_callback_receives_last_response() {
    // Given
    addToPipeline();
    when(streamIds.acquire()).thenReturn(42);
    MockResponseCallback responseCallback = new MockResponseCallback(frame -> frame.message instanceof Error);
    channel.writeAndFlush(new DriverChannel.RequestMessage(QUERY, false, Frame.NO_PAYLOAD, responseCallback)).awaitUninterruptibly();
    Frame requestFrame = readOutboundFrame();
    for (int i = 0; i < 5; i++) {
        Frame responseFrame = buildInboundFrame(requestFrame, Void.INSTANCE);
        writeInboundFrame(responseFrame);
        assertThat(responseCallback.getLastResponse()).isSameAs(responseFrame);
        verify(streamIds, never()).release(42);
    }
    // When
    // cancelled mid-flight
    channel.writeAndFlush(responseCallback);
    // Then
    // subsequent non-final responses are not propagated (we assume the callback completed itself
    // already), but do not release the stream id
    writeInboundFrame(requestFrame, Void.INSTANCE);
    assertThat(responseCallback.getLastResponse()).isNull();
    verify(streamIds, never()).release(42);
    // When
    // the terminal response arrives
    writeInboundFrame(requestFrame, new Error(0, "test"));
    // Then
    // still not propagated but the id is released
    assertThat(responseCallback.getLastResponse()).isNull();
    verify(streamIds).release(42);
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) Test(org.junit.Test)

Example 23 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_considered_beta_by_server.

@Test
public void should_fail_if_version_specified_and_considered_beta_by_server() {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(true);
    when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn("V5");
    when(protocolVersionRegistry.fromName("V5")).thenReturn(DefaultProtocolVersion.V5);
    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.V5.getCode());
    // Server considers v5 beta, e.g. C* 3.10 or 3.11
    writeInboundFrame(requestFrame, new Error(ProtocolConstants.ErrorCode.PROTOCOL_ERROR, "Beta version of the protocol used (5/v5-beta), but USE_BETA flag is unset"));
    // Then
    assertThatStage(channelFuture).isFailed(e -> {
        assertThat(e).isInstanceOf(UnsupportedProtocolVersionException.class).hasMessageContaining("Host does not support protocol version V5");
        assertThat(((UnsupportedProtocolVersionException) e).getAttemptedVersions()).containsExactly(DefaultProtocolVersion.V5);
    });
}
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)

Example 24 with Frame

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

the class ChannelFactoryProtocolNegotiationTest method should_succeed_if_version_not_specified_and_server_supports_latest_supported.

@Test
public void should_succeed_if_version_not_specified_and_server_supports_latest_supported() {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(false);
    when(protocolVersionRegistry.highestNonBeta()).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());
    writeInboundFrame(requestFrame, new Ready());
    requestFrame = readOutboundFrame();
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("mockClusterName"));
    // Then
    assertThatStage(channelFuture).isSuccess(channel -> assertThat(channel.getClusterName()).isEqualTo("mockClusterName"));
    assertThat(factory.protocolVersion).isEqualTo(DefaultProtocolVersion.V4);
}
Also used : Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Test(org.junit.Test)

Example 25 with Frame

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

the class ChannelFactoryProtocolNegotiationTest method should_fail_if_negotiation_finds_no_matching_version.

@Test
@UseDataProvider("unsupportedProtocolCodes")
public void should_fail_if_negotiation_finds_no_matching_version(int errorCode) {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(false);
    when(protocolVersionRegistry.highestNonBeta()).thenReturn(DefaultProtocolVersion.V4);
    when(protocolVersionRegistry.downgrade(DefaultProtocolVersion.V4)).thenReturn(Optional.of(DefaultProtocolVersion.V3));
    when(protocolVersionRegistry.downgrade(DefaultProtocolVersion.V3)).thenReturn(Optional.empty());
    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"));
    // Client retries with v3
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V3.getCode());
    // Server does not support v3
    writeInboundFrame(requestFrame, new Error(errorCode, "Invalid or unsupported protocol version"));
    // Then
    assertThatStage(channelFuture).isFailed(e -> {
        assertThat(e).isInstanceOf(UnsupportedProtocolVersionException.class).hasMessageContaining("Protocol negotiation failed: could not find a common version " + "(attempted: [V4, V3])");
        assertThat(((UnsupportedProtocolVersionException) e).getAttemptedVersions()).containsExactly(DefaultProtocolVersion.V4, DefaultProtocolVersion.V3);
    });
}
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