Skip to content

Commit 8cd7975

Browse files
author
Brian Burkhalter
committed
8364761: (aio) AsynchronousChannelGroup.execute doesn't check null command
Reviewed-by: alanb, vyazici
1 parent 958383d commit 8cd7975

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

src/java.base/share/classes/sun/nio/ch/AsynchronousChannelGroupImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
import java.nio.channels.spi.AsynchronousChannelProvider;
3131
import java.io.IOException;
3232
import java.io.FileDescriptor;
33+
import java.util.Objects;
3334
import java.util.Queue;
3435
import java.util.concurrent.*;
3536
import java.util.concurrent.atomic.AtomicInteger;
@@ -299,6 +300,7 @@ public final boolean awaitTermination(long timeout, TimeUnit unit)
299300
*/
300301
@Override
301302
public final void execute(Runnable task) {
303+
Objects.requireNonNull(task, "task");
302304
executeOnPooledThread(task);
303305
}
304306
}
Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,49 +23,75 @@
2323

2424
/*
2525
* @test
26-
* @bug 4607272
26+
* @bug 4607272 8364761
2727
* @summary tests tasks can be submitted to a channel group's thread pool.
28-
* @run main AsExecutor
28+
* @run junit AsExecutor
2929
*/
3030

31+
import java.io.IOException;
3132
import java.nio.channels.AsynchronousChannelGroup;
33+
import java.util.ArrayList;
34+
import java.util.List;
3235
import java.util.concurrent.CountDownLatch;
3336
import java.util.concurrent.Executor;
3437
import java.util.concurrent.Executors;
3538
import java.util.concurrent.ThreadFactory;
39+
import java.util.stream.Stream;
40+
41+
import org.junit.jupiter.api.BeforeAll;
42+
import org.junit.jupiter.params.ParameterizedTest;
43+
import org.junit.jupiter.params.provider.Arguments;
44+
import org.junit.jupiter.params.provider.MethodSource;
45+
46+
import static org.junit.jupiter.api.Assertions.assertThrows;
3647

3748
public class AsExecutor {
49+
private static ThreadFactory factory;
50+
51+
@BeforeAll
52+
public static void createThreadFactory() {
53+
factory = Executors.defaultThreadFactory();
54+
}
3855

39-
public static void main(String[] args) throws Exception {
40-
// create channel groups
41-
ThreadFactory factory = Executors.defaultThreadFactory();
42-
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
43-
.withFixedThreadPool(5, factory);
44-
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
45-
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
46-
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
47-
.withThreadPool(Executors.newFixedThreadPool(10, factory));
56+
private static Stream<Arguments> channelGroups() throws IOException {
57+
List<Arguments> list = new ArrayList<Arguments>();
58+
list.add(Arguments.of(AsynchronousChannelGroup
59+
.withFixedThreadPool(5, factory)));
60+
list.add(Arguments.of(AsynchronousChannelGroup
61+
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0)));
62+
list.add(Arguments.of(AsynchronousChannelGroup
63+
.withThreadPool(Executors.newFixedThreadPool(10, factory))));
64+
return list.stream();
65+
}
4866

67+
@ParameterizedTest
68+
@MethodSource("channelGroups")
69+
public void simpleTask(AsynchronousChannelGroup group)
70+
throws InterruptedException
71+
{
4972
try {
50-
// execute simple tasks
51-
testSimpleTask(group1);
52-
testSimpleTask(group2);
53-
testSimpleTask(group3);
73+
Executor executor = (Executor)group;
74+
final CountDownLatch latch = new CountDownLatch(1);
75+
executor.execute(new Runnable() {
76+
public void run() {
77+
latch.countDown();
78+
}
79+
});
80+
latch.await();
5481
} finally {
55-
group1.shutdown();
56-
group2.shutdown();
57-
group3.shutdown();
82+
group.shutdown();
5883
}
5984
}
6085

61-
static void testSimpleTask(AsynchronousChannelGroup group) throws Exception {
86+
@ParameterizedTest
87+
@MethodSource("channelGroups")
88+
public void nullTask(AsynchronousChannelGroup group) {
6289
Executor executor = (Executor)group;
63-
final CountDownLatch latch = new CountDownLatch(1);
64-
executor.execute(new Runnable() {
65-
public void run() {
66-
latch.countDown();
67-
}
68-
});
69-
latch.await();
90+
try {
91+
assertThrows(NullPointerException.class,
92+
() -> executor.execute(null));
93+
} finally {
94+
group.shutdown();
95+
}
7096
}
7197
}

0 commit comments

Comments
 (0)