TsReaper commented on a change in pull request #12867:
URL: https://github.com/apache/flink/pull/12867#discussion_r455633715



##########
File path: 
flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/collect/CollectSinkFunctionRandomITCase.java
##########
@@ -0,0 +1,366 @@
+/*
+ * 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.streaming.api.operators.collect;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import 
org.apache.flink.streaming.api.operators.collect.utils.CollectSinkFunctionTestWrapper;
+import org.apache.flink.streaming.api.operators.collect.utils.TestJobClient;
+import org.apache.flink.util.OptionalFailure;
+import org.apache.flink.util.TestLogger;
+import org.apache.flink.util.function.RunnableWithException;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.CompletableFuture;
+
+import static 
org.apache.flink.streaming.api.operators.collect.utils.CollectSinkFunctionTestWrapper.ACCUMULATOR_NAME;
+
+/**
+ * Random IT cases for {@link CollectSinkFunction}.
+ * It will perform random insert, random checkpoint and random restart.
+ */
+public class CollectSinkFunctionRandomITCase extends TestLogger {
+
+       private static final int MAX_RESULTS_PER_BATCH = 3;
+       private static final JobID TEST_JOB_ID = new JobID();
+       private static final OperatorID TEST_OPERATOR_ID = new OperatorID();
+
+       private static final TypeSerializer<Integer> serializer = 
IntSerializer.INSTANCE;
+
+       private CollectSinkFunctionTestWrapper<Integer> functionWrapper;
+       private boolean jobFinished;
+
+       @Test
+       public void testUncheckpointedFunction() throws Exception {
+               // run multiple times for this random test
+               for (int testCount = 30; testCount > 0; testCount--) {
+                       functionWrapper = new 
CollectSinkFunctionTestWrapper<>(serializer, MAX_RESULTS_PER_BATCH * 4);
+                       jobFinished = false;
+
+                       List<Integer> expected = new ArrayList<>();
+                       for (int i = 0; i < 50; i++) {
+                               expected.add(i);
+                       }
+                       Thread feeder = new ThreadWithException(new 
UncheckpointedDataFeeder(expected));
+
+                       List<Integer> actual = runFunctionRandomTest(feeder);
+                       assertResultsEqualAfterSort(expected, actual);
+
+                       functionWrapper.closeWrapper();
+               }
+       }
+
+       @Test
+       public void testCheckpointedFunction() throws Exception {
+               // run multiple times for this random test
+               for (int testCount = 30; testCount > 0; testCount--) {
+                       functionWrapper = new 
CollectSinkFunctionTestWrapper<>(serializer, MAX_RESULTS_PER_BATCH * 4);
+                       jobFinished = false;
+
+                       List<Integer> expected = new ArrayList<>();
+                       for (int i = 0; i < 50; i++) {
+                               expected.add(i);
+                       }
+                       Thread feeder = new ThreadWithException(new 
CheckpointedDataFeeder(expected));
+
+                       List<Integer> actual = runFunctionRandomTest(feeder);
+                       assertResultsEqualAfterSort(expected, actual);
+
+                       functionWrapper.closeWrapper();
+               }
+       }
+
+       private List<Integer> runFunctionRandomTest(Thread feeder) throws 
Exception {
+               CollectClient collectClient = new CollectClient();
+               Thread client = new ThreadWithException(collectClient);
+
+               Thread.UncaughtExceptionHandler exceptionHandler = (t, e) -> {
+                       feeder.interrupt();
+                       client.interrupt();
+                       e.printStackTrace();
+               };
+               feeder.setUncaughtExceptionHandler(exceptionHandler);
+               client.setUncaughtExceptionHandler(exceptionHandler);
+
+               feeder.start();
+               client.start();
+               feeder.join();
+               client.join();
+
+               return collectClient.results;
+       }
+
+       private void assertResultsEqualAfterSort(List<Integer> expected, 
List<Integer> actual) {
+               Collections.sort(expected);
+               Collections.sort(actual);
+               Assert.assertThat(actual, CoreMatchers.is(expected));
+       }
+
+       /**
+        * A {@link RunnableWithException} feeding data to the function. It 
will fail when half of the data is fed.
+        */
+       private class UncheckpointedDataFeeder implements RunnableWithException 
{
+
+               private LinkedList<Integer> data;
+               private final List<Integer> checkpointedData;

Review comment:
       `originalData` might be the proper name.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to