Elffuss BraidHub

kikocis/java-driver public

Add tests with disabled metadata (JAVA-828).
Changeset0c8a5cae871c5ee81b6f85080da965812e800ac23dfb376a4ab62078fddd69c7
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-08-12 22:54 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-14 18:59 +02:00
Parentsee08e578f68e

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/SchemaChangesTest.java +154 −4
@@ -25,10 +25,8 @@
2525 import org.mockito.ArgumentCaptor;
2626 import org.testng.annotations.*;
2727
28-import static org.mockito.Mockito.mock;
29-import static org.mockito.Mockito.reset;
30-import static org.mockito.Mockito.timeout;
31-import static org.mockito.Mockito.verify;
28+import static org.mockito.Matchers.any;
29+import static org.mockito.Mockito.*;
3230
3331 import com.datastax.driver.core.utils.Bytes;
3432
@@ -50,17 +48,23 @@
5048
5149 Cluster cluster1;
5250 Cluster cluster2; // a second cluster to check that other clients also get notified
51+ Cluster schemaDisabledCluster; // a cluster with schema metadata disabled.
5352
5453 // The metadatas of the two clusters (we'll test that they're kept in sync)
5554 List<Metadata> metadatas;
5655
5756 Session session1;
5857
58+ Session schemaDisabledSession;
59+
5960 SchemaChangeListener listener1;
6061 SchemaChangeListener listener2;
62+ SchemaChangeListener schemaDisabledListener;
6163
6264 List<SchemaChangeListener> listeners;
6365
66+ ControlConnection schemaDisabledControlConnection;
67+
6468 @Override
6569 protected Collection<String> getTableDefinitions() {
6670 return Lists.newArrayList();
@@ -77,6 +81,20 @@
7781 );
7882 cluster1 = builder.build();
7983 cluster2 = builder.build();
84+ schemaDisabledCluster = spy(configure(Cluster.builder())
85+ .addContactPointsWithPorts(Collections.singletonList(hostAddress))
86+ .withClusterName("schema-disabled")
87+ .withQueryOptions(new QueryOptions()
88+ .setRefreshNodeIntervalMillis(0)
89+ .setRefreshNodeListIntervalMillis(0)
90+ .setRefreshSchemaIntervalMillis(0)
91+ .setMetadataEnabled(false)
92+ ).build());
93+
94+ schemaDisabledSession = schemaDisabledCluster.connect();
95+
96+ schemaDisabledControlConnection = spy(schemaDisabledCluster.manager.controlConnection);
97+ schemaDisabledCluster.manager.controlConnection = schemaDisabledControlConnection;
8098
8199 metadatas = Lists.newArrayList(cluster1.getMetadata(), cluster2.getMetadata());
82100
@@ -86,6 +104,9 @@
86104 cluster1.register(listener2 = mock(SchemaChangeListener.class));
87105 listeners = Lists.newArrayList(listener1, listener2);
88106
107+ schemaDisabledCluster.register(schemaDisabledListener = mock(SchemaChangeListener.class));
108+ verify(schemaDisabledListener, times(1)).onRegister(schemaDisabledCluster);
109+
89110 execute(CREATE_KEYSPACE, "lowercase");
90111 execute(CREATE_KEYSPACE, "\"CaseSensitive\"");
91112 }
@@ -96,6 +117,8 @@
96117 cluster1.close();
97118 if (cluster2 != null)
98119 cluster2.close();
120+ if (schemaDisabledCluster != null)
121+ schemaDisabledCluster.close();
99122 }
100123
101124 @DataProvider(name = "existingKeyspaceName")
@@ -113,6 +136,19 @@
113136 for (SchemaChangeListener listener : listeners) {
114137 reset(listener);
115138 }
139+ reset(schemaDisabledControlConnection);
140+ }
141+
142+ /**
143+ * Ensures that a listener registered on a Cluster that has schema metadata disabled
144+ * is never invoked with schema change events.
145+ *
146+ * @jira_ticket JAVA-858
147+ * @since 2.0.11
148+ */
149+ @AfterMethod(groups = "short")
150+ public void verifyNoMoreInteractionsWithListener() {
151+ verifyNoMoreInteractions(schemaDisabledListener);
116152 }
117153
118154 @Test(groups = "short", dataProvider = "existingKeyspaceName")
@@ -125,6 +161,7 @@
125161 .isInKeyspace(handleId(keyspace))
126162 .hasName("table1");
127163 }
164+
128165 for (Metadata m : metadatas)
129166 assertThat(m.getKeyspace(keyspace).getTable("table1")).isNotNull();
130167 }
@@ -243,6 +280,119 @@
243280 }
244281 }
245282
283+
284+ /**
285+ * Ensures that calling {@link Metadata#newToken(String)} on a Cluster that has schema
286+ * metadata disabled will throw a {@link IllegalStateException}.
287+ *
288+ * @jira_ticket JAVA-858
289+ * @since 2.0.11
290+ */
291+ @Test(groups = "short", expectedExceptions = IllegalStateException.class)
292+ public void should_throw_illegal_state_exception_on_newToken_with_metadata_disabled() {
293+ Cluster cluster = configure(Cluster.builder())
294+ .addContactPointsWithPorts(Collections.singletonList(hostAddress))
295+ .withQueryOptions(new QueryOptions()
296+ .setRefreshNodeIntervalMillis(0)
297+ .setRefreshNodeListIntervalMillis(0)
298+ .setRefreshSchemaIntervalMillis(0)
299+ .setMetadataEnabled(false)
300+ ).build();
301+
302+ try {
303+ cluster.init();
304+ cluster.getMetadata().newToken("0x00");
305+ } finally {
306+ cluster.close();
307+ }
308+ }
309+
310+
311+ /**
312+ * Ensures that calling {@link Metadata#newTokenRange(Token, Token)} on a Cluster that has schema
313+ * metadata disabled will throw a {@link IllegalStateException}.
314+ *
315+ * @jira_ticket JAVA-858
316+ * @since 2.0.11
317+ */
318+ @Test(groups = "short", expectedExceptions = IllegalStateException.class)
319+ public void should_throw_illegal_state_exception_on_newTokenRange_with_metadata_disabled() {
320+ Cluster cluster = configure(Cluster.builder())
321+ .addContactPointsWithPorts(Collections.singletonList(hostAddress))
322+ .withQueryOptions(new QueryOptions()
323+ .setRefreshNodeIntervalMillis(0)
324+ .setRefreshNodeListIntervalMillis(0)
325+ .setRefreshSchemaIntervalMillis(0)
326+ .setMetadataEnabled(false)
327+ ).build();
328+
329+ try {
330+ cluster.init();
331+ Token.Factory factory = Token.getFactory("Murmur3Partitioner");
332+ Token token = factory.fromString(Long.toString(1));
333+ cluster.getMetadata().newTokenRange(token, token);
334+ } finally {
335+ cluster.close();
336+ }
337+ }
338+
339+ /**
340+ * Ensures that executing a query causing a schema change with a Cluster that has schema metadata
341+ * disabled will still wait on schema agreement, but not refresh the schema.
342+ *
343+ * @jira_ticket JAVA-858
344+ * @since 2.0.11
345+ */
346+ @Test(groups = "short")
347+ public void should_not_refresh_schema_on_schema_change_response() throws InterruptedException {
348+ ResultSet rs = schemaDisabledSession.execute(String.format(CREATE_TABLE, keyspace));
349+
350+ // Should still wait on schema agreement.
351+ assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue();
352+ assertThat(schemaDisabledCluster.getMetadata().checkSchemaAgreement()).isTrue();
353+
354+ // Wait up to 1 second (since refreshSchema submitted in an executor) and check that refreshSchema never called.
355+ verify(schemaDisabledControlConnection, after(1000).never()).refreshSchema(any(String.class), any(String.class));
356+ }
357+
358+ /**
359+ * Ensures that when schema metadata is enabled using {@link QueryOptions#setMetadataEnabled(boolean)}
360+ * that a schema and nodelist refresh is submitted, but only if schema metadata is currently disabled.
361+ *
362+ * @jira_ticket JAVA-858
363+ * @since 2.0.11
364+ */
365+ @Test(groups = "short")
366+ public void should_refresh_schema_and_token_map_if_schema_metadata_reenabled() throws Exception {
367+ try {
368+ schemaDisabledCluster.getConfiguration().getQueryOptions().setMetadataEnabled(true);
369+
370+ verify(schemaDisabledControlConnection, after(1000)).refreshSchema(null, null);
371+ // TODO: Remove atLeastOnce() if submitNodeRefresh removed from setMetadataEnabled.
372+ verify(schemaDisabledControlConnection, atLeastOnce()).refreshNodeListAndTokenMap();
373+
374+ // Ensure that there is schema metadata.
375+ assertThat(schemaDisabledCluster.getMetadata().getKeyspace(keyspace)).isNotNull();
376+ Token token1 = schemaDisabledCluster.getMetadata().newToken("0");
377+ Token token2 = schemaDisabledCluster.getMetadata().newToken("111111");
378+ assertThat(token1).isNotNull();
379+ assertThat(token2).isNotNull();
380+ assertThat(schemaDisabledCluster.getMetadata().newTokenRange(token1, token2)).isNotNull();
381+
382+ assertThat(schemaDisabledCluster.getMetadata().getTokenRanges()).isNotNull().isNotEmpty();
383+
384+ // Try enabling again and ensure schema is not refreshed again.
385+ reset(schemaDisabledControlConnection);
386+ schemaDisabledCluster.getConfiguration().getQueryOptions().setMetadataEnabled(true);
387+ verify(schemaDisabledControlConnection, after(1000).never()).refreshSchema(null, null);
388+ verify(schemaDisabledControlConnection, never()).refreshNodeListAndTokenMap();
389+ } finally {
390+ // Reset listener mock to not count it's interactions in this test.
391+ reset(schemaDisabledListener);
392+ schemaDisabledCluster.getConfiguration().getQueryOptions().setMetadataEnabled(false);
393+ }
394+ }
395+
246396 @AfterMethod(groups = "short")
247397 public void cleanup() throws InterruptedException {
248398 ListenableFuture<List<ResultSet>> f = Futures.successfulAsList(Lists.newArrayList(