Search in sources :

Example 6 with UnsupportedProtocolVersionException

use of com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException 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 7 with UnsupportedProtocolVersionException

use of com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException 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

UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)7 Test (org.junit.Test)5 Frame (com.datastax.oss.protocol.internal.Frame)3 Error (com.datastax.oss.protocol.internal.response.Error)3 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)2 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)2 CassandraRequirement (com.datastax.oss.driver.api.testinfra.CassandraRequirement)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 DseProtocolVersion (com.datastax.dse.driver.api.core.DseProtocolVersion)1 DefaultProtocolVersion (com.datastax.oss.driver.api.core.DefaultProtocolVersion)1 Version (com.datastax.oss.driver.api.core.Version)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 Node (com.datastax.oss.driver.api.core.metadata.Node)1 TypesafeDriverConfig (com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig)1 NettyOptions (com.datastax.oss.driver.internal.core.context.NettyOptions)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1