8364761: (aio) AsynchronousChannelGroup.execute doesn't check null command

Reviewed-by: alanb, vyazici
This commit is contained in:
Brian Burkhalter 2025-08-11 18:50:39 +00:00
parent 958383d69c
commit 8cd79752c6
2 changed files with 56 additions and 28 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,7 @@ import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.io.IOException;
import java.io.FileDescriptor;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -299,6 +300,7 @@ abstract class AsynchronousChannelGroupImpl
*/
@Override
public final void execute(Runnable task) {
Objects.requireNonNull(task, "task");
executeOnPooledThread(task);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,42 +23,53 @@
/*
* @test
* @bug 4607272
* @bug 4607272 8364761
* @summary tests tasks can be submitted to a channel group's thread pool.
* @run main AsExecutor
* @run junit AsExecutor
*/
import java.io.IOException;
import java.nio.channels.AsynchronousChannelGroup;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class AsExecutor {
private static ThreadFactory factory;
public static void main(String[] args) throws Exception {
// create channel groups
ThreadFactory factory = Executors.defaultThreadFactory();
AsynchronousChannelGroup group1 = AsynchronousChannelGroup
.withFixedThreadPool(5, factory);
AsynchronousChannelGroup group2 = AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
AsynchronousChannelGroup group3 = AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory));
@BeforeAll
public static void createThreadFactory() {
factory = Executors.defaultThreadFactory();
}
private static Stream<Arguments> channelGroups() throws IOException {
List<Arguments> list = new ArrayList<Arguments>();
list.add(Arguments.of(AsynchronousChannelGroup
.withFixedThreadPool(5, factory)));
list.add(Arguments.of(AsynchronousChannelGroup
.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0)));
list.add(Arguments.of(AsynchronousChannelGroup
.withThreadPool(Executors.newFixedThreadPool(10, factory))));
return list.stream();
}
@ParameterizedTest
@MethodSource("channelGroups")
public void simpleTask(AsynchronousChannelGroup group)
throws InterruptedException
{
try {
// execute simple tasks
testSimpleTask(group1);
testSimpleTask(group2);
testSimpleTask(group3);
} finally {
group1.shutdown();
group2.shutdown();
group3.shutdown();
}
}
static void testSimpleTask(AsynchronousChannelGroup group) throws Exception {
Executor executor = (Executor)group;
final CountDownLatch latch = new CountDownLatch(1);
executor.execute(new Runnable() {
@ -67,5 +78,20 @@ public class AsExecutor {
}
});
latch.await();
} finally {
group.shutdown();
}
}
@ParameterizedTest
@MethodSource("channelGroups")
public void nullTask(AsynchronousChannelGroup group) {
Executor executor = (Executor)group;
try {
assertThrows(NullPointerException.class,
() -> executor.execute(null));
} finally {
group.shutdown();
}
}
}