< prev index next >

test/jdk/java/foreign/channels/TestSocketChannels.java

Print this page

 23 
 24 /*
 25  * @test
 26  * @library /test/lib
 27  * @modules java.base/sun.nio.ch
 28  *          jdk.incubator.foreign/jdk.internal.foreign
 29  * @key randomness
 30  * @run testng/othervm TestSocketChannels
 31  */
 32 
 33 import java.net.InetAddress;
 34 import java.net.InetSocketAddress;
 35 import java.nio.ByteBuffer;
 36 import java.nio.channels.ServerSocketChannel;
 37 import java.nio.channels.SocketChannel;
 38 import java.util.Arrays;
 39 import java.util.List;
 40 import java.util.concurrent.atomic.AtomicReference;
 41 import java.util.function.Supplier;
 42 import java.util.stream.Stream;
 43 import jdk.incubator.foreign.MemoryAccess;
 44 import jdk.incubator.foreign.MemorySegment;
 45 import jdk.incubator.foreign.ResourceScope;
 46 import org.testng.annotations.*;


 47 import static org.testng.Assert.*;
 48 
 49 /**
 50  * Tests consisting of buffer views with synchronous NIO network channels.
 51  */
 52 public class TestSocketChannels extends AbstractChannelsTest {
 53 
 54     static final Class<IllegalStateException> ISE = IllegalStateException.class;
 55 
 56     @Test(dataProvider = "closeableScopes")
 57     public void testBasicIOWithClosedSegment(Supplier<ResourceScope> scopeSupplier)
 58         throws Exception
 59     {
 60         try (var channel = SocketChannel.open();
 61              var server = ServerSocketChannel.open();
 62              var connectedChannel = connectChannels(server, channel)) {
 63             ResourceScope scope = scopeSupplier.get();
 64             ByteBuffer bb = segmentBufferOfSize(scope, 16);
 65             scope.close();
 66             assertMessage(expectThrows(ISE, () -> channel.read(bb)),                           "Already closed");

 84             scope.close();
 85             assertMessage(expectThrows(ISE, () -> channel.write(buffers)),       "Already closed");
 86             assertMessage(expectThrows(ISE, () -> channel.read(buffers)),        "Already closed");
 87             assertMessage(expectThrows(ISE, () -> channel.write(buffers, 0 ,8)), "Already closed");
 88             assertMessage(expectThrows(ISE, () -> channel.read(buffers, 0, 8)),  "Already closed");
 89         }
 90     }
 91 
 92     @Test(dataProvider = "allScopes")
 93     public void testBasicIO(Supplier<ResourceScope> scopeSupplier)
 94         throws Exception
 95     {
 96         ResourceScope scope;
 97         try (var sc1 = SocketChannel.open();
 98              var ssc = ServerSocketChannel.open();
 99              var sc2 = connectChannels(ssc, sc1);
100              var scp = closeableScopeOrNull(scope = scopeSupplier.get())) {
101             MemorySegment segment1 = MemorySegment.allocateNative(10, 1, scope);
102             MemorySegment segment2 = MemorySegment.allocateNative(10, 1, scope);
103             for (int i = 0; i < 10; i++) {
104                 MemoryAccess.setByteAtOffset(segment1, i, (byte) i);
105             }
106             ByteBuffer bb1 = segment1.asByteBuffer();
107             ByteBuffer bb2 = segment2.asByteBuffer();
108             assertEquals(sc1.write(bb1), 10);
109             assertEquals(sc2.read(bb2), 10);
110             assertEquals(bb2.flip(), ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
111         }
112     }
113 
114     @Test
115     public void testBasicHeapIOWithGlobalScope() throws Exception {
116         try (var sc1 = SocketChannel.open();
117              var ssc = ServerSocketChannel.open();
118              var sc2 = connectChannels(ssc, sc1)) {
119             var segment1 = MemorySegment.ofArray(new byte[10]);
120             var segment2 = MemorySegment.ofArray(new byte[10]);
121             for (int i = 0; i < 10; i++) {
122                 MemoryAccess.setByteAtOffset(segment1, i, (byte) i);
123             }
124             ByteBuffer bb1 = segment1.asByteBuffer();
125             ByteBuffer bb2 = segment2.asByteBuffer();
126             assertEquals(sc1.write(bb1), 10);
127             assertEquals(sc2.read(bb2), 10);
128             assertEquals(bb2.flip(), ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
129         }
130     }
131 
132     @Test(dataProvider = "confinedScopes")
133     public void testIOOnConfinedFromAnotherThread(Supplier<ResourceScope> scopeSupplier)
134         throws Exception
135     {
136         try (var channel = SocketChannel.open();
137              var server = ServerSocketChannel.open();
138              var connected = connectChannels(server, channel);
139              var scope = scopeSupplier.get()) {
140             var segment = MemorySegment.allocateNative(10, 1, scope);
141             ByteBuffer bb = segment.asByteBuffer();
142             List<ThrowingRunnable> ioOps = List.of(

 23 
 24 /*
 25  * @test
 26  * @library /test/lib
 27  * @modules java.base/sun.nio.ch
 28  *          jdk.incubator.foreign/jdk.internal.foreign
 29  * @key randomness
 30  * @run testng/othervm TestSocketChannels
 31  */
 32 
 33 import java.net.InetAddress;
 34 import java.net.InetSocketAddress;
 35 import java.nio.ByteBuffer;
 36 import java.nio.channels.ServerSocketChannel;
 37 import java.nio.channels.SocketChannel;
 38 import java.util.Arrays;
 39 import java.util.List;
 40 import java.util.concurrent.atomic.AtomicReference;
 41 import java.util.function.Supplier;
 42 import java.util.stream.Stream;
 43 
 44 import jdk.incubator.foreign.MemorySegment;
 45 import jdk.incubator.foreign.ResourceScope;
 46 import org.testng.annotations.*;
 47 
 48 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 49 import static org.testng.Assert.*;
 50 
 51 /**
 52  * Tests consisting of buffer views with synchronous NIO network channels.
 53  */
 54 public class TestSocketChannels extends AbstractChannelsTest {
 55 
 56     static final Class<IllegalStateException> ISE = IllegalStateException.class;
 57 
 58     @Test(dataProvider = "closeableScopes")
 59     public void testBasicIOWithClosedSegment(Supplier<ResourceScope> scopeSupplier)
 60         throws Exception
 61     {
 62         try (var channel = SocketChannel.open();
 63              var server = ServerSocketChannel.open();
 64              var connectedChannel = connectChannels(server, channel)) {
 65             ResourceScope scope = scopeSupplier.get();
 66             ByteBuffer bb = segmentBufferOfSize(scope, 16);
 67             scope.close();
 68             assertMessage(expectThrows(ISE, () -> channel.read(bb)),                           "Already closed");

 86             scope.close();
 87             assertMessage(expectThrows(ISE, () -> channel.write(buffers)),       "Already closed");
 88             assertMessage(expectThrows(ISE, () -> channel.read(buffers)),        "Already closed");
 89             assertMessage(expectThrows(ISE, () -> channel.write(buffers, 0 ,8)), "Already closed");
 90             assertMessage(expectThrows(ISE, () -> channel.read(buffers, 0, 8)),  "Already closed");
 91         }
 92     }
 93 
 94     @Test(dataProvider = "allScopes")
 95     public void testBasicIO(Supplier<ResourceScope> scopeSupplier)
 96         throws Exception
 97     {
 98         ResourceScope scope;
 99         try (var sc1 = SocketChannel.open();
100              var ssc = ServerSocketChannel.open();
101              var sc2 = connectChannels(ssc, sc1);
102              var scp = closeableScopeOrNull(scope = scopeSupplier.get())) {
103             MemorySegment segment1 = MemorySegment.allocateNative(10, 1, scope);
104             MemorySegment segment2 = MemorySegment.allocateNative(10, 1, scope);
105             for (int i = 0; i < 10; i++) {
106                 segment1.set(JAVA_BYTE, i, (byte) i);
107             }
108             ByteBuffer bb1 = segment1.asByteBuffer();
109             ByteBuffer bb2 = segment2.asByteBuffer();
110             assertEquals(sc1.write(bb1), 10);
111             assertEquals(sc2.read(bb2), 10);
112             assertEquals(bb2.flip(), ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
113         }
114     }
115 
116     @Test
117     public void testBasicHeapIOWithGlobalScope() throws Exception {
118         try (var sc1 = SocketChannel.open();
119              var ssc = ServerSocketChannel.open();
120              var sc2 = connectChannels(ssc, sc1)) {
121             var segment1 = MemorySegment.ofArray(new byte[10]);
122             var segment2 = MemorySegment.ofArray(new byte[10]);
123             for (int i = 0; i < 10; i++) {
124                 segment1.set(JAVA_BYTE, i, (byte) i);
125             }
126             ByteBuffer bb1 = segment1.asByteBuffer();
127             ByteBuffer bb2 = segment2.asByteBuffer();
128             assertEquals(sc1.write(bb1), 10);
129             assertEquals(sc2.read(bb2), 10);
130             assertEquals(bb2.flip(), ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
131         }
132     }
133 
134     @Test(dataProvider = "confinedScopes")
135     public void testIOOnConfinedFromAnotherThread(Supplier<ResourceScope> scopeSupplier)
136         throws Exception
137     {
138         try (var channel = SocketChannel.open();
139              var server = ServerSocketChannel.open();
140              var connected = connectChannels(server, channel);
141              var scope = scopeSupplier.get()) {
142             var segment = MemorySegment.allocateNative(10, 1, scope);
143             ByteBuffer bb = segment.asByteBuffer();
144             List<ThrowingRunnable> ioOps = List.of(
< prev index next >