1 /*
2  * Copyright (c) 2018, 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.test.lib.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.test.lib.jfr.Events;
41 import jdk.test.lib.thread.TestThread;
42 import jdk.test.lib.thread.XRun;
43 
44 /**
45  * @test
46  * @key jfr
47  * @requires vm.hasJFR
48  * @library /test/lib /test/jdk
49  * @run main/othervm jdk.jfr.event.io.TestSocketChannelEvents
50  */
51 public class TestSocketChannelEvents {
52     private static final int bufSizeA = 10;
53     private static final int bufSizeB = 20;
54 
55     private List<IOEvent> expectedEvents = new ArrayList<>();
addExpectedEvent(IOEvent event)56     private synchronized void addExpectedEvent(IOEvent event) {
57         expectedEvents.add(event);
58     }
59 
main(String[] args)60     public static void main(String[] args) throws Throwable {
61         new TestSocketChannelEvents().test();
62     }
63 
test()64     public void test() throws Throwable {
65         Recording recording = new Recording();
66 
67         try (ServerSocketChannel ss = ServerSocketChannel.open()) {
68             recording.enable(IOEvent.EVENT_SOCKET_READ).withThreshold(Duration.ofMillis(0));
69             recording.enable(IOEvent.EVENT_SOCKET_WRITE).withThreshold(Duration.ofMillis(0));
70             recording.start();
71 
72             ss.socket().setReuseAddress(true);
73             ss.socket().bind(null);
74 
75             TestThread readerThread = new TestThread(new XRun() {
76                 @Override
77                 public void xrun() throws IOException {
78                     ByteBuffer bufA = ByteBuffer.allocate(bufSizeA);
79                     ByteBuffer bufB = ByteBuffer.allocate(bufSizeB);
80                     try (SocketChannel sc = ss.accept()) {
81                         int readSize = sc.read(bufA);
82                         assertEquals(readSize, bufSizeA, "Wrong readSize bufA");
83                         addExpectedEvent(IOEvent.createSocketReadEvent(bufSizeA, sc.socket()));
84 
85                         bufA.clear();
86                         bufA.limit(1);
87                         readSize = (int)sc.read(new ByteBuffer[] { bufA, bufB });
88                         assertEquals(readSize, 1 + bufSizeB, "Wrong readSize 1+bufB");
89                         addExpectedEvent(IOEvent.createSocketReadEvent(readSize, sc.socket()));
90 
91                         // We try to read, but client have closed. Should get EOF.
92                         bufA.clear();
93                         bufA.limit(1);
94                         readSize = sc.read(bufA);
95                         assertEquals(readSize, -1, "Wrong readSize at EOF");
96                         addExpectedEvent(IOEvent.createSocketReadEvent(-1, sc.socket()));
97                     }
98                 }
99             });
100             readerThread.start();
101 
102             try (SocketChannel sc = SocketChannel.open(ss.socket().getLocalSocketAddress())) {
103                 ByteBuffer bufA = ByteBuffer.allocateDirect(bufSizeA);
104                 ByteBuffer bufB = ByteBuffer.allocateDirect(bufSizeB);
105                 for (int i = 0; i < bufSizeA; ++i) {
106                     bufA.put((byte)('a' + (i % 20)));
107                 }
108                 for (int i = 0; i < bufSizeB; ++i) {
109                     bufB.put((byte)('A' + (i % 20)));
110                 }
111                 bufA.flip();
112                 bufB.flip();
113 
114                 sc.write(bufA);
115                 addExpectedEvent(IOEvent.createSocketWriteEvent(bufSizeA, sc.socket()));
116 
117                 bufA.clear();
118                 bufA.limit(1);
119                 int bytesWritten = (int)sc.write(new ByteBuffer[] { bufA, bufB });
120                 assertEquals(bytesWritten, 1 + bufSizeB, "Wrong bytesWritten 1+bufB");
121                 addExpectedEvent(IOEvent.createSocketWriteEvent(bytesWritten, sc.socket()));
122             }
123 
124             readerThread.joinAndThrow();
125             recording.stop();
126             List<RecordedEvent> events= Events.fromRecording(recording);
127             IOHelper.verifyEquals(events, expectedEvents);
128         }
129     }
130 }
131