Jiabao-Sun commented on code in PR #23955:
URL: https://github.com/apache/flink/pull/23955#discussion_r1431064368


##########
flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/collect/CollectSinkFunctionTest.java:
##########
@@ -129,18 +129,19 @@ void testInvalidVersion() throws Exception {
 
     @Test
     void testConfiguredPortIsUsed() throws Exception {
-        int bindingPort = 50500;
-        functionWrapper
-                .getRuntimeContext()
-                .getTaskManagerRuntimeInfo()
-                .getConfiguration()
-                .setInteger(TaskManagerOptions.COLLECT_PORT, bindingPort);
-        functionWrapper.openFunction();
-        assertThatThrownBy(() -> new ServerSocket(bindingPort))
-                .satisfies(
-                        FlinkAssertions.anyCauseMatches(
-                                BindException.class, "Address already in use 
(Bind failed)"));
-        functionWrapper.closeFunctionNormally();
+        try (ServerSocket socket = new ServerSocket(0)) {
+            functionWrapper
+                    .getRuntimeContext()
+                    .getTaskManagerRuntimeInfo()
+                    .getConfiguration()
+                    .setInteger(TaskManagerOptions.COLLECT_PORT, 
socket.getLocalPort());
+            assertThatThrownBy(() -> functionWrapper.openFunction())
+                    .satisfies(
+                            FlinkAssertions.anyCauseMatches(
+                                    BindException.class, "Address already in 
use (Bind failed)"));
+        } finally {
+            functionWrapper.closeWrapper();

Review Comment:
   `functionWrapper.closeFunctionNormally()` will cause NullPointerException 
because the `BindException` throws in `ServerThread`'s constructor method. 
Maybe we don't need the finally block?
   
   ```
   java.lang.NullPointerException
        at 
org.apache.flink.streaming.api.operators.collect.CollectSinkFunction$ServerThread.access$200(CollectSinkFunction.java:362)
        at 
org.apache.flink.streaming.api.operators.collect.CollectSinkFunction.close(CollectSinkFunction.java:299)
        at 
org.apache.flink.streaming.api.operators.collect.utils.CollectSinkFunctionTestWrapper.closeFunctionNormally(CollectSinkFunctionTestWrapper.java:134)
        at 
org.apache.flink.streaming.api.operators.collect.CollectSinkFunctionTest.testConfiguredPortIsUsed(CollectSinkFunctionTest.java:143)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189)
        at 
java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java)
        at 
java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1067)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1703)
        at 
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
   ```
   
   
https://github.com/apache/flink/blob/4cc24c1dd17b0abe3c4372652c7ab88fedc7e478/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/collect/CollectSinkFunction.java#L298-L301
   



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

Reply via email to