yunfengzhou-hub commented on code in PR #23927:
URL: https://github.com/apache/flink/pull/23927#discussion_r1433793487


##########
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/UnionResultSubpartitionViewTest.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.runtime.io.network.partition;
+
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.util.TestBufferFactory;
+
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for {@link UnionResultSubpartitionView}. */
+public class UnionResultSubpartitionViewTest {
+
+    private UnionResultSubpartitionView view;
+
+    private List<Buffer> buffers0;
+
+    private ResultSubpartitionView view0;
+
+    private List<Buffer> buffers1;
+
+    private ResultSubpartitionView view1;
+
+    @BeforeEach
+    void before() {
+        view = new UnionResultSubpartitionView((ResultSubpartitionView x) -> 
{});
+
+        buffers0 =
+                Arrays.asList(
+                        TestBufferFactory.createBuffer(10),
+                        TestBufferFactory.createBuffer(10),
+                        TestBufferFactory.createBuffer(10, 
Buffer.DataType.EVENT_BUFFER));
+        view0 = new TestingResultSubpartitionView(view, buffers0);
+        view.notifyViewCreated(0, view0);
+
+        buffers1 =
+                Arrays.asList(
+                        TestBufferFactory.createBuffer(10),
+                        TestBufferFactory.createBuffer(10),
+                        TestBufferFactory.createBuffer(10, 
Buffer.DataType.EVENT_BUFFER));
+        view1 = new TestingResultSubpartitionView(view, buffers1);
+        view.notifyViewCreated(1, view1);
+    }
+
+    @Test
+    void testGetNextBuffer() throws IOException {
+        assertThat(view.getNextBuffer()).isNull();
+        view0.notifyDataAvailable();
+        ResultSubpartition.BufferAndBacklog bufferAndBacklog = 
view.getNextBuffer();
+        assertThat(bufferAndBacklog.buffer()).isEqualTo(buffers0.get(0));
+        
assertThat(bufferAndBacklog.buffersInBacklog()).isEqualTo(buffers0.size() - 1);
+
+        view1.notifyDataAvailable();
+        assertThat(view.getNextBuffer().buffer()).isEqualTo(buffers0.get(1));
+
+        List<Buffer> buffers = new ArrayList<>();
+        while (view.getAvailabilityAndBacklog(true).isAvailable()) {
+            buffers.add(view.getNextBuffer().buffer());
+        }
+        assertThat(buffers)
+                .hasSize(buffers0.size() + buffers1.size() - 2)
+                .containsSubsequence(buffers0.subList(2, buffers0.size()))
+                .containsSubsequence(buffers1);
+    }
+
+    @Test
+    void testGetAvailabilityAndBacklog() throws IOException {
+        view0.notifyDataAvailable();
+        view1.notifyDataAvailable();
+
+        ResultSubpartitionView.AvailabilityWithBacklog availabilityAndBacklog1 
=
+                view.getAvailabilityAndBacklog(false);
+        assertThat(availabilityAndBacklog1.getBacklog()).isPositive();
+        assertThat(availabilityAndBacklog1.isAvailable()).isEqualTo(false);
+        ResultSubpartitionView.AvailabilityWithBacklog availabilityAndBacklog2 
=
+                view.getAvailabilityAndBacklog(true);
+        assertThat(availabilityAndBacklog2.getBacklog()).isPositive();
+        assertThat(availabilityAndBacklog2.isAvailable()).isEqualTo(true);
+
+        for (int i = 1; i < buffers0.size() + buffers1.size(); i++) {
+            view.getNextBuffer();
+        }
+
+        ResultSubpartitionView.AvailabilityWithBacklog availabilityAndBacklog3 
=
+                view.getAvailabilityAndBacklog(false);
+        assertThat(availabilityAndBacklog3.getBacklog()).isEqualTo(0);
+        assertThat(availabilityAndBacklog3.isAvailable()).isEqualTo(true);
+        ResultSubpartitionView.AvailabilityWithBacklog availabilityAndBacklog4 
=
+                view.getAvailabilityAndBacklog(true);
+        assertThat(availabilityAndBacklog4.getBacklog()).isEqualTo(0);
+        assertThat(availabilityAndBacklog4.isAvailable()).isEqualTo(true);
+    }
+
+    @Test
+    void testReleaseAllResources() throws IOException {
+        assertThat(view.isReleased()).isFalse();
+        assertThat(view0.isReleased()).isFalse();
+        assertThat(view1.isReleased()).isFalse();
+
+        view.releaseAllResources();
+
+        assertThat(view.isReleased()).isTrue();
+        assertThat(view0.isReleased()).isTrue();
+        assertThat(view1.isReleased()).isTrue();
+    }
+
+    private static class TestingResultSubpartitionView extends 
NoOpResultSubpartitionView {

Review Comment:
   A more common practice is to create a subclass of 
`NoOpResultSubpartitionView`, like `DefaultBufferResultSubpartitionView`, 
`EmptyAlwaysAvailableResultSubpartitionView`, and 
`NextIsEventResultSubpartitionView`. We are not sure what other tests need in a 
`ResultSubpartitionView`, so it might be hard to determine what the new 
`TestingResultSubpartitionView` class should look like.



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