echauchot commented on code in PR #19680: URL: https://github.com/apache/flink/pull/19680#discussion_r879264371
########## flink-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/batch/connectors/cassandra/CassandraOutputFormatBaseTest.java: ########## @@ -0,0 +1,352 @@ +/* + * 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.ExceptionUtils; +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.Assert; +import org.junit.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.CountDownLatch; +import java.util.concurrent.TimeoutException; +import java.util.function.Function; + +import static org.junit.Assert.assertTrue; +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 long DEFAULT_TEST_TIMEOUT = 5000; + private static final Duration DEFAULT_MAX_CONCURRENT_REQUESTS_TIMEOUT = + Duration.ofMillis(Long.MAX_VALUE); + + @Test(expected = NoHostAvailableException.class) + public void testHostNotFoundErrorHandling() throws Exception { + CassandraOutputFormatBase cassandraOutputFormatBase = + new CassandraOutputFormatBase( + 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 send(Object value) { + return null; + } + }; + cassandraOutputFormatBase.configure(new Configuration()); + cassandraOutputFormatBase.open(1, 1); + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testSuccessfulWrite() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + testCassandraOutputFormat.enqueueCompletableFuture( + CompletableFuture.completedFuture(null)); + + final int originalPermits = testCassandraOutputFormat.getAvailablePermits(); + assertTrue(originalPermits > 0); + Assert.assertEquals(0, testCassandraOutputFormat.getAcquiredPermits()); + + testCassandraOutputFormat.writeRecord("hello"); + + Assert.assertEquals(originalPermits, testCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(0, testCassandraOutputFormat.getAcquiredPermits()); + } + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testThrowErrorOnClose() throws Exception { + TestCassandraOutputFormat testCassandraOutputFormat = createTestCassandraOutputFormat(); + testCassandraOutputFormat.open(1, 1); + + Exception cause = new RuntimeException(); + testCassandraOutputFormat.enqueueCompletableFuture( + FutureUtils.completedExceptionally(cause)); + testCassandraOutputFormat.writeRecord("hello"); + try { + testCassandraOutputFormat.close(); + Assert.fail("Close should have thrown an exception."); + } catch (IOException e) { + ExceptionUtils.findThrowable(e, candidate -> candidate == cause).orElseThrow(() -> e); + } + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testThrowErrorOnWrite() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + Exception cause = new RuntimeException(); + testCassandraOutputFormat.enqueueCompletableFuture( + FutureUtils.completedExceptionally(cause)); + + testCassandraOutputFormat.writeRecord("hello"); + + try { + testCassandraOutputFormat.writeRecord("world"); + // should fail because only one completableFuture was enqueued + Assert.fail("Sending of second value should have failed."); + } catch (IOException e) { + Assert.assertEquals(cause, e.getCause()); + Assert.assertEquals(0, testCassandraOutputFormat.getAcquiredPermits()); + } + } + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testWaitForPendingUpdatesOnClose() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + + CompletableFuture<ResultSet> completableFuture = new CompletableFuture<>(); + testCassandraOutputFormat.enqueueCompletableFuture(completableFuture); + + testCassandraOutputFormat.writeRecord("hello"); + Assert.assertEquals(1, testCassandraOutputFormat.getAcquiredPermits()); + + final CountDownLatch latch = new CountDownLatch(1); + Thread t = + new CheckedThread("Flink-CassandraOutputFormatBaseTest") { + @Override + public void go() throws Exception { + testCassandraOutputFormat.close(); + latch.countDown(); + } + }; + t.start(); + while (t.getState() != Thread.State.TIMED_WAITING) { + Thread.sleep(5); + } + + Assert.assertEquals(1, testCassandraOutputFormat.getAcquiredPermits()); + // start writing + completableFuture.complete(null); + latch.await(); + Assert.assertEquals(0, testCassandraOutputFormat.getAcquiredPermits()); + } + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testReleaseOnSuccess() throws Exception { + try (TestCassandraOutputFormat openedTestCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + Assert.assertEquals(1, openedTestCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(0, openedTestCassandraOutputFormat.getAcquiredPermits()); + + CompletableFuture<ResultSet> completableFuture = new CompletableFuture<>(); + openedTestCassandraOutputFormat.enqueueCompletableFuture(completableFuture); + openedTestCassandraOutputFormat.writeRecord("N/A"); + + Assert.assertEquals(0, openedTestCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(1, openedTestCassandraOutputFormat.getAcquiredPermits()); + + // start writing + completableFuture.complete(null); + + Assert.assertEquals(1, openedTestCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(0, openedTestCassandraOutputFormat.getAcquiredPermits()); + } + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testReleaseOnFailure() throws Exception { + try (TestCassandraOutputFormat testCassandraOutputFormat = + createOpenedTestCassandraOutputFormat()) { + Assert.assertEquals(1, testCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(0, testCassandraOutputFormat.getAcquiredPermits()); + + CompletableFuture<ResultSet> completableFuture = new CompletableFuture<>(); + testCassandraOutputFormat.enqueueCompletableFuture(completableFuture); + testCassandraOutputFormat.writeRecord("N/A"); + + Assert.assertEquals(0, testCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(1, testCassandraOutputFormat.getAcquiredPermits()); + + completableFuture.completeExceptionally(new RuntimeException()); + + Assert.assertEquals(1, testCassandraOutputFormat.getAvailablePermits()); + Assert.assertEquals(0, testCassandraOutputFormat.getAcquiredPermits()); + } catch (IOException ignored) { + // format.close() throws the exception gathered in format.writeRecord() + } + } + + @Test(timeout = DEFAULT_TEST_TIMEOUT) + public void testReleaseOnThrowingSend() throws Exception { + Function<String, ListenableFuture<ResultSet>> failingSendFunction = + ignoredMessage -> { + throwCheckedAsUnchecked(new Throwable("expected")); + //noinspection ReturnOfNull + return null; Review Comment: done -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org