0

I have one unit test in which I am writing and reading from cassandra multiple times.

future = function(Cassandra update with value x) - async //write and read updated value
value = future.get(); //reading
print value; 
assert value == x;

//doing the above operation multiple times with different values of x; 

Running the same code multiple times is showing different results i.e printing different result for 'value' attribute.

I am using cassandra on local host with

replication = {
 'class': 'SimpleStrategy',
  'replication_factor': '1'
};

It's worth noting that I am writing and reading at a same row in the table (in all read and write, primary key is same). And though I am modifying the same object multiple times, but they are supposed to run sequentially as I am running blocking function future.get() after every update statement.

I am using Cassandra 2.0.14 with datastax driver and jdk 1.8.

Any ideas why I must be facing such behaviour?

  • It might be an issue of 'Test Run War' as I am using same object/ fixture in multiple assert statements. –  Aug 10 '15 at 19:00
  • Is there any way I can see what all queries have been applied on a keyspace i.e they are logged somewhere? –  Aug 10 '15 at 19:03

1 Answers1

0

Figured out the reason. In my code (not the test code), I wasn't actually writing and reading sequentially. Read wasn't waiting for the write to be completed.

What I was doing:

`CompletionStage<Void> Function() {
   someOperation
    .thenAccept(variable -> AsyncWriteInDb(variable));
}
// AsyncWriteInDb returns CompletionStage<Void> when write is completed.
// I was reading just after execution of this function. 
`

What I should be doing:

` CompletionStage<Void> Function() {
   someOperation
    .thenCompose(variable -> AsyncWriteInDb(variable));
}
//AsyncWriteInDb returns CompletionStage<Void> when write is completed.
`

It's easier to understand if I write earlier (wrong) code part as below:

`CompletionStage<Void> Function() {
   someOperation
    .thenAccept(variable -> {
       AsyncWriteInDb(variable);
       return;
    });
}
// thenAccept's lamda was returning after initiating an asyncDbWrite.
// Reading just after this doesnt ensure sequential reading after writing.
`