Search in sources :

Example 1 with Frame

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

the class ProtocolInitHandlerTest method should_initialize_with_keyspace.

@Test
public void should_initialize_with_keyspace() {
    DriverChannelOptions options = DriverChannelOptions.builder().withKeyspace(CqlIdentifier.fromCql("ks")).build();
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, options, 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"));
    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) Query(com.datastax.oss.protocol.internal.request.Query) InetSocketAddress(java.net.InetSocketAddress) SetKeyspace(com.datastax.oss.protocol.internal.response.result.SetKeyspace) Test(org.junit.Test)

Example 2 with Frame

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

the class ProtocolInitHandlerTest method should_initialize_with_events.

@Test
public void should_initialize_with_events() {
    List<String> eventTypes = ImmutableList.of("foo", "bar");
    EventCallback eventCallback = mock(EventCallback.class);
    DriverChannelOptions driverChannelOptions = DriverChannelOptions.builder().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(Register.class);
    assertThat(((Register) requestFrame.message).eventTypes).containsExactly("foo", "bar");
    writeInboundFrame(requestFrame, new Ready());
    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) Register(com.datastax.oss.protocol.internal.request.Register) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 3 with Frame

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

the class DriverChannelTest method should_wait_for_coalesced_writes_when_closing_gracefully.

/**
 * Ensures that the potential delay introduced by the write coalescer does not mess with the
 * graceful shutdown sequence: any write submitted before {@link DriverChannel#close()} is
 * guaranteed to complete.
 */
@Test
public void should_wait_for_coalesced_writes_when_closing_gracefully() {
    // 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.close();
    // Then
    // not closed yet because there is still a pending write
    assertThat(closeFuture).isNotDone();
    assertNoOutboundFrame();
    // When
    // the coalescer finally runs
    writeCoalescer.triggerFlush();
    // Then
    // the pending write goes through
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame).isNotNull();
    // not closed yet because there is now a pending response
    assertThat(closeFuture).isNotDone();
    // When
    // the pending response arrives
    writeInboundFrame(requestFrame, Void.INSTANCE);
    assertThat(responseCallback.getLastResponse().message).isEqualTo(Void.INSTANCE);
    // Then
    assertThat(closeFuture).isSuccess();
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) Void(com.datastax.oss.protocol.internal.response.result.Void) Test(org.junit.Test)

Example 4 with Frame

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

the class InFlightHandlerTest method should_notify_callback_of_response.

@Test
public void should_notify_callback_of_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
    Frame responseFrame = buildInboundFrame(requestFrame, Void.INSTANCE);
    writeInboundFrame(responseFrame);
    // Then
    assertThat(responseCallback.getLastResponse()).isSameAs(responseFrame);
    verify(streamIds).release(42);
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) Test(org.junit.Test)

Example 5 with Frame

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

the class InFlightHandlerTest method should_assign_streamid_and_send_frame.

@Test
public void should_assign_streamid_and_send_frame() {
    // Given
    addToPipeline();
    when(streamIds.acquire()).thenReturn(42);
    MockResponseCallback responseCallback = new MockResponseCallback();
    // When
    ChannelFuture writeFuture = channel.writeAndFlush(new DriverChannel.RequestMessage(QUERY, false, Frame.NO_PAYLOAD, responseCallback));
    // Then
    assertThat(writeFuture).isSuccess();
    verify(streamIds).acquire();
    Frame frame = readOutboundFrame();
    assertThat(frame.streamId).isEqualTo(42);
    assertThat(frame.message).isEqualTo(QUERY);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Frame(com.datastax.oss.protocol.internal.Frame) Test(org.junit.Test)

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