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();
}
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"));
}
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();
}
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"));
}
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);
});
}
Aggregations