8379154: Refactor Selector TestNG tests to use JUnit

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2026-03-09 18:05:46 +00:00
parent 7b0024adbb
commit 9454f86cfb
5 changed files with 190 additions and 150 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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,7 +23,7 @@
/* @test
* @bug 6350055
* @run testng AtomicUpdates
* @run junit AtomicUpdates
* @summary Unit test for SelectionKey interestOpsOr and interestOpsAnd
*/
@ -37,15 +37,19 @@ import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Test;
import static java.nio.channels.SelectionKey.OP_READ;
import static java.nio.channels.SelectionKey.OP_WRITE;
import static java.nio.channels.SelectionKey.OP_CONNECT;
import static java.nio.channels.SelectionKey.OP_ACCEPT;
import static org.testng.Assert.*;
@Test
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
public class AtomicUpdates {
private SelectionKey keyFor(SocketChannel sc) {
@ -94,74 +98,69 @@ public class AtomicUpdates {
}
private void test(SelectionKey key) {
assertTrue(key.channel() instanceof SocketChannel);
assertInstanceOf(SocketChannel.class, key.channel());
key.interestOps(0);
// 0 -> 0
int previous = key.interestOpsOr(0);
assertTrue(previous == 0);
assertTrue(key.interestOps() == 0);
assertEquals(0, previous);
assertEquals(0, key.interestOps());
// 0 -> OP_CONNECT
previous = key.interestOpsOr(OP_CONNECT);
assertTrue(previous == 0);
assertTrue(key.interestOps() == OP_CONNECT);
assertEquals(0, previous);
assertEquals(OP_CONNECT, key.interestOps());
// OP_CONNECT -> OP_CONNECT
previous = key.interestOpsOr(0);
assertTrue(previous == OP_CONNECT);
assertTrue(key.interestOps() == OP_CONNECT);
assertEquals(OP_CONNECT, previous);
assertEquals(OP_CONNECT, key.interestOps());
// OP_CONNECT -> OP_CONNECT | OP_READ | OP_WRITE
previous = key.interestOpsOr(OP_READ | OP_WRITE);
assertTrue(previous == OP_CONNECT);
assertTrue(key.interestOps() == (OP_CONNECT | OP_READ | OP_WRITE));
assertEquals(OP_CONNECT, previous);
assertEquals(OP_CONNECT | OP_READ | OP_WRITE, key.interestOps());
// OP_CONNECT | OP_READ | OP_WRITE -> OP_CONNECT
previous = key.interestOpsAnd(~(OP_READ | OP_WRITE));
assertTrue(previous == (OP_CONNECT | OP_READ | OP_WRITE));
assertTrue(key.interestOps() == OP_CONNECT);
assertEquals(OP_CONNECT | OP_READ | OP_WRITE, previous);
assertEquals(OP_CONNECT, key.interestOps());
// OP_CONNECT -> 0
previous = key.interestOpsAnd(~OP_CONNECT);
assertTrue(previous == OP_CONNECT);
assertTrue(key.interestOps() == 0);
assertEquals(OP_CONNECT, previous);
assertEquals(0, key.interestOps());
// OP_READ | OP_WRITE -> OP_READ | OP_WRITE
key.interestOps(OP_READ | OP_WRITE);
previous = key.interestOpsAnd(~OP_ACCEPT);
assertTrue(previous == (OP_READ | OP_WRITE));
assertTrue(key.interestOps() == (OP_READ | OP_WRITE));
assertEquals(OP_READ | OP_WRITE, previous);
assertEquals(OP_READ | OP_WRITE, key.interestOps());
// OP_READ | OP_WRITE -> 0
previous = key.interestOpsAnd(0);
assertTrue(previous == (OP_READ | OP_WRITE));
assertTrue(key.interestOps() == 0);
assertEquals(OP_READ | OP_WRITE, previous);
assertEquals(0, key.interestOps());
// 0 -> 0
previous = key.interestOpsAnd(0);
assertTrue(previous == 0);
assertTrue(key.interestOps() == 0);
assertEquals(0, previous);
assertEquals(0, key.interestOps());
try {
key.interestOpsOr(OP_ACCEPT);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) { }
assertThrows(IllegalArgumentException.class,
() -> key.interestOpsOr(OP_ACCEPT));
key.cancel();
try {
key.interestOpsOr(OP_READ);
fail("CancelledKeyException expected");
} catch (CancelledKeyException expected) { }
try {
key.interestOpsAnd(~OP_READ);
fail("CancelledKeyException expected");
} catch (CancelledKeyException expected) { }
assertThrows(CancelledKeyException.class,
() -> key.interestOpsOr(OP_READ));
assertThrows(CancelledKeyException.class,
() -> key.interestOpsAnd(~OP_READ));
}
/**
* Test default implementation of interestOpsOr/interestOpsAnd
*/
@Test
public void testDefaultImplementation() throws Exception {
try (SocketChannel sc = SocketChannel.open()) {
SelectionKey key = keyFor(sc);
@ -172,6 +171,7 @@ public class AtomicUpdates {
/**
* Test the default provider implementation of SelectionKey.
*/
@Test
public void testNioImplementation() throws Exception {
try (SocketChannel sc = SocketChannel.open();
Selector sel = Selector.open()) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -24,12 +24,12 @@
/* @test
* @summary Unit test for Selector.select/selectNow(Consumer)
* @bug 8199433 8208780
* @run testng SelectWithConsumer
* @run junit SelectWithConsumer
*/
/* @test
* @requires (os.family == "windows")
* @run testng/othervm -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider SelectWithConsumer
* @run junit/othervm -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider SelectWithConsumer
*/
import java.io.Closeable;
@ -49,11 +49,16 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static java.util.concurrent.TimeUnit.*;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Test
public class SelectWithConsumer {
/**
@ -81,46 +86,47 @@ public class SelectWithConsumer {
// select(Consumer)
notifiedOps.set(0);
int n = sel.select(k -> {
assertTrue(Thread.currentThread() == callerThread);
assertTrue(k == key);
assertSame(callerThread, Thread.currentThread());
assertSame(key, k);
int readyOps = key.readyOps();
assertTrue((readyOps & interestOps) != 0);
assertTrue((readyOps & notifiedOps.get()) == 0);
assertNotEquals(0, readyOps & interestOps);
assertEquals(0, readyOps & notifiedOps.get());
notifiedOps.set(notifiedOps.get() | readyOps);
});
assertTrue((n == 1) ^ (expectedOps == 0));
assertTrue(notifiedOps.get() == expectedOps);
assertEquals(expectedOps, notifiedOps.get());
// select(Consumer, timeout)
notifiedOps.set(0);
n = sel.select(k -> {
assertTrue(Thread.currentThread() == callerThread);
assertTrue(k == key);
assertSame(callerThread, Thread.currentThread());
assertSame(key, k);
int readyOps = key.readyOps();
assertTrue((readyOps & interestOps) != 0);
assertTrue((readyOps & notifiedOps.get()) == 0);
assertNotEquals(0, readyOps & interestOps);
assertEquals(0, readyOps & notifiedOps.get());
notifiedOps.set(notifiedOps.get() | readyOps);
}, 1000);
assertTrue((n == 1) ^ (expectedOps == 0));
assertTrue(notifiedOps.get() == expectedOps);
assertEquals(expectedOps, notifiedOps.get());
// selectNow(Consumer)
notifiedOps.set(0);
n = sel.selectNow(k -> {
assertTrue(Thread.currentThread() == callerThread);
assertTrue(k == key);
assertSame(callerThread, Thread.currentThread());
assertSame(key, k);
int readyOps = key.readyOps();
assertTrue((readyOps & interestOps) != 0);
assertTrue((readyOps & notifiedOps.get()) == 0);
assertNotEquals(0, readyOps & interestOps);
assertEquals(0, readyOps & notifiedOps.get());
notifiedOps.set(notifiedOps.get() | readyOps);
});
assertTrue((n == 1) ^ (expectedOps == 0));
assertTrue(notifiedOps.get() == expectedOps);
assertEquals(expectedOps, notifiedOps.get());
}
/**
* Test that an action is performed when a channel is ready for reading.
*/
@Test
public void testReadable() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -142,6 +148,7 @@ public class SelectWithConsumer {
/**
* Test that an action is performed when a channel is ready for writing.
*/
@Test
public void testWritable() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -161,6 +168,7 @@ public class SelectWithConsumer {
* Test that an action is performed when a channel is ready for both
* reading and writing.
*/
@Test
public void testReadableAndWriteable() throws Exception {
ServerSocketChannel ssc = null;
SocketChannel sc = null;
@ -188,6 +196,7 @@ public class SelectWithConsumer {
/**
* Test that the action is called for two selected channels
*/
@Test
public void testTwoChannels() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -217,8 +226,8 @@ public class SelectWithConsumer {
assertTrue(k == key1 || k == key2);
counter.incrementAndGet();
});
assertTrue(n == 2);
assertTrue(counter.get() == 2);
assertEquals(2, n);
assertEquals(2, counter.get());
// select(Consumer, timeout)
counter.set(0);
@ -226,8 +235,8 @@ public class SelectWithConsumer {
assertTrue(k == key1 || k == key2);
counter.incrementAndGet();
}, 1000);
assertTrue(n == 2);
assertTrue(counter.get() == 2);
assertEquals(2, n);
assertEquals(2, counter.get());
// selectNow(Consumer)
counter.set(0);
@ -235,8 +244,8 @@ public class SelectWithConsumer {
assertTrue(k == key1 || k == key2);
counter.incrementAndGet();
});
assertTrue(n == 2);
assertTrue(counter.get() == 2);
assertEquals(2, n);
assertEquals(2, counter.get());
} finally {
closePipe(p);
}
@ -245,6 +254,7 @@ public class SelectWithConsumer {
/**
* Test calling select twice, the action should be invoked each time
*/
@Test
public void testRepeatedSelect1() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -269,6 +279,7 @@ public class SelectWithConsumer {
* Test calling select twice. An I/O operation is performed after the
* first select so the channel will not be selected by the second select.
*/
@Test
public void testRepeatedSelect2() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -301,6 +312,7 @@ public class SelectWithConsumer {
/**
* Test timeout
*/
@Test
public void testTimeout() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -311,7 +323,7 @@ public class SelectWithConsumer {
long start = millisTime();
int n = sel.select(k -> assertTrue(false), 1000L);
expectDuration(start, 500, Long.MAX_VALUE);
assertTrue(n == 0);
assertEquals(0, n);
} finally {
closePipe(p);
}
@ -320,12 +332,13 @@ public class SelectWithConsumer {
/**
* Test wakeup prior to select
*/
@Test
public void testWakeupBeforeSelect() throws Exception {
// select(Consumer)
try (Selector sel = Selector.open()) {
sel.wakeup();
int n = sel.select(k -> assertTrue(false));
assertTrue(n == 0);
assertEquals(0, n);
}
// select(Consumer, timeout)
@ -334,19 +347,20 @@ public class SelectWithConsumer {
long start = millisTime();
int n = sel.select(k -> assertTrue(false), 60*1000);
expectDuration(start, 0, 20_000);
assertTrue(n == 0);
assertEquals(0, n);
}
}
/**
* Test wakeup during select
*/
@Test
public void testWakeupDuringSelect() throws Exception {
// select(Consumer)
try (Selector sel = Selector.open()) {
scheduleWakeup(sel, 1, SECONDS);
int n = sel.select(k -> assertTrue(false));
assertTrue(n == 0);
assertEquals(0, n);
}
// select(Consumer, timeout)
@ -355,19 +369,20 @@ public class SelectWithConsumer {
long start = millisTime();
int n = sel.select(k -> assertTrue(false), 60*1000);
expectDuration(start, 0, 20_000);
assertTrue(n == 0);
assertEquals(0, n);
}
}
/**
* Test invoking select with interrupted status set
*/
@Test
public void testInterruptBeforeSelect() throws Exception {
// select(Consumer)
try (Selector sel = Selector.open()) {
Thread.currentThread().interrupt();
int n = sel.select(k -> assertTrue(false));
assertTrue(n == 0);
assertEquals(0, n);
assertTrue(Thread.currentThread().isInterrupted());
assertTrue(sel.isOpen());
} finally {
@ -380,7 +395,7 @@ public class SelectWithConsumer {
long start = millisTime();
int n = sel.select(k -> assertTrue(false), 60*1000);
expectDuration(start, 0, 20_000);
assertTrue(n == 0);
assertEquals(0, n);
assertTrue(Thread.currentThread().isInterrupted());
assertTrue(sel.isOpen());
} finally {
@ -391,12 +406,13 @@ public class SelectWithConsumer {
/**
* Test interrupt thread during select
*/
@Test
public void testInterruptDuringSelect() throws Exception {
// select(Consumer)
try (Selector sel = Selector.open()) {
scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
int n = sel.select(k -> assertTrue(false));
assertTrue(n == 0);
assertEquals(0, n);
assertTrue(Thread.currentThread().isInterrupted());
assertTrue(sel.isOpen());
} finally {
@ -407,7 +423,7 @@ public class SelectWithConsumer {
try (Selector sel = Selector.open()) {
scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
int n = sel.select(k -> assertTrue(false), 60*1000);
assertTrue(n == 0);
assertEquals(0, n);
assertTrue(Thread.currentThread().isInterrupted());
assertTrue(sel.isOpen());
} finally {
@ -418,34 +434,38 @@ public class SelectWithConsumer {
/**
* Test invoking select on a closed selector
*/
@Test(expectedExceptions = ClosedSelectorException.class)
@Test
public void testClosedSelector1() throws Exception {
Selector sel = Selector.open();
sel.close();
sel.select(k -> assertTrue(false));
assertThrows(ClosedSelectorException.class,
() -> sel.select(k -> assertTrue(false)));
}
@Test(expectedExceptions = ClosedSelectorException.class)
@Test
public void testClosedSelector2() throws Exception {
Selector sel = Selector.open();
sel.close();
sel.select(k -> assertTrue(false), 1000);
assertThrows(ClosedSelectorException.class,
() -> sel.select(k -> assertTrue(false), 1000));
}
@Test(expectedExceptions = ClosedSelectorException.class)
@Test
public void testClosedSelector3() throws Exception {
Selector sel = Selector.open();
sel.close();
sel.selectNow(k -> assertTrue(false));
assertThrows(ClosedSelectorException.class,
() -> sel.selectNow(k -> assertTrue(false)));
}
/**
* Test closing selector while in a selection operation
*/
@Test
public void testCloseDuringSelect() throws Exception {
// select(Consumer)
try (Selector sel = Selector.open()) {
scheduleClose(sel, 3, SECONDS);
int n = sel.select(k -> assertTrue(false));
assertTrue(n == 0);
assertEquals(0, n);
assertFalse(sel.isOpen());
}
@ -458,7 +478,7 @@ public class SelectWithConsumer {
long after = System.nanoTime();
long selectDuration = (after - start) / 1000000;
long scheduleDuration = (start - before) / 1000000;
assertTrue(n == 0);
assertEquals(0, n);
assertTrue(selectDuration > 2000 && selectDuration < 10*1000,
"select took " + selectDuration + " ms schedule took " +
scheduleDuration + " ms");
@ -469,7 +489,7 @@ public class SelectWithConsumer {
/**
* Test action closing selector
*/
@Test(expectedExceptions = ClosedSelectorException.class)
@Test
public void testActionClosingSelector() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -482,12 +502,14 @@ public class SelectWithConsumer {
sink.write(messageBuffer());
// should relay ClosedSelectorException
sel.select(k -> {
assertTrue(k == key);
try {
sel.close();
} catch (IOException ioe) { }
});
assertThrows(ClosedSelectorException.class,
() -> sel.select(k -> {
assertTrue(k == key);
try {
sel.close();
} catch (IOException ioe) { }
})
);
} finally {
closePipe(p);
}
@ -497,6 +519,7 @@ public class SelectWithConsumer {
* Test that the action is invoked while synchronized on the selector and
* its selected-key set.
*/
@Test
public void testLocks() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -510,7 +533,7 @@ public class SelectWithConsumer {
// select(Consumer)
sel.select(k -> {
assertTrue(k == key);
assertSame(key, k);
assertTrue(Thread.holdsLock(sel));
assertFalse(Thread.holdsLock(sel.keys()));
assertTrue(Thread.holdsLock(sel.selectedKeys()));
@ -518,7 +541,7 @@ public class SelectWithConsumer {
// select(Consumer, timeout)
sel.select(k -> {
assertTrue(k == key);
assertSame(key, k);
assertTrue(Thread.holdsLock(sel));
assertFalse(Thread.holdsLock(sel.keys()));
assertTrue(Thread.holdsLock(sel.selectedKeys()));
@ -526,7 +549,7 @@ public class SelectWithConsumer {
// selectNow(Consumer)
sel.selectNow(k -> {
assertTrue(k == key);
assertSame(key, k);
assertTrue(Thread.holdsLock(sel));
assertFalse(Thread.holdsLock(sel.keys()));
assertTrue(Thread.holdsLock(sel.selectedKeys()));
@ -540,6 +563,7 @@ public class SelectWithConsumer {
* Test that selection operations remove cancelled keys from the selector's
* key and selected-key sets.
*/
@Test
public void testCancel() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -569,7 +593,7 @@ public class SelectWithConsumer {
// cancel key1
key1.cancel();
int n = sel.selectNow(k -> assertTrue(k == key2));
assertTrue(n == 1);
assertEquals(1, n);
assertFalse(sel.keys().contains(key1));
assertTrue(sel.keys().contains(key2));
assertFalse(sel.selectedKeys().contains(key1));
@ -578,7 +602,7 @@ public class SelectWithConsumer {
// cancel key2
key2.cancel();
n = sel.selectNow(k -> assertTrue(false));
assertTrue(n == 0);
assertEquals(0, n);
assertFalse(sel.keys().contains(key1));
assertFalse(sel.keys().contains(key2));
assertFalse(sel.selectedKeys().contains(key1));
@ -591,6 +615,7 @@ public class SelectWithConsumer {
/**
* Test an action invoking select()
*/
@Test
public void testReentrantSelect1() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -611,7 +636,7 @@ public class SelectWithConsumer {
} catch (IllegalStateException expected) {
}
});
assertTrue(n == 1);
assertEquals(1, n);
} finally {
closePipe(p);
}
@ -620,6 +645,7 @@ public class SelectWithConsumer {
/**
* Test an action invoking selectNow()
*/
@Test
public void testReentrantSelect2() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -640,7 +666,7 @@ public class SelectWithConsumer {
} catch (IllegalStateException expected) {
}
});
assertTrue(n == 1);
assertEquals(1, n);
} finally {
closePipe(p);
}
@ -649,6 +675,7 @@ public class SelectWithConsumer {
/**
* Test an action invoking select(Consumer)
*/
@Test
public void testReentrantSelect3() throws Exception {
Pipe p = Pipe.open();
try (Selector sel = Selector.open()) {
@ -669,7 +696,7 @@ public class SelectWithConsumer {
} catch (IllegalStateException expected) {
}
});
assertTrue(n == 1);
assertEquals(1, n);
} finally {
closePipe(p);
}
@ -678,42 +705,46 @@ public class SelectWithConsumer {
/**
* Negative timeout
*/
@Test(expectedExceptions = IllegalArgumentException.class)
@Test
public void testNegativeTimeout() throws Exception {
try (Selector sel = Selector.open()) {
sel.select(k -> { }, -1L);
assertThrows(IllegalArgumentException.class,
() -> sel.select(k -> { }, -1L));
}
}
/**
* Null action
*/
@Test(expectedExceptions = NullPointerException.class)
@Test
public void testNull1() throws Exception {
try (Selector sel = Selector.open()) {
sel.select(null);
assertThrows(NullPointerException.class,
() -> sel.select(null));
}
}
@Test(expectedExceptions = NullPointerException.class)
@Test
public void testNull2() throws Exception {
try (Selector sel = Selector.open()) {
sel.select(null, 1000);
assertThrows(NullPointerException.class,
() -> sel.select(null, 1000));
}
}
@Test(expectedExceptions = NullPointerException.class)
@Test
public void testNull3() throws Exception {
try (Selector sel = Selector.open()) {
sel.selectNow(null);
assertThrows(NullPointerException.class,
() -> sel.selectNow(null));
}
}
// -- support methods ---
private final ScheduledExecutorService POOL = Executors.newScheduledThreadPool(1);
private static final ScheduledExecutorService POOL = Executors.newScheduledThreadPool(1);
@AfterTest
void shutdownThreadPool() {
@AfterAll
static void shutdownThreadPool() {
POOL.shutdown();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -22,7 +22,7 @@
*/
/* @test
* @run testng UpdateReadyOps
* @run junit UpdateReadyOps
* @summary Test that the ready set from a selection operation is bitwise-disjoined
* into a key's ready set when the key is already in the selected-key set
*/
@ -37,16 +37,19 @@ import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Test
public class UpdateReadyOps {
/**
* Test that OP_WRITE is preserved when updating the ready set of a key in
* the selected-key set to add OP_READ.
*/
@Test
public void testOpWritePreserved() throws Exception {
try (ConnectionPair pair = new ConnectionPair();
Selector sel = Selector.open()) {
@ -58,14 +61,14 @@ public class UpdateReadyOps {
SelectionKey key = sc1.register(sel, SelectionKey.OP_WRITE);
int updated = sel.select();
assertTrue(updated == 1);
assertEquals(1, updated);
assertTrue(sel.selectedKeys().contains(key));
assertFalse(key.isReadable());
assertTrue(key.isWritable());
// select again, should be no updates
updated = sel.select();
assertTrue(updated == 0);
assertEquals(0, updated);
assertTrue(sel.selectedKeys().contains(key));
assertFalse(key.isReadable());
assertTrue(key.isWritable());
@ -78,16 +81,17 @@ public class UpdateReadyOps {
key.interestOps(SelectionKey.OP_READ);
updated = sel.select();
assertTrue(updated == 1);
assertTrue(sel.selectedKeys().size() == 1);
assertEquals(1, updated);
assertEquals(1, sel.selectedKeys().size());
assertTrue(key.isReadable());
assertTrue(key.isWritable());
assertTrue(key.readyOps() == (SelectionKey.OP_READ|SelectionKey.OP_WRITE));
assertEquals(SelectionKey.OP_READ|SelectionKey.OP_WRITE,
key.readyOps());
// select again, should be no updates
updated = sel.select();
assertTrue(updated == 0);
assertTrue(sel.selectedKeys().size() == 1);
assertEquals(0, updated);
assertEquals(1, sel.selectedKeys().size());
assertTrue(key.isReadable());
assertTrue(key.isWritable());
}
@ -97,6 +101,7 @@ public class UpdateReadyOps {
* Test that OP_READ is preserved when updating the ready set of a key in
* the selected-key set to add OP_WRITE.
*/
@Test
public void testOpReadPreserved() throws Exception {
try (ConnectionPair pair = new ConnectionPair();
Selector sel = Selector.open()) {
@ -111,32 +116,32 @@ public class UpdateReadyOps {
sc2.write(helloMessage());
int updated = sel.select();
assertTrue(updated == 1);
assertTrue(sel.selectedKeys().size() == 1);
assertEquals(1, updated);
assertEquals(1, sel.selectedKeys().size());
assertTrue(sel.selectedKeys().contains(key));
assertTrue(key.isReadable());
assertFalse(key.isWritable());
// select again, should be no updates
updated = sel.select();
assertTrue(updated == 0);
assertEquals(0, updated);
assertTrue(sel.selectedKeys().contains(key));
assertTrue(key.isReadable());
assertFalse(key.isWritable());
key.interestOps(SelectionKey.OP_WRITE);
updated = sel.select();
assertTrue(updated == 1);
assertTrue(sel.selectedKeys().size() == 1);
assertEquals(1, updated);
assertEquals(1, sel.selectedKeys().size());
assertTrue(sel.selectedKeys().contains(key));
assertTrue(key.isReadable());
assertTrue(key.isWritable());
assertTrue(key.readyOps() == (SelectionKey.OP_READ|SelectionKey.OP_WRITE));
assertEquals(SelectionKey.OP_READ|SelectionKey.OP_WRITE, key.readyOps());
// select again, should be no updates
updated = sel.select();
assertTrue(updated == 0);
assertTrue(sel.selectedKeys().size() == 1);
assertEquals(0, updated);
assertEquals(1, sel.selectedKeys().size());
assertTrue(key.isReadable());
assertTrue(key.isWritable());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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
@ -25,19 +25,22 @@
* @test
* @bug 8254692
* @summary Basic test for java.nio.channels.spi.SelectorProvider.java default implementation
* @run testng TestDefaultImplementation
* @run junit TestDefaultImplementation
*/
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.ProtocolFamily;
import java.nio.channels.*;
import java.nio.channels.DatagramChannel;
import java.nio.channels.Pipe;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.AbstractSelector;
import java.nio.channels.spi.SelectorProvider;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class TestDefaultImplementation {
static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
@ -67,4 +70,4 @@ public class TestDefaultImplementation {
@Override public ServerSocketChannel openServerSocketChannel() { return null; }
@Override public SocketChannel openSocketChannel() { return null; }
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2026, 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
@ -36,7 +36,7 @@
* UnixSocketTest StateTest StateTestService EchoTest EchoService
* UnixDomainChannelTest CloseTest Launcher Util
* CheckIPv6Test CheckIPv6Service
* @run testng/othervm/native InheritedChannelTest
* @run junit/othervm/native InheritedChannelTest
* @key intermittent
*/
@ -45,14 +45,16 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.Utils;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.Platform;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static java.util.Arrays.asList;
@ -64,19 +66,18 @@ public class InheritedChannelTest {
private static final Path libraryPath
= Paths.get(System.getProperty("java.library.path"));
@DataProvider
public Object[][] testCases() {
return new Object[][] {
{ "UnixDomainChannelTest", List.of(UnixDomainChannelTest.class.getName())},
{ "UnixSocketTest", List.of(UnixSocketTest.class.getName())},
{ "StateTest", List.of(StateTest.class.getName(), "-Dtest.classes="+TEST_CLASSES)},
{ "EchoTest", List.of(EchoTest.class.getName()) },
{ "CheckIPv6Test", List.of(CheckIPv6Test.class.getName()) },
{ "CloseTest", List.of(CloseTest.class.getName()) },
};
public static Stream<Arguments> testCases() {
return Stream.of
(Arguments.of( "UnixDomainChannelTest", List.of(UnixDomainChannelTest.class.getName())),
Arguments.of( "UnixSocketTest", List.of(UnixSocketTest.class.getName())),
Arguments.of( "StateTest", List.of(StateTest.class.getName(), "-Dtest.classes="+TEST_CLASSES)),
Arguments.of( "EchoTest", List.of(EchoTest.class.getName())),
Arguments.of( "CheckIPv6Test", List.of(CheckIPv6Test.class.getName())),
Arguments.of( "CloseTest", List.of(CloseTest.class.getName())));
}
@Test(dataProvider = "testCases")
@ParameterizedTest
@MethodSource("testCases")
public void test(String desc, List<String> opts) throws Throwable {
String pathVar = Platform.sharedLibraryPathVariableName();
System.out.println(pathVar + "=" + libraryPath);