akhaku commented on code in PR #1920:
URL:
https://github.com/apache/cassandra-java-driver/pull/1920#discussion_r1525614078
##########
core/src/test/java/com/datastax/oss/driver/internal/core/metadata/MetadataManagerTest.java:
##########
@@ -286,6 +290,25 @@ public void should_remove_node() {
assertThat(refresh.broadcastRpcAddressToRemove).isEqualTo(broadcastRpcAddress2);
}
+ @Test
+ public void refreshSchema_should_work() {
+ // Given
+ IllegalStateException expectedException = new IllegalStateException("Error
we're testing");
+ when(schemaQueriesFactory.newInstance()).thenThrow(expectedException);
+
when(topologyMonitor.refreshNodeList()).thenReturn(CompletableFuture.completedFuture(ImmutableList.of(mock(NodeInfo.class))));
+
when(topologyMonitor.checkSchemaAgreement()).thenReturn(CompletableFuture.completedFuture(Boolean.TRUE));
+ when(controlConnection.init(anyBoolean(), anyBoolean(),
anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
+ metadataManager.refreshNodes(); // required internal state setup for this
+ waitForPendingAdminTasks(() -> metadataManager.refreshes.size() == 1); //
sanity check
+
+ // When
+ CompletionStage<MetadataManager.RefreshSchemaResult> result =
metadataManager.refreshSchema("foo", true, true);
+
+ // Then
+ waitForPendingAdminTasks(() -> result.toCompletableFuture().isDone());
Review Comment:
Not strictly required, could remove. Really the `assertThatStage` after this
is what we're testing.
##########
core/src/main/java/com/datastax/oss/driver/internal/core/metadata/MetadataManager.java:
##########
@@ -437,30 +437,35 @@ private void
startSchemaRequest(CompletableFuture<RefreshSchemaResult> refreshFu
if (agreementError != null) {
refreshFuture.completeExceptionally(agreementError);
} else {
- schemaQueriesFactory
- .newInstance()
- .execute()
- .thenApplyAsync(this::parseAndApplySchemaRows,
adminExecutor)
- .whenComplete(
- (newMetadata, metadataError) -> {
- if (metadataError != null) {
-
refreshFuture.completeExceptionally(metadataError);
- } else {
- refreshFuture.complete(
- new RefreshSchemaResult(newMetadata,
schemaInAgreement));
- }
-
- firstSchemaRefreshFuture.complete(null);
-
- currentSchemaRefresh = null;
- // If another refresh was enqueued during this
one, run it now
- if (queuedSchemaRefresh != null) {
- CompletableFuture<RefreshSchemaResult> tmp =
- this.queuedSchemaRefresh;
- this.queuedSchemaRefresh = null;
- startSchemaRequest(tmp);
- }
- });
+ try {
+ schemaQueriesFactory
+ .newInstance()
+ .execute()
+ .thenApplyAsync(this::parseAndApplySchemaRows,
adminExecutor)
+ .whenComplete(
+ (newMetadata, metadataError) -> {
+ if (metadataError != null) {
+
refreshFuture.completeExceptionally(metadataError);
+ } else {
+ refreshFuture.complete(
+ new
RefreshSchemaResult(newMetadata, schemaInAgreement));
+ }
+
+
firstSchemaRefreshFuture.complete(null);
+
+ currentSchemaRefresh = null;
+ // If another refresh was enqueued
during this one, run it now
+ if (queuedSchemaRefresh != null) {
+
CompletableFuture<RefreshSchemaResult> tmp =
+ this.queuedSchemaRefresh;
+ this.queuedSchemaRefresh = null;
+ startSchemaRequest(tmp);
+ }
+ });
+ } catch (Throwable t) {
Review Comment:
Was considering RuntimeException but ultimately decided Throwable was better
for this use case.
Also, a couple notes here:
- the diff is mostly whitespace since this block is now wrapped with a
try/catch
- the only bits that can throw at this level (vs inside the completion
handlers) are the `newInstance()` and `execute()` calls, but this felt a lot
cleaner than extracting those two bits for the try/catch wrap and then
nullchecking the result before the `thenApplyAsync` and `whenComplete`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]