Search in sources :

Example 31 with Frame

use of com.datastax.oss.protocol.internal.Frame 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();
}
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) Register(com.datastax.oss.protocol.internal.request.Register) InetSocketAddress(java.net.InetSocketAddress) SetKeyspace(com.datastax.oss.protocol.internal.response.result.SetKeyspace) Test(org.junit.Test)

Example 32 with Frame

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

the class ProtocolInitHandlerTest method should_add_heartbeat_handler_to_pipeline_on_success.

@Test
public void should_add_heartbeat_handler_to_pipeline_on_success() {
    ProtocolInitHandler protocolInitHandler = new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false);
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, protocolInitHandler);
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    // heartbeat should initially not be in pipeline
    assertThat(channel.pipeline().get(ChannelFactory.HEARTBEAT_HANDLER_NAME)).isNull();
    // It should send a STARTUP message
    Frame 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();
    // should have added heartbeat handler to pipeline.
    assertThat(channel.pipeline().get(ChannelFactory.HEARTBEAT_HANDLER_NAME)).isEqualTo(heartbeatHandler);
    // should have removed itself from pipeline.
    assertThat(channel.pipeline().last()).isNotEqualTo(protocolInitHandler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 33 with Frame

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

the class ProtocolInitHandlerTest method should_fail_to_initialize_if_server_sends_auth_error.

@Test
public void should_fail_to_initialize_if_server_sends_auth_error() throws Throwable {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    String serverAuthenticator = "mockServerAuthenticator";
    AuthProvider authProvider = mock(AuthProvider.class);
    MockAuthenticator authenticator = new MockAuthenticator();
    when(authProvider.newAuthenticator(END_POINT, serverAuthenticator)).thenReturn(authenticator);
    when(internalDriverContext.getAuthProvider()).thenReturn(Optional.of(authProvider));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Startup.class);
    assertThat(connectFuture).isNotDone();
    writeInboundFrame(requestFrame, new Authenticate("mockServerAuthenticator"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(AuthResponse.class);
    assertThat(connectFuture).isNotDone();
    writeInboundFrame(requestFrame, new Error(ProtocolConstants.ErrorCode.AUTH_ERROR, "mock error"));
    assertThat(connectFuture).isFailed(e -> assertThat(e).isInstanceOf(AuthenticationException.class).hasMessage(String.format("Authentication error on node %s: server replied with 'mock error' to AuthResponse request", END_POINT)));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Frame(com.datastax.oss.protocol.internal.Frame) Authenticate(com.datastax.oss.protocol.internal.response.Authenticate) InetSocketAddress(java.net.InetSocketAddress) Error(com.datastax.oss.protocol.internal.response.Error) AuthProvider(com.datastax.oss.driver.api.core.auth.AuthProvider) Test(org.junit.Test)

Example 34 with Frame

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

the class SegmentToFrameDecoderTest method should_decode_self_contained.

@Test
public void should_decode_self_contained() {
    ByteBuf payload = UnpooledByteBufAllocator.DEFAULT.buffer();
    payload.writeBytes(encodeFrame(Void.INSTANCE));
    payload.writeBytes(encodeFrame(new AuthResponse(Bytes.fromHexString("0xabcdef"))));
    channel.writeInbound(new Segment<>(payload, true));
    Frame frame1 = channel.readInbound();
    assertThat(frame1.message).isInstanceOf(Void.class);
    Frame frame2 = channel.readInbound();
    assertThat(frame2.message).isInstanceOf(AuthResponse.class);
}
Also used : Frame(com.datastax.oss.protocol.internal.Frame) ByteBuf(io.netty.buffer.ByteBuf) AuthResponse(com.datastax.oss.protocol.internal.request.AuthResponse) 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