echauchot commented on code in PR #19680: URL: https://github.com/apache/flink/pull/19680#discussion_r886525871
########## flink-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/batch/connectors/cassandra/CassandraOutputFormatBaseTest.java: ########## @@ -0,0 +1,334 @@ +/* + * 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.flink.batch.connectors.cassandra; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connectors.cassandra.utils.ResultSetFutures; +import org.apache.flink.core.testutils.CheckedThread; +import org.apache.flink.streaming.connectors.cassandra.CassandraSinkBase; +import org.apache.flink.streaming.connectors.cassandra.ClusterBuilder; +import org.apache.flink.util.Preconditions; +import org.apache.flink.util.concurrent.FutureUtils; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.exceptions.NoHostAvailableException; +import com.google.common.util.concurrent.ListenableFuture; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.Duration; +import java.util.LinkedList; +import java.util.Queue; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeoutException; +import java.util.function.Function; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +/** Tests for the {@link CassandraSinkBase}. */ +public class CassandraOutputFormatBaseTest { + + private static final Duration DEFAULT_MAX_CONCURRENT_REQUESTS_TIMEOUT = + Duration.ofMillis(Long.MAX_VALUE); + + @Test + public void testHostNotFoundErrorHandling() { + CassandraOutputFormatBase<Object, Void> cassandraOutputFormatBase = + new CassandraOutputFormatBase<Object, Void>( + new ClusterBuilder() { + @Override + protected Cluster buildCluster(Cluster.Builder builder) { + return builder.addContactPoint("127.0.0.1") + .withoutJMXReporting() + .withoutMetrics() + .build(); + } + }, + Integer.MAX_VALUE, + DEFAULT_MAX_CONCURRENT_REQUESTS_TIMEOUT) { + @Override + public ListenableFuture<Void> send(Object value) { + return null; + } + }; + cassandraOutputFormatBase.configure(new Configuration()); + assertThatThrownBy(() -> cassandraOutputFormatBase.open(1, 1)) + .isInstanceOf(NoHostAvailableException.class); + } + + @Test + public void testSuccessfulWrite() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + testCassandraOutputFormat.enqueueCompletableFuture( + CompletableFuture.completedFuture(null)); + + final int originalPermits = testCassandraOutputFormat.getAvailablePermits(); + assertThat(originalPermits).isGreaterThan(0); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + + testCassandraOutputFormat.writeRecord("hello"); + + assertThat(testCassandraOutputFormat.getAvailablePermits()).isEqualTo(originalPermits); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + } + } + + @Test + public void testThrowErrorOnClose() throws Exception { + TestCassandraOutputFormat testCassandraOutputFormat = createTestCassandraOutputFormat(); + testCassandraOutputFormat.open(1, 1); + + Exception cause = new RuntimeException(); + testCassandraOutputFormat.enqueueCompletableFuture( + FutureUtils.completedExceptionally(cause)); + testCassandraOutputFormat.writeRecord("hello"); + + assertThatThrownBy(() -> testCassandraOutputFormat.close()) + .isInstanceOf(IOException.class) + .hasCauseReference(cause); + } + + @Test + public void testThrowErrorOnWrite() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + Exception cause = new RuntimeException(); + testCassandraOutputFormat.enqueueCompletableFuture( + FutureUtils.completedExceptionally(cause)); + + testCassandraOutputFormat.writeRecord("hello"); + + // should fail because the first write failed and the second will check for asynchronous + // errors (throwable set by the async callback) + assertThatThrownBy( + () -> testCassandraOutputFormat.writeRecord("world"), + "Sending of second value should have failed.") + .isInstanceOf(IOException.class) + .hasCauseReference(cause); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + } + } + + @Test + public void testWaitForPendingUpdatesOnClose() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + + CompletableFuture<ResultSet> completableFuture = new CompletableFuture<>(); + testCassandraOutputFormat.enqueueCompletableFuture(completableFuture); + + testCassandraOutputFormat.writeRecord("hello"); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(1); + + CheckedThread checkedThread = + new CheckedThread("Flink-CassandraOutputFormatBaseTest") { + @Override + public void go() throws Exception { + testCassandraOutputFormat.close(); + } + }; + checkedThread.start(); + while (checkedThread.getState() != Thread.State.TIMED_WAITING) { + Thread.sleep(5); + } + + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(1); + // start writing + completableFuture.complete(null); + checkedThread.sync(); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + } + } + + @Test + public void testReleaseOnSuccess() throws Exception { + try (TestCassandraOutputFormat openedTestCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + assertThat(openedTestCassandraOutputFormat.getAvailablePermits()).isEqualTo(1); + assertThat(openedTestCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + + CompletableFuture<ResultSet> completableFuture = new CompletableFuture<>(); + openedTestCassandraOutputFormat.enqueueCompletableFuture(completableFuture); + openedTestCassandraOutputFormat.writeRecord("N/A"); + + assertThat(openedTestCassandraOutputFormat.getAvailablePermits()).isEqualTo(0); + assertThat(openedTestCassandraOutputFormat.getAcquiredPermits()).isEqualTo(1); + + // start writing + completableFuture.complete(null); + + assertThat(openedTestCassandraOutputFormat.getAvailablePermits()).isEqualTo(1); + assertThat(openedTestCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + } + } + + @Test + public void testReleaseOnFailure() throws Exception { + TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat(); + assertThat(testCassandraOutputFormat.getAvailablePermits()).isEqualTo(1); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + + CompletableFuture<ResultSet> completableFuture = new CompletableFuture<>(); + testCassandraOutputFormat.enqueueCompletableFuture(completableFuture); + testCassandraOutputFormat.writeRecord("N/A"); + + assertThat(testCassandraOutputFormat.getAvailablePermits()).isEqualTo(0); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(1); + + completableFuture.completeExceptionally(new RuntimeException()); + + assertThat(testCassandraOutputFormat.getAvailablePermits()).isEqualTo(1); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + try { + testCassandraOutputFormat.close(); + } catch (IOException ignored) { + // the aim is not to assert on the exception in this test + } + } + + @Test + public void testReleaseOnThrowingSend() throws Exception { + Function<String, ListenableFuture<ResultSet>> failingSendFunction = + ignoredMessage -> { + throw new RuntimeException("expected"); + }; + + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedMockOutputFormat(failingSendFunction)) { + assertThat(testCassandraOutputFormat.getAvailablePermits()).isEqualTo(1); + assertThat(testCassandraOutputFormat.getAcquiredPermits()).isEqualTo(0); + + assertThatThrownBy(() -> testCassandraOutputFormat.writeRecord("none")); Review Comment: added a comment in code and an ignored catch. -- 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]
