JeetKunDoug commented on code in PR #74: URL: https://github.com/apache/cassandra-sidecar/pull/74#discussion_r1414749206
########## src/test/integration/org/apache/cassandra/sidecar/common/CQLSessionProviderTest.java: ########## @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.sidecar.common; + +import java.util.concurrent.TimeUnit; + +import com.google.common.util.concurrent.Uninterruptibles; +import org.junit.jupiter.api.extension.ExtendWith; + +import io.vertx.core.Promise; +import io.vertx.ext.web.client.HttpRequest; +import io.vertx.ext.web.client.HttpResponse; +import io.vertx.ext.web.client.WebClient; +import io.vertx.ext.web.codec.BodyCodec; +import io.vertx.junit5.VertxExtension; +import io.vertx.junit5.VertxTestContext; +import org.apache.cassandra.distributed.UpgradeableCluster; +import org.apache.cassandra.distributed.shared.ClusterUtils; +import org.apache.cassandra.sidecar.testing.IntegrationTestBase; +import org.apache.cassandra.testing.CassandraIntegrationTest; +import org.apache.cassandra.testing.CassandraTestContext; + +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test CQLSessionProvider in a variety of cluster states + */ +@ExtendWith(VertxExtension.class) +public class CQLSessionProviderTest extends IntegrationTestBase +{ + + public static final String OK_KEYSPACE_RESPONSE_START = "{\"schema\":\"CREATE KEYSPACE "; + public static final String KEYSPACE_FAILED_RESPONSE_START = "{\"status\":\"Service Unavailable\","; + + @CassandraIntegrationTest(nodesPerDc = 2, startCluster = false) + void testCqlSessionProviderWorksAsExpected(VertxTestContext context, CassandraTestContext cassandraTestContext) + throws Exception + { + UpgradeableCluster cluster = cassandraTestContext.getCluster(); + testWithClient(context, false, webClient -> { + + // To start, both instances are stopped, so we should get 503s for both + buildInstanceHealthRequest(webClient, "1") + .send() + .onSuccess(response -> assertHealthCheckFailed(response, context)) + .compose(_ignored -> + buildInstanceHealthRequest(webClient, "2") + .send() + .onSuccess(response -> assertHealthCheckFailed(response, context))) + .compose(_ignored -> + buildKeyspaceRequest(webClient) + .send() + // With no instances available in the cluster, keyspace requests should fail + .onSuccess(response -> assertKeyspaceFailed(response, context))) + .compose(_ignored -> { + // Start instance 1 and check both again + Promise<Void> promise = Promise.promise(); + new Thread(() -> { + cluster.get(1).startup(); + // Instance 1 should now be up - wait for reconnect before testing + Uninterruptibles.sleepUninterruptibly(5000, TimeUnit.MILLISECONDS); + promise.complete(); + } + ).start(); + return promise.future(); + }) + .compose(_ignored -> + buildInstanceHealthRequest(webClient, "1") + .send() + .onSuccess(response -> assertHealthCheckOk(response, context))) + .compose(_ignored -> + buildInstanceHealthRequest(webClient, "2") + .send() + .onSuccess(response -> assertHealthCheckFailed(response, context)) + ) + .compose(_ignored -> + // Even with only 1 instance connected/up, we should still have keyspace metadata + buildKeyspaceRequest(webClient) + .send() + .onSuccess(response -> assertKeyspaceOk(response, context))) + .compose(_ignored -> { + // Start instance 2 and check both again + Promise<Void> promise = Promise.promise(); + new Thread(() -> { + cluster.get(2).startup(); + ClusterUtils.assertInRing(cluster.get(1), cluster.get(2)); + // Instance 2 should now be up - wait for gossip + // and reconnect before testing + Uninterruptibles.sleepUninterruptibly(5000, TimeUnit.MILLISECONDS); + promise.complete(); + } + ).start(); + return promise.future(); + }) + .compose(_ignored -> + buildInstanceHealthRequest(webClient, "1") + .send() + .onSuccess(response -> assertHealthCheckOk(response, context))) + .compose(_ignored -> + buildInstanceHealthRequest(webClient, "2") + .send() + .onSuccess(response -> assertHealthCheckOk(response, context)) + ) + .onSuccess(_ignored -> context.completeNow()) + .onFailure(context::failNow); + } + ); + } + + Review Comment: Unfortunately the indentation is what the code style does - I can de-dent it, but if you reformat the file or touch the curly-brace at the bottom it's just going to reformat back to this indentation. `Future.future` just doesn't seem to do anything - it looks cool, but doesn't actually _run_ in the test, so it fails if you replace the thread with `Future.future(...)` instead. -- 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]

