Search in sources :

Example 1 with Unprepared

use of com.datastax.oss.protocol.internal.response.error.Unprepared in project java-driver by datastax.

the class CqlRequestHandlerTest method should_reprepare_on_the_fly_if_not_prepared.

@Test
public void should_reprepare_on_the_fly_if_not_prepared() throws InterruptedException {
    ByteBuffer mockId = Bytes.fromHexString("0xffff");
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    when(preparedStatement.getId()).thenReturn(mockId);
    ColumnDefinitions columnDefinitions = mock(ColumnDefinitions.class);
    when(columnDefinitions.size()).thenReturn(0);
    when(preparedStatement.getResultSetDefinitions()).thenReturn(columnDefinitions);
    BoundStatement boundStatement = mock(BoundStatement.class);
    when(boundStatement.getPreparedStatement()).thenReturn(preparedStatement);
    when(boundStatement.getValues()).thenReturn(Collections.emptyList());
    when(boundStatement.getNowInSeconds()).thenReturn(Statement.NO_NOW_IN_SECONDS);
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder();
    // For the first attempt that gets the UNPREPARED response
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    // For the second attempt that succeeds
    harnessBuilder.withResponse(node1, defaultFrameOf(singleRow()));
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        // The handler will look for the info to reprepare in the session's cache, put it there
        ConcurrentMap<ByteBuffer, RepreparePayload> repreparePayloads = new ConcurrentHashMap<>();
        repreparePayloads.put(mockId, new RepreparePayload(mockId, "mock query", null, Collections.emptyMap()));
        when(harness.getSession().getRepreparePayloads()).thenReturn(repreparePayloads);
        CompletionStage<AsyncResultSet> resultSetFuture = new CqlRequestHandler(boundStatement, harness.getSession(), harness.getContext(), "test").handle();
        // Before we proceed, mock the PREPARE exchange that will occur as soon as we complete the
        // first response.
        node1Behavior.mockFollowupRequest(Prepare.class, defaultFrameOf(new Prepared(Bytes.getArray(mockId), null, null, null)));
        node1Behavior.setWriteSuccess();
        node1Behavior.setResponseSuccess(defaultFrameOf(new Unprepared("mock message", Bytes.getArray(mockId))));
        // Should now re-prepare, re-execute and succeed.
        assertThatStage(resultSetFuture).isSuccess();
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Prepared(com.datastax.oss.protocol.internal.response.result.Prepared) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ByteBuffer(java.nio.ByteBuffer) RepreparePayload(com.datastax.oss.driver.internal.core.session.RepreparePayload) Unprepared(com.datastax.oss.protocol.internal.response.error.Unprepared) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test)

Aggregations

AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 RepreparePayload (com.datastax.oss.driver.internal.core.session.RepreparePayload)1 Unprepared (com.datastax.oss.protocol.internal.response.error.Unprepared)1 Prepared (com.datastax.oss.protocol.internal.response.result.Prepared)1 ByteBuffer (java.nio.ByteBuffer)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1