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