Elffuss BraidHub

kikocis/java-driver public

Set idempotence on Statement instead of globally

Makes test more reliable as its possible any ongoing queries could be
executed and increment the speculative execution counter.

Also reattempt test if scheduled execution did not occur.  It is
possible, yet unlikely, that a scheduled execution may not fire if the
timeout is not invoked before the response is received.   This should be
incredibly rare.
Changeset4a805f55f1287ceebb0bfed58bbf920a90a734e0903d6ffc24947427114eac4f
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-06-29 16:34 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2016-07-13 13:12 -07:00
Parents16435aedc323

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionIntegrationTest.java +25 −12
@@ -20,7 +20,7 @@
2020 import org.mockito.Mockito;
2121 import org.testng.annotations.Test;
2222
23-import static org.assertj.core.api.Assertions.assertThat;
23+import static org.assertj.core.api.Assertions.fail;
2424 import static org.mockito.Mockito.times;
2525
2626 /**
@@ -41,8 +41,7 @@
4141 timestampGenerator = Mockito.spy(ServerSideTimestampGenerator.INSTANCE);
4242 return Cluster.builder()
4343 .withTimestampGenerator(timestampGenerator)
44- .withQueryOptions(new QueryOptions().setDefaultIdempotence(true))
45- // Set an artificially low timeout to force speculative execution
44+ // Set an artificially low timeout to force speculative execution
4645 .withSpeculativeExecutionPolicy(new ConstantSpeculativeExecutionPolicy(1, 2));
4746 }
4847
@@ -58,16 +57,30 @@
5857 public void should_use_same_default_timestamp_for_all_executions() {
5958 Metrics.Errors errors = cluster().getMetrics().getErrorMetrics();
6059
61- Mockito.reset(timestampGenerator);
62- long execStartCount = errors.getSpeculativeExecutions().getCount();
63-
64- BatchStatement batch = new BatchStatement();
65- for (int k = 0; k < 1000; k++) {
66- batch.add(new SimpleStatement("insert into foo(k,v) values (?,1)", k));
60+ // The check is attempted up to 10 times to account for the small possibility that a
61+ // scheduled execution is not needed/exercised. Even though the policy is set up
62+ // to schedule an execution after 1ms, the timeout might not fire before the response is received.
63+ int tryCount = 0;
64+ int maxTries = 10;
65+ while (tryCount++ < maxTries) {
66+ Mockito.reset(timestampGenerator);
67+ long execStartCount = errors.getSpeculativeExecutions().getCount();
68+
69+ BatchStatement batch = new BatchStatement();
70+ for (int k = 0; k < 1000; k++) {
71+ batch.add(new SimpleStatement("insert into foo(k,v) values (?,1)", k).setIdempotent(true));
72+ }
73+ batch.setIdempotent(true);
74+ session().execute(batch);
75+
76+ if (errors.getSpeculativeExecutions().getCount() == execStartCount + 1) {
77+ Mockito.verify(timestampGenerator, times(1)).next();
78+ break;
79+ }
6780 }
68- session().execute(batch);
6981
70- assertThat(errors.getSpeculativeExecutions().getCount()).isEqualTo(execStartCount + 1);
71- Mockito.verify(timestampGenerator, times(1)).next();
82+ if (tryCount == maxTries) {
83+ fail("Observed no speculative executions in 10 attempts");
84+ }
7285 }
7386 }