kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 0c8a5cae871c5ee81b6f85080da965812e800ac23dfb376a4ab62078fddd69c7 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-08-12 22:54 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-08-14 18:59 +02:00 |
| Parents | ee08e578f68e |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/SchemaChangesTest.java +154 −4
| @@ -25,10 +25,8 @@ | ||
| 25 | 25 | import org.mockito.ArgumentCaptor; |
| 26 | 26 | import org.testng.annotations.*; |
| 27 | 27 | |
| 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.*; | |
| 32 | 30 | |
| 33 | 31 | import com.datastax.driver.core.utils.Bytes; |
| 34 | 32 | |
| @@ -50,17 +48,23 @@ | ||
| 50 | 48 | |
| 51 | 49 | Cluster cluster1; |
| 52 | 50 | Cluster cluster2; // a second cluster to check that other clients also get notified |
| 51 | + Cluster schemaDisabledCluster; // a cluster with schema metadata disabled. | |
| 53 | 52 | |
| 54 | 53 | // The metadatas of the two clusters (we'll test that they're kept in sync) |
| 55 | 54 | List<Metadata> metadatas; |
| 56 | 55 | |
| 57 | 56 | Session session1; |
| 58 | 57 | |
| 58 | + Session schemaDisabledSession; | |
| 59 | + | |
| 59 | 60 | SchemaChangeListener listener1; |
| 60 | 61 | SchemaChangeListener listener2; |
| 62 | + SchemaChangeListener schemaDisabledListener; | |
| 61 | 63 | |
| 62 | 64 | List<SchemaChangeListener> listeners; |
| 63 | 65 | |
| 66 | + ControlConnection schemaDisabledControlConnection; | |
| 67 | + | |
| 64 | 68 | @Override |
| 65 | 69 | protected Collection<String> getTableDefinitions() { |
| 66 | 70 | return Lists.newArrayList(); |
| @@ -77,6 +81,20 @@ | ||
| 77 | 81 | ); |
| 78 | 82 | cluster1 = builder.build(); |
| 79 | 83 | 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; | |
| 80 | 98 | |
| 81 | 99 | metadatas = Lists.newArrayList(cluster1.getMetadata(), cluster2.getMetadata()); |
| 82 | 100 | |
| @@ -86,6 +104,9 @@ | ||
| 86 | 104 | cluster1.register(listener2 = mock(SchemaChangeListener.class)); |
| 87 | 105 | listeners = Lists.newArrayList(listener1, listener2); |
| 88 | 106 | |
| 107 | + schemaDisabledCluster.register(schemaDisabledListener = mock(SchemaChangeListener.class)); | |
| 108 | + verify(schemaDisabledListener, times(1)).onRegister(schemaDisabledCluster); | |
| 109 | + | |
| 89 | 110 | execute(CREATE_KEYSPACE, "lowercase"); |
| 90 | 111 | execute(CREATE_KEYSPACE, "\"CaseSensitive\""); |
| 91 | 112 | } |
| @@ -96,6 +117,8 @@ | ||
| 96 | 117 | cluster1.close(); |
| 97 | 118 | if (cluster2 != null) |
| 98 | 119 | cluster2.close(); |
| 120 | + if (schemaDisabledCluster != null) | |
| 121 | + schemaDisabledCluster.close(); | |
| 99 | 122 | } |
| 100 | 123 | |
| 101 | 124 | @DataProvider(name = "existingKeyspaceName") |
| @@ -113,6 +136,19 @@ | ||
| 113 | 136 | for (SchemaChangeListener listener : listeners) { |
| 114 | 137 | reset(listener); |
| 115 | 138 | } |
| 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); | |
| 116 | 152 | } |
| 117 | 153 | |
| 118 | 154 | @Test(groups = "short", dataProvider = "existingKeyspaceName") |
| @@ -125,6 +161,7 @@ | ||
| 125 | 161 | .isInKeyspace(handleId(keyspace)) |
| 126 | 162 | .hasName("table1"); |
| 127 | 163 | } |
| 164 | + | |
| 128 | 165 | for (Metadata m : metadatas) |
| 129 | 166 | assertThat(m.getKeyspace(keyspace).getTable("table1")).isNotNull(); |
| 130 | 167 | } |
| @@ -243,6 +280,119 @@ | ||
| 243 | 280 | } |
| 244 | 281 | } |
| 245 | 282 | |
| 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 | + | |
| 246 | 396 | @AfterMethod(groups = "short") |
| 247 | 397 | public void cleanup() throws InterruptedException { |
| 248 | 398 | ListenableFuture<List<ResultSet>> f = Futures.successfulAsList(Lists.newArrayList( |