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