1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.io; 27 28 import static jdk.testlibrary.Asserts.assertEquals; 29 30 import java.io.IOException; 31 import java.nio.ByteBuffer; 32 import java.nio.channels.ServerSocketChannel; 33 import java.nio.channels.SocketChannel; 34 import java.time.Duration; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 import jdk.jfr.Recording; 39 import jdk.jfr.consumer.RecordedEvent; 40 import jdk.testlibrary.jfr.Events; 41 import jdk.testlibrary.thread.TestThread; 42 import jdk.testlibrary.thread.XRun; 43 import jdk.testlibrary.jfr.IOEvent; 44 import jdk.testlibrary.jfr.IOHelper; 45 46 /* 47 * @test 48 * @key jfr 49 * @library /lib/testlibrary 50 * @run main/othervm jdk.jfr.event.io.TestSocketChannelEvents 51 */ 52 public class TestSocketChannelEvents { 53 private static final int bufSizeA = 10; 54 private static final int bufSizeB = 20; 55 56 private List<IOEvent> expectedEvents = new ArrayList<>(); 57 private synchronized void addExpectedEvent(IOEvent event) { 58 expectedEvents.add(event); 59 } 60 61 public static void main(String[] args) throws Throwable { 62 new TestSocketChannelEvents().test(); 63 } 64 65 public void test() throws Throwable { 66 Recording recording = new Recording(); 67 68 try (ServerSocketChannel ss = ServerSocketChannel.open()) { 69 recording.enable(IOEvent.EVENT_SOCKET_READ).withThreshold(Duration.ofMillis(0)); 70 recording.enable(IOEvent.EVENT_SOCKET_WRITE).withThreshold(Duration.ofMillis(0)); 71 recording.start(); 72 73 ss.socket().setReuseAddress(true); 74 ss.socket().bind(null); 75 76 TestThread readerThread = new TestThread(new XRun() { 77 @Override 78 public void xrun() throws IOException { 79 ByteBuffer bufA = ByteBuffer.allocate(bufSizeA); 80 ByteBuffer bufB = ByteBuffer.allocate(bufSizeB); 81 try (SocketChannel sc = ss.accept()) { 82 int readSize = sc.read(bufA); 83 assertEquals(readSize, bufSizeA, "Wrong readSize bufA"); 84 addExpectedEvent(IOEvent.createSocketReadEvent(bufSizeA, sc.socket())); 85 86 bufA.clear(); 87 bufA.limit(1); 88 readSize = (int)sc.read(new ByteBuffer[] { bufA, bufB }); 89 assertEquals(readSize, 1 + bufSizeB, "Wrong readSize 1+bufB"); 90 addExpectedEvent(IOEvent.createSocketReadEvent(readSize, sc.socket())); 91 92 // We try to read, but client have closed. Should get EOF. 93 bufA.clear(); 94 bufA.limit(1); 95 readSize = sc.read(bufA); 96 assertEquals(readSize, -1, "Wrong readSize at EOF"); 97 addExpectedEvent(IOEvent.createSocketReadEvent(-1, sc.socket())); 98 } 99 } 100 }); 101 readerThread.start(); 102 103 try (SocketChannel sc = SocketChannel.open(ss.socket().getLocalSocketAddress())) { 104 ByteBuffer bufA = ByteBuffer.allocateDirect(bufSizeA); 105 ByteBuffer bufB = ByteBuffer.allocateDirect(bufSizeB); 106 for (int i = 0; i < bufSizeA; ++i) { 107 bufA.put((byte)('a' + (i % 20))); 108 } 109 for (int i = 0; i < bufSizeB; ++i) { 110 bufB.put((byte)('A' + (i % 20))); 111 } 112 bufA.flip(); 113 bufB.flip(); 114 115 sc.write(bufA); 116 addExpectedEvent(IOEvent.createSocketWriteEvent(bufSizeA, sc.socket())); 117 118 bufA.clear(); 119 bufA.limit(1); 120 int bytesWritten = (int)sc.write(new ByteBuffer[] { bufA, bufB }); 121 assertEquals(bytesWritten, 1 + bufSizeB, "Wrong bytesWritten 1+bufB"); 122 addExpectedEvent(IOEvent.createSocketWriteEvent(bytesWritten, sc.socket())); 123 } 124 125 readerThread.joinAndThrow(); 126 recording.stop(); 127 List<RecordedEvent> events= Events.fromRecording(recording); 128 IOHelper.verifyEquals(events, expectedEvents); 129 } 130 } 131 }