kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8a16bdb3a49c416c19bd25bff119ff9ff22b816b2cf0ab0dcc74540ff2f12ffc |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-06-26 23:25 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-26 23:27 +02:00 |
| Parents | afd83c38482e |
1 file changed
added driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionIntegrationTest.java +77 −0
| @@ -0,0 +1,77 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import java.util.Collection; | |
| 19 | + | |
| 20 | +import com.google.common.collect.Lists; | |
| 21 | +import org.mockito.Mockito; | |
| 22 | +import org.testng.annotations.Test; | |
| 23 | + | |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | +import static org.mockito.Mockito.times; | |
| 26 | + | |
| 27 | +import com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy; | |
| 28 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Test that needs a real CCM cluster (as opposed to SCassandra for other specex tests), because | |
| 32 | + * it uses a protocol v3 feature. | |
| 33 | + */ | |
| 34 | +public class SpeculativeExecutionIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 35 | + | |
| 36 | + TimestampGenerator timestampGenerator; | |
| 37 | + | |
| 38 | + @Override | |
| 39 | + protected Collection<String> getTableDefinitions() { | |
| 40 | + return Lists.newArrayList("create table foo(k int primary key, v int)"); | |
| 41 | + } | |
| 42 | + | |
| 43 | + @Override | |
| 44 | + protected Cluster.Builder configure(Cluster.Builder builder) { | |
| 45 | + timestampGenerator = Mockito.spy(ServerSideTimestampGenerator.INSTANCE); | |
| 46 | + return builder | |
| 47 | + .withTimestampGenerator(timestampGenerator) | |
| 48 | + .withQueryOptions(new QueryOptions().setDefaultIdempotence(true)) | |
| 49 | + // Set an artificially low timeout to force speculative execution | |
| 50 | + .withSpeculativeExecutionPolicy(new ConstantSpeculativeExecutionPolicy(1, 2)); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Validates that if a query gets speculatively re-executed, the second execution uses the same default timestamp. | |
| 55 | + * | |
| 56 | + * @test_category tracing | |
| 57 | + * @jira_ticket JAVA-724 | |
| 58 | + * @expected_result timestamp generator invoked only once for a query that caused two executions. | |
| 59 | + */ | |
| 60 | + @Test(groups = "short") | |
| 61 | + @CassandraVersion(major = 2.1) | |
| 62 | + public void should_use_same_default_timestamp_for_all_executions() { | |
| 63 | + Metrics.Errors errors = cluster.getMetrics().getErrorMetrics(); | |
| 64 | + | |
| 65 | + Mockito.reset(timestampGenerator); | |
| 66 | + long execStartCount = errors.getSpeculativeExecutions().getCount(); | |
| 67 | + | |
| 68 | + BatchStatement batch = new BatchStatement(); | |
| 69 | + for (int k = 0; k < 1000; k++) { | |
| 70 | + batch.add(new SimpleStatement("insert into foo(k,v) values (?,1)", k)); | |
| 71 | + } | |
| 72 | + session.execute(batch); | |
| 73 | + | |
| 74 | + assertThat(errors.getSpeculativeExecutions().getCount()).isEqualTo(execStartCount + 1); | |
| 75 | + Mockito.verify(timestampGenerator, times(1)).next(); | |
| 76 | + } | |
| 77 | +} |