| 
1 | 1 | /*  | 
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.  | 
3 | 3 |  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.  | 
4 | 4 |  *  | 
5 | 5 |  * This code is free software; you can redistribute it and/or modify it  | 
 | 
23 | 23 | 
 
  | 
24 | 24 | /*  | 
25 | 25 |  * @test  | 
26 |  | - * @bug 4607272  | 
 | 26 | + * @bug 4607272 8364761  | 
27 | 27 |  * @summary tests tasks can be submitted to a channel group's thread pool.  | 
28 |  | - * @run main AsExecutor  | 
 | 28 | + * @run junit AsExecutor  | 
29 | 29 |  */  | 
30 | 30 | 
 
  | 
 | 31 | +import java.io.IOException;  | 
31 | 32 | import java.nio.channels.AsynchronousChannelGroup;  | 
 | 33 | +import java.util.ArrayList;  | 
 | 34 | +import java.util.List;  | 
32 | 35 | import java.util.concurrent.CountDownLatch;  | 
33 | 36 | import java.util.concurrent.Executor;  | 
34 | 37 | import java.util.concurrent.Executors;  | 
35 | 38 | 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;  | 
36 | 47 | 
 
  | 
37 | 48 | public class AsExecutor {  | 
 | 49 | +    private static ThreadFactory factory;  | 
 | 50 | + | 
 | 51 | +    @BeforeAll  | 
 | 52 | +    public static void createThreadFactory() {  | 
 | 53 | +         factory = Executors.defaultThreadFactory();  | 
 | 54 | +    }  | 
38 | 55 | 
 
  | 
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 | +    }  | 
48 | 66 | 
 
  | 
 | 67 | +    @ParameterizedTest  | 
 | 68 | +    @MethodSource("channelGroups")  | 
 | 69 | +    public void simpleTask(AsynchronousChannelGroup group)  | 
 | 70 | +        throws InterruptedException  | 
 | 71 | +    {  | 
49 | 72 |         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();  | 
54 | 81 |         } finally {  | 
55 |  | -            group1.shutdown();  | 
56 |  | -            group2.shutdown();  | 
57 |  | -            group3.shutdown();  | 
 | 82 | +            group.shutdown();  | 
58 | 83 |         }  | 
59 | 84 |     }  | 
60 | 85 | 
 
  | 
61 |  | -    static void testSimpleTask(AsynchronousChannelGroup group) throws Exception {  | 
 | 86 | +    @ParameterizedTest  | 
 | 87 | +    @MethodSource("channelGroups")  | 
 | 88 | +    public void nullTask(AsynchronousChannelGroup group) {  | 
62 | 89 |         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 | +        }  | 
70 | 96 |     }  | 
71 | 97 | }  | 
0 commit comments