Skip to content

Commit f63b797

Browse files
author
Alexandre Dutra
committed
Merge branch '2.1' into 3.0
Conflicts: driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java driver-core/src/test/java/com/datastax/driver/core/BatchStatementTest.java driver-core/src/test/java/com/datastax/driver/core/CustomTypeTest.java driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java driver-core/src/test/java/com/datastax/driver/core/FetchingTest.java driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyRefreshTest.java driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java driver-core/src/test/java/com/datastax/driver/core/SSLEncryptionTest.java driver-core/src/test/java/com/datastax/driver/core/SchemaRefreshDebouncerTest.java driver-core/src/test/java/com/datastax/driver/core/SchemaTest.java driver-core/src/test/java/com/datastax/driver/core/SessionLeakTest.java driver-core/src/test/java/com/datastax/driver/core/SessionTest.java driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java driver-core/src/test/java/com/datastax/driver/core/TestUtils.java driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java driver-core/src/test/java/com/datastax/driver/core/UserTypesTest.java driver-core/src/test/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicyIntegrationTest.java driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperDefaultKeyspaceTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperEnumTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperInvalidCollectionTypesTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperOptionTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTCollectionsTest.java driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java
2 parents ee1c2d8 + 1d3aa2f commit f63b797

126 files changed

Lines changed: 2294 additions & 2087 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

driver-core/src/test/java/com/datastax/driver/core/AbstractPoliciesTest.java

Lines changed: 53 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package com.datastax.driver.core;
1717

18+
import com.google.common.util.concurrent.Uninterruptibles;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.testng.annotations.AfterMethod;
1822
import org.testng.annotations.BeforeMethod;
1923

2024
import java.net.InetAddress;
@@ -23,45 +27,53 @@
2327
import java.util.Map;
2428
import java.util.concurrent.Callable;
2529

26-
import static com.datastax.driver.core.TestUtils.*;
30+
import static com.datastax.driver.core.ConditionChecker.check;
31+
import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_GENERIC_FORMAT;
32+
import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT;
2733
import static com.datastax.driver.core.querybuilder.QueryBuilder.batch;
2834
import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
29-
import static org.testng.Assert.*;
35+
import static java.util.concurrent.TimeUnit.MINUTES;
36+
import static org.testng.Assert.assertEquals;
3037

3138
public abstract class AbstractPoliciesTest extends CCMTestsSupport {
3239

33-
private static final boolean DEBUG = false;
40+
private static final Logger logger = LoggerFactory.getLogger(AbstractPoliciesTest.class);
41+
private String tableName;
42+
43+
private static class SchemaInAgreement implements Callable<Boolean> {
44+
45+
private final Cluster cluster;
46+
47+
private SchemaInAgreement(Cluster cluster) {
48+
this.cluster = cluster;
49+
}
50+
51+
@Override
52+
public Boolean call() throws Exception {
53+
return cluster.getMetadata().checkSchemaAgreement();
54+
}
55+
}
3456

3557
protected Map<InetAddress, Integer> coordinators = new HashMap<InetAddress, Integer>();
3658

3759
protected PreparedStatement prepared;
3860

3961
protected void createSchema(int replicationFactor) {
40-
final String ks = TestUtils.getAvailableKeyspaceName();
41-
session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ks, replicationFactor));
42-
// tests fail randomly here with InvalidQueryException: Keyspace 'xxx' does not exist
43-
ConditionChecker.awaitUntil(new Callable<Boolean>() {
44-
@Override
45-
public Boolean call() {
46-
return cluster.getMetadata().getKeyspace(ks) != null;
47-
}
48-
}, 60000);
49-
session.execute("USE " + ks);
50-
session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", SIMPLE_TABLE));
62+
final String ks = TestUtils.generateIdentifier("ks_");
63+
tableName = TestUtils.generateIdentifier("table_");
64+
session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ks, replicationFactor));
65+
useKeyspace(ks);
66+
session().execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", tableName));
67+
check().before(5, MINUTES).that(new SchemaInAgreement(cluster())).becomesTrue();
5168
}
5269

5370
protected void createMultiDCSchema(int dc1RF, int dc2RF) {
54-
final String ks = TestUtils.getAvailableKeyspaceName();
55-
session.execute(String.format(CREATE_KEYSPACE_GENERIC_FORMAT, ks, "NetworkTopologyStrategy", String.format("'dc1' : %d, 'dc2' : %d", dc1RF, dc2RF)));
56-
// tests fail randomly here with InvalidQueryException: Keyspace 'xxx' does not exist
57-
ConditionChecker.awaitUntil(new Callable<Boolean>() {
58-
@Override
59-
public Boolean call() {
60-
return cluster.getMetadata().getKeyspace(ks) != null;
61-
}
62-
}, 60000);
63-
session.execute("USE " + ks);
64-
session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", SIMPLE_TABLE));
71+
final String ks = TestUtils.generateIdentifier("ks_");
72+
tableName = TestUtils.generateIdentifier("table_");
73+
session().execute(String.format(CREATE_KEYSPACE_GENERIC_FORMAT, ks, "NetworkTopologyStrategy", String.format("'dc1' : %d, 'dc2' : %d", dc1RF, dc2RF)));
74+
useKeyspace(ks);
75+
session().execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", tableName));
76+
check().before(5, MINUTES).that(new SchemaInAgreement(cluster())).becomesTrue();
6577
}
6678

6779
/**
@@ -78,6 +90,12 @@ protected void resetCoordinators() {
7890
coordinators = new HashMap<InetAddress, Integer>();
7991
}
8092

93+
@AfterMethod(groups = "long")
94+
protected void pause() {
95+
// pause before engaging in another expensive CCM cluster creation
96+
Uninterruptibles.sleepUninterruptibly(1, MINUTES);
97+
}
98+
8199
private String queriedMapString() {
82100
StringBuilder sb = new StringBuilder();
83101
sb.append("{");
@@ -92,67 +110,15 @@ private String queriedMapString() {
92110
protected void assertQueried(String host, int n) {
93111
try {
94112
Integer queried = coordinators.get(InetAddress.getByName(host));
95-
if (DEBUG)
96-
System.out.println(String.format("Expected: %s\tReceived: %s", n, queried));
97-
else {
98-
assertEquals(queried == null ? 0 : queried, n, queriedMapString());
99-
}
100-
} catch (Exception e) {
101-
throw new RuntimeException(e);
102-
}
103-
}
104-
105-
protected void assertQueriedAtLeast(String host, int n) {
106-
try {
107-
Integer queried = coordinators.get(InetAddress.getByName(host));
108-
queried = queried == null ? 0 : queried;
109-
if (DEBUG)
110-
System.out.println(String.format("Expected > %s\tReceived: %s", n, queried));
113+
if (logger.isDebugEnabled())
114+
logger.debug(String.format("Expected: %s\tReceived: %s", n, queried));
111115
else
112-
assertTrue(queried >= n, "For " + host);
113-
} catch (Exception e) {
114-
throw new RuntimeException(e);
115-
}
116-
}
117-
118-
/**
119-
* Assert that one of the nodes in the list was queried with n, no matter which one
120-
*/
121-
protected void assertOneNodeQueried(int n, String... hosts) {
122-
try {
123-
boolean found = false;
124-
for (String host : hosts) {
125-
InetAddress addr = InetAddress.getByName(host);
126-
int queried = coordinators.containsKey(addr) ? coordinators.get(addr) : 0;
127-
if (DEBUG)
128-
System.out.println(String.format("Expected: %s\tReceived: %s", n, queried));
129-
else {
130-
131-
if (n == queried) {
132-
if (found)
133-
throw new AssertionError(String.format("Found 2 nodes with %s queries in %s", n, queriedMapString()));
134-
found = true;
135-
} else {
136-
if (queried != 0)
137-
throw new AssertionError(String.format("Host %s should have be queried: %s", addr, queriedMapString()));
138-
}
139-
}
140-
}
141-
if (!found)
142-
throw new AssertionError("Found no host queried exactly " + n + " times in " + queriedMapString());
116+
assertEquals(queried == null ? 0 : queried, n, queriedMapString());
143117
} catch (Exception e) {
144118
throw new RuntimeException(e);
145119
}
146120
}
147121

148-
protected void failDebug(String message) {
149-
if (DEBUG)
150-
System.out.println(message);
151-
else
152-
fail(message);
153-
}
154-
155-
156122
/**
157123
* Init methods that handle writes using batch and consistency options.
158124
*/
@@ -170,7 +136,7 @@ protected void init(int n, ConsistencyLevel cl) {
170136

171137
protected void init(int n, boolean batch, ConsistencyLevel cl) {
172138
write(n, batch, cl);
173-
prepared = session.prepare("SELECT * FROM " + SIMPLE_TABLE + " WHERE k = ?").setConsistencyLevel(cl);
139+
prepared = session().prepare("SELECT * FROM " + tableName + " WHERE k = ?").setConsistencyLevel(cl);
174140
}
175141

176142
protected void write(int n) {
@@ -191,11 +157,11 @@ protected void write(int n, boolean batch, ConsistencyLevel cl) {
191157
for (int i = 0; i < n; ++i)
192158
if (batch)
193159
// BUG: WriteType == SIMPLE
194-
session.execute(batch()
195-
.add(insertInto(SIMPLE_TABLE).values(new String[]{"k", "i"}, new Object[]{0, 0}))
160+
session().execute(batch()
161+
.add(insertInto(tableName).values(new String[]{"k", "i"}, new Object[]{0, 0}))
196162
.setConsistencyLevel(cl));
197163
else
198-
session.execute(new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", SIMPLE_TABLE)).setConsistencyLevel(cl));
164+
session().execute(new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", tableName)).setConsistencyLevel(cl));
199165
}
200166

201167

@@ -218,12 +184,13 @@ protected void query(int n, boolean usePrepared, ConsistencyLevel cl) {
218184
if (usePrepared) {
219185
BoundStatement bs = prepared.bind(0);
220186
for (int i = 0; i < n; ++i)
221-
addCoordinator(session.execute(bs));
187+
addCoordinator(session().execute(bs));
222188
} else {
223189
ByteBuffer routingKey = ByteBuffer.allocate(4);
224190
routingKey.putInt(0, 0);
225191
for (int i = 0; i < n; ++i)
226-
addCoordinator(session.execute(new SimpleStatement(String.format("SELECT * FROM %s WHERE k = 0", SIMPLE_TABLE)).setRoutingKey(routingKey).setConsistencyLevel(cl)));
192+
addCoordinator(session().execute(new SimpleStatement(String.format("SELECT * FROM %s WHERE k = 0", tableName)).setRoutingKey(routingKey).setConsistencyLevel(cl)));
227193
}
228194
}
195+
229196
}

driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.*;
3434
import java.util.concurrent.atomic.AtomicReference;
3535

36+
import static com.datastax.driver.core.ConditionChecker.check;
3637
import static org.assertj.core.api.Assertions.assertThat;
3738
import static org.mockito.Mockito.*;
3839
import static org.testng.Assert.fail;
@@ -43,8 +44,14 @@ public class AbstractReconnectionHandlerTest {
4344
ScheduledExecutorService executor;
4445
MockReconnectionSchedule schedule;
4546
MockReconnectionWork work;
46-
final AtomicReference<ListenableFuture<?>> future = new AtomicReference<ListenableFuture<?>>();
47+
AtomicReference<ListenableFuture<?>> future = new AtomicReference<ListenableFuture<?>>();
4748
AbstractReconnectionHandler handler;
49+
Callable<Boolean> nextTryAssigned = new Callable<Boolean>() {
50+
@Override
51+
public Boolean call() throws Exception {
52+
return handler.handlerFuture.nextTry != null;
53+
}
54+
};
4855

4956
@BeforeMethod(groups = {"unit", "short"})
5057
public void setup() {
@@ -60,7 +67,7 @@ protected Connection tryReconnect() throws ConnectionException, InterruptedExcep
6067

6168
@Override
6269
protected void onReconnection(Connection connection) {
63-
work.onReconnection(connection);
70+
work.onReconnection();
6471
}
6572
};
6673
}
@@ -151,12 +158,7 @@ public void should_stop_if_cancelled_between_attempts() {
151158
verify(executor, timeout(10000)).schedule(handler, schedule.delay, TimeUnit.MILLISECONDS);
152159

153160
// Wait until nextTry is assigned after schedule completes.
154-
ConditionChecker.awaitUntil(new Callable<Boolean>() {
155-
@Override
156-
public Boolean call() throws Exception {
157-
return handler.handlerFuture.nextTry != null;
158-
}
159-
}, 10000);
161+
check().before(10000).that(nextTryAssigned).becomesTrue();
160162

161163
future.get().cancel(false);
162164

@@ -343,7 +345,7 @@ protected Connection tryReconnect() throws ConnectionException {
343345
tries += 1;
344346
logger.debug("in reconnection work, wait for tick from main thread");
345347
try {
346-
barrier.await(10, TimeUnit.SECONDS);
348+
barrier.await(60, TimeUnit.SECONDS);
347349
logger.debug("in reconnection work, got tick from main thread, proceeding");
348350
} catch (Exception e) {
349351
fail("Error while waiting for tick", e);
@@ -364,14 +366,14 @@ protected Connection tryReconnect() throws ConnectionException {
364366
public void tick() {
365367
logger.debug("send tick to reconnection work");
366368
try {
367-
barrier.await(10, TimeUnit.SECONDS);
369+
barrier.await(60, TimeUnit.SECONDS);
368370
} catch (Exception e) {
369371
fail("Error while sending tick, no thread was waiting", e);
370372
}
371373
barrier.reset();
372374
}
373375

374-
protected void onReconnection(Connection connection) {
376+
protected void onReconnection() {
375377
success = true;
376378
}
377379
}

driver-core/src/test/java/com/datastax/driver/core/AggregateMetadataTest.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
import com.datastax.driver.core.utils.CassandraVersion;
1919
import org.testng.annotations.Test;
2020

21-
import java.util.Collection;
22-
2321
import static com.datastax.driver.core.Assertions.assertThat;
2422
import static com.datastax.driver.core.DataType.cint;
2523
import static com.datastax.driver.core.DataType.text;
26-
import static com.google.common.collect.Lists.newArrayList;
2724

2825
@CassandraVersion(major = 2.2)
2926
@CCMConfig(config = "enable_user_defined_functions:true")
@@ -35,10 +32,10 @@ public void should_parse_and_format_aggregate_with_initcond_and_no_finalfunc() {
3532
String cqlFunction = String.format("CREATE FUNCTION %s.cat(s text,v int) RETURNS NULL ON NULL INPUT RETURNS text LANGUAGE java AS 'return s+v;';", keyspace);
3633
String cqlAggregate = String.format("CREATE AGGREGATE %s.cat_tos(int) SFUNC cat STYPE text INITCOND '0';", keyspace);
3734
// when
38-
session.execute(cqlFunction);
39-
session.execute(cqlAggregate);
35+
session().execute(cqlFunction);
36+
session().execute(cqlAggregate);
4037
// then
41-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace);
38+
KeyspaceMetadata keyspace = cluster().getMetadata().getKeyspace(this.keyspace);
4239
FunctionMetadata stateFunc = keyspace.getFunction("cat", text(), cint());
4340
AggregateMetadata aggregate = keyspace.getAggregate("cat_tos", cint());
4441
assertThat(aggregate).isNotNull();
@@ -62,10 +59,10 @@ public void should_parse_and_format_aggregate_with_no_arguments() {
6259
String cqlFunction = String.format("CREATE FUNCTION %s.inc(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i+1;';", keyspace);
6360
String cqlAggregate = String.format("CREATE AGGREGATE %s.mycount() SFUNC inc STYPE int INITCOND 0;", keyspace);
6461
// when
65-
session.execute(cqlFunction);
66-
session.execute(cqlAggregate);
62+
session().execute(cqlFunction);
63+
session().execute(cqlAggregate);
6764
// then
68-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace);
65+
KeyspaceMetadata keyspace = cluster().getMetadata().getKeyspace(this.keyspace);
6966
FunctionMetadata stateFunc = keyspace.getFunction("inc", cint());
7067
AggregateMetadata aggregate = keyspace.getAggregate("mycount");
7168
assertThat(aggregate).isNotNull();
@@ -90,11 +87,11 @@ public void should_parse_and_format_aggregate_with_final_function() {
9087
String cqlFunction2 = String.format("CREATE FUNCTION %s.announce(i int) RETURNS NULL ON NULL INPUT RETURNS int LANGUAGE java AS 'return i;';", keyspace);
9188
String cqlAggregate = String.format("CREATE AGGREGATE %s.prettysum(int) SFUNC plus STYPE int FINALFUNC announce INITCOND 0;", keyspace);
9289
// when
93-
session.execute(cqlFunction1);
94-
session.execute(cqlFunction2);
95-
session.execute(cqlAggregate);
90+
session().execute(cqlFunction1);
91+
session().execute(cqlFunction2);
92+
session().execute(cqlAggregate);
9693
// then
97-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace);
94+
KeyspaceMetadata keyspace = cluster().getMetadata().getKeyspace(this.keyspace);
9895
FunctionMetadata stateFunc = keyspace.getFunction("plus", cint(), cint());
9996
FunctionMetadata finalFunc = keyspace.getFunction("announce", cint());
10097
AggregateMetadata aggregate = keyspace.getAggregate("prettysum", cint());
@@ -120,10 +117,10 @@ public void should_parse_and_format_aggregate_with_no_initcond() {
120117
String cqlFunction = String.format("CREATE FUNCTION %s.plus2(i int, j int) CALLED ON NULL INPUT RETURNS int LANGUAGE java AS 'return i+j;';", keyspace);
121118
String cqlAggregate = String.format("CREATE AGGREGATE %s.sum(int) SFUNC plus2 STYPE int;", keyspace);
122119
// when
123-
session.execute(cqlFunction);
124-
session.execute(cqlAggregate);
120+
session().execute(cqlFunction);
121+
session().execute(cqlAggregate);
125122
// then
126-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace);
123+
KeyspaceMetadata keyspace = cluster().getMetadata().getKeyspace(this.keyspace);
127124
FunctionMetadata stateFunc = keyspace.getFunction("plus2", cint(), cint());
128125
AggregateMetadata aggregate = keyspace.getAggregate("sum", cint());
129126
assertThat(aggregate).isNotNull();
@@ -155,10 +152,10 @@ public void should_parse_and_format_aggregate_with_udts() {
155152
+ "STYPE \"Address\";",
156153
keyspace);
157154
// when
158-
session.execute(cqlFunction);
159-
session.execute(cqlAggregate);
155+
session().execute(cqlFunction);
156+
session().execute(cqlAggregate);
160157
// then
161-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(this.keyspace);
158+
KeyspaceMetadata keyspace = cluster().getMetadata().getKeyspace(this.keyspace);
162159
UserType addressType = keyspace.getUserType("\"Address\"");
163160
FunctionMetadata stateFunc = keyspace.getFunction("\"MY_FUNC\"", addressType, addressType);
164161
AggregateMetadata aggregate = keyspace.getAggregate("\"MY_AGGREGATE\"", addressType);
@@ -175,8 +172,8 @@ public void should_parse_and_format_aggregate_with_udts() {
175172
}
176173

177174
@Override
178-
public Collection<String> createTestFixtures() {
179-
return newArrayList(
175+
public void onTestContextInitialized() {
176+
execute(
180177
String.format("CREATE TYPE IF NOT EXISTS %s.phone (number text)", keyspace),
181178
String.format("CREATE TYPE IF NOT EXISTS %s.\"Address\" ("
182179
+ " street text,"

0 commit comments

Comments
 (0)