Search in sources :

Example 1 with CassandraSessionImpl

use of org.apache.ignite.cache.store.cassandra.session.CassandraSessionImpl in project ignite by apache.

the class CassandraSessionImplTest method executeFailureTest.

/**
 */
@SuppressWarnings("unchecked")
@Test
public void executeFailureTest() {
    Session session1 = mock(Session.class);
    Session session2 = mock(Session.class);
    when(session1.prepare(nullable(String.class))).thenReturn(preparedStatement1);
    when(session2.prepare(nullable(String.class))).thenReturn(preparedStatement2);
    ResultSetFuture rsFuture = mock(ResultSetFuture.class);
    ResultSet rs = mock(ResultSet.class);
    Iterator it = mock(Iterator.class);
    when(it.hasNext()).thenReturn(true);
    when(it.next()).thenReturn(mock(Row.class));
    when(rs.iterator()).thenReturn(it);
    when(rsFuture.getUninterruptibly()).thenReturn(rs);
    /* @formatter:off */
    when(session1.executeAsync(any(Statement.class))).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance")).thenThrow(new RuntimeException("this session should be refreshed / recreated"));
    when(session2.executeAsync(boundStatement1)).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"));
    when(session2.executeAsync(boundStatement2)).thenReturn(rsFuture);
    /* @formatter:on */
    Cluster cluster = mock(Cluster.class);
    when(cluster.connect()).thenReturn(session1).thenReturn(session2);
    when(session1.getCluster()).thenReturn(cluster);
    when(session2.getCluster()).thenReturn(cluster);
    Cluster.Builder builder = mock(Cluster.Builder.class);
    when(builder.build()).thenReturn(cluster);
    CassandraSessionImpl cassandraSession = new CassandraSessionImpl(builder, null, ConsistencyLevel.ONE, ConsistencyLevel.ONE, 0, mock(IgniteLogger.class));
    BatchExecutionAssistant<String, String> batchExecutionAssistant = new MyBatchExecutionAssistant();
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        data.add(String.valueOf(i));
    }
    cassandraSession.execute(batchExecutionAssistant, data);
    verify(cluster, times(2)).connect();
    verify(session1, times(1)).prepare(nullable(String.class));
    verify(session2, times(1)).prepare(nullable(String.class));
    assertEquals(10, batchExecutionAssistant.processedCount());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) CassandraSessionImpl(org.apache.ignite.cache.store.cassandra.session.CassandraSessionImpl) ResultSet(com.datastax.driver.core.ResultSet) Iterator(java.util.Iterator) Row(com.datastax.driver.core.Row) IgniteLogger(org.apache.ignite.IgniteLogger) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

Cluster (com.datastax.driver.core.Cluster)1 ResultSet (com.datastax.driver.core.ResultSet)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Row (com.datastax.driver.core.Row)1 Session (com.datastax.driver.core.Session)1 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 CassandraSessionImpl (org.apache.ignite.cache.store.cassandra.session.CassandraSessionImpl)1 Test (org.junit.Test)1