vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614911032


##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import 
org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+    private final List<Long> destination = new ArrayList<>();
+
+    @Test
+    void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws 
Exception {
+        TestSinkInitContextAnyThreadMailbox context = new 
TestSinkInitContextAnyThreadMailbox();
+        TestProcessingTimeService tpts = 
context.getTestProcessingTimeService();
+        TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+        tpts.setCurrentTime(0);
+        writer.write("1", null);
+        tpts.setCurrentTime(10);
+        writer.deliverMessage();
+        tpts.setCurrentTime(120);
+        writer.flush(false);
+        assertThat(destination).containsExactly(1L);
+    }
+
+    @Test
+    void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+        TestSinkInitContextAnyThreadMailbox context = new 
TestSinkInitContextAnyThreadMailbox();
+        TestProcessingTimeService tpts = 
context.getTestProcessingTimeService();
+        TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+        tpts.setCurrentTime(0);
+        writer.write("1", null);
+        // element should be requeued back after timeout
+        tpts.setCurrentTime(110);
+        // deliver initial message after timeout
+        writer.deliverMessage();
+        // flush outstanding mailbox messages containing resubmission of the 
element
+        writer.flush(false);
+        assertThat(destination).containsExactly(1L, 1L);
+    }
+
+    @Test
+    void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+        TestSinkInitContextAnyThreadMailbox context = new 
TestSinkInitContextAnyThreadMailbox();
+        TestProcessingTimeService tpts = 
context.getTestProcessingTimeService();
+        TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+        tpts.setCurrentTime(0);
+        writer.write("1", null);
+        tpts.setCurrentTime(110);
+        writer.deliverMessage();
+        assertThatExceptionOfType(FlinkRuntimeException.class)
+                .isThrownBy(() -> writer.flush(false))
+                .withCauseInstanceOf(TimeoutException.class)
+                .havingCause()
+                .withMessage("Request timed out after 100ms with failOnTimeout 
set to true.");
+    }
+
+    @Test
+    void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+        TestSinkInitContextAnyThreadMailbox context = new 
TestSinkInitContextAnyThreadMailbox();
+        TestProcessingTimeService tpts = 
context.getTestProcessingTimeService();
+        TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+        writer.setShouldFailRequest(true);
+        tpts.setCurrentTime(0);
+        writer.write("1", null);
+        tpts.setCurrentTime(110);
+        // deliver initial message after timeout, message request would fail 
but elements should be
+        // discarded as timeout already occurred
+        writer.deliverMessage();
+        // flush outstanding mailbox messages to resubmit timed out elements
+        writer.flush(false);
+        assertThat(destination).containsExactly(1L);
+    }
+
+    @Test
+    void writerShouldFailOnFatalError() throws Exception {
+        TestSinkInitContextAnyThreadMailbox context = new 
TestSinkInitContextAnyThreadMailbox();
+        TestProcessingTimeService tpts = 
context.getTestProcessingTimeService();
+        TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+        tpts.setCurrentTime(0);
+        writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+        writer.write("1", null);
+        writer.deliverMessage();
+        assertThatExceptionOfType(FlinkRuntimeException.class)
+                .isThrownBy(() -> writer.flush(false))
+                .withMessage("Fatal error");
+    }
+
+    /** Test that the writer can handle a timeout. */
+    private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+        private Exception fatalError;
+        private final CountDownLatch completionLatch;
+        private Thread submitThread;
+
+        private boolean shouldFailRequest = false;

Review Comment:
   It is only used in scope of tests, we also guarantee no concurrent 
modifications since it is set by main test thread before latch is triggered to 
execute the request. I am happy to change but seems to detailed for a mock 
replacement class



-- 
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

Reply via email to