/* * Copyright (c) 2018, 2022, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test id=direct-register * @summary Test virtual threads doing blocking I/O on java.net sockets and with * the I/O poller configured to use direct registration * @enablePreview * @library /test/lib * @run testng/othervm -Djdk.useDirectRegister BlockingSocketOps
*/
// delayed read from s2 to EOF
InputStream in = s2.getInputStream(); Thread reader = runAfterParkedAsync(() ->
in.transferTo(OutputStream.nullOutputStream()));
// write should block byte[] ba = newbyte[100*1024]; try (OutputStream out = s1.getOutputStream()) { for (int i = 0; i < 1000; i++) {
out.write(ba);
}
}
// wait for reader to finish
reader.join();
}
});
}
// delayed abrupt close of s2
s2.setSoLinger(true, 0);
runAfterParkedAsync(s2::close);
// read from s1 should block, then throw try { int n = s1.getInputStream().read();
fail("read " + n);
} catch (IOException ioe) { // expected
}
}
});
}
/** * Socket close while virtual thread blocked in read.
*/
@Test publicvoid testSocketReadAsyncClose1() throws Exception {
testSocketReadAsyncClose(0);
}
/** * Socket close while virtual thread blocked in timed read.
*/
@Test publicvoid testSocketReadAsyncClose2() throws Exception {
testSocketReadAsyncClose(0);
}
void testSocketReadAsyncClose(int timeout) throws Exception {
VThreadRunner.run(() -> { try (var connection = new Connection()) {
Socket s = connection.socket1();
// delayed close of s
runAfterParkedAsync(s::close);
// read from s should block, then throw if (timeout > 0) {
s.setSoTimeout(timeout);
} try { int n = s.getInputStream().read();
fail("read " + n);
} catch (SocketException expected) { }
}
});
}
/** * Virtual thread interrupted while blocked in Socket read.
*/
@Test publicvoid testSocketReadInterrupt1() throws Exception {
testSocketReadInterrupt(0);
}
/** * Virtual thread interrupted while blocked in Socket read with timeout
*/
@Test publicvoid testSocketReadInterrupt2() throws Exception {
testSocketReadInterrupt(60_000);
}
void testSocketReadInterrupt(int timeout) throws Exception {
VThreadRunner.run(() -> { try (var connection = new Connection()) {
Socket s = connection.socket1();
// delayed interrupt of current thread Thread thisThread = Thread.currentThread();
runAfterParkedAsync(thisThread::interrupt);
// read from s should block, then throw if (timeout > 0) {
s.setSoTimeout(timeout);
} try { int n = s.getInputStream().read();
fail("read " + n);
} catch (SocketException expected) {
assertTrue(Thread.interrupted());
assertTrue(s.isClosed());
}
}
});
}
/** * Socket close while virtual thread blocked in write.
*/
@Test publicvoid testSocketWriteAsyncClose() throws Exception {
VThreadRunner.run(() -> { try (var connection = new Connection()) {
Socket s = connection.socket1();
// delayedclose of s
runAfterParkedAsync(s::close);
// write to s should block, then throw try { byte[] ba = newbyte[100*1024];
OutputStream out = s.getOutputStream(); for (;;) {
out.write(ba);
}
} catch (SocketException expected) { }
}
});
}
/** * Virtual thread interrupted while blocked in Socket write.
*/
@Test publicvoid testSocketWriteInterrupt() throws Exception {
VThreadRunner.run(() -> { try (var connection = new Connection()) {
Socket s = connection.socket1();
// delayed interrupt of current thread Thread thisThread = Thread.currentThread();
runAfterParkedAsync(thisThread::interrupt);
// write to s should block, then throw try { byte[] ba = newbyte[100*1024];
OutputStream out = s.getOutputStream(); for (;;) {
out.write(ba);
}
} catch (SocketException expected) {
assertTrue(Thread.interrupted());
assertTrue(s.isClosed());
}
}
});
}
/** * Virtual thread reading urgent data when SO_OOBINLINE is enabled.
*/
@Test publicvoid testSocketReadUrgentData() throws Exception {
VThreadRunner.run(() -> { try (var connection = new Connection()) {
Socket s1 = connection.socket1();
Socket s2 = connection.socket2();
// urgent data should be received
runAfterParkedAsync(() -> s2.sendUrgentData('X'));
// read should block, then read the OOB byte
s1.setOOBInline(true); byte[] ba = newbyte[10]; int n = s1.getInputStream().read(ba);
assertTrue(n == 1);
assertTrue(ba[0] == 'X');
// urgent data should not be received
s1.setOOBInline(false);
s1.setSoTimeout(500);
s2.sendUrgentData('X'); try {
s1.getInputStream().read(ba);
fail();
} catch (SocketTimeoutException expected) { }
}
});
}
/** * Runs the given task asynchronously after the current virtual thread has parked. * @return the thread started to run the task
*/ staticThread runAfterParkedAsync(ThrowingRunnable task) { Thread target = Thread.currentThread(); if (!target.isVirtual()) thrownew WrongThreadException(); returnThread.ofPlatform().daemon().start(() -> { try { Thread.State state = target.getState(); while (state != Thread.State.WAITING
&& state != Thread.State.TIMED_WAITING) { Thread.sleep(20);
state = target.getState();
} Thread.sleep(20); // give a bit more time to release carrier
task.run();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.3 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.