1 /*
2  * Copyright (c) 2018, 2020, 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.io.InputStream;
32 import java.io.OutputStream;
33 import java.net.ServerSocket;
34 import java.net.Socket;
35 import java.time.Duration;
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 import jdk.jfr.Recording;
40 import jdk.jfr.consumer.RecordedEvent;
41 import jdk.test.lib.jfr.Events;
42 import jdk.test.lib.thread.TestThread;
43 import jdk.test.lib.thread.XRun;
44 
45 /**
46  * @test
47  * @key jfr
48  * @requires vm.hasJFR
49  * @library /test/lib /test/jdk
50  * @run main/othervm jdk.jfr.event.io.TestSocketEvents
51  */
52 public class TestSocketEvents {
53 
54     private static final int writeInt = 'A';
55     private static final byte[] writeBuf = { 'B', 'C', 'D', 'E' };
56 
addExpectedEvent(IOEvent event)57     private List<IOEvent> expectedEvents = new ArrayList<>();
58 
59     private synchronized void addExpectedEvent(IOEvent event) {
60         expectedEvents.add(event);
main(String[] args)61     }
62 
63     public static void main(String[] args) throws Throwable {
64         new TestSocketEvents().test();
65     }
try(Recording recording = new Recording())66 
67     private void test() throws Throwable {
68         try (Recording recording = new Recording()) {
69             try (ServerSocket ss = new ServerSocket()) {
70                 recording.enable(IOEvent.EVENT_SOCKET_READ).withThreshold(Duration.ofMillis(0));
71                 recording.enable(IOEvent.EVENT_SOCKET_WRITE).withThreshold(Duration.ofMillis(0));
72                 recording.start();
73 
74                 ss.setReuseAddress(true);
75                 ss.bind(null);
76 
77                 TestThread readerThread = new TestThread(new XRun() {
78                     @Override
79                     public void xrun() throws IOException {
80                         byte[] bs = new byte[4];
81                         try (Socket s = ss.accept(); InputStream is = s.getInputStream()) {
82                             int readInt = is.read();
83                             assertEquals(readInt, writeInt, "Wrong readInt");
84                             addExpectedEvent(IOEvent.createSocketReadEvent(1, s));
85 
86                             int bytesRead = is.read(bs, 0, 3);
87                             assertEquals(bytesRead, 3, "Wrong bytesRead partial buffer");
88                             addExpectedEvent(IOEvent.createSocketReadEvent(bytesRead, s));
89 
90                             bytesRead = is.read(bs);
91                             assertEquals(bytesRead, writeBuf.length, "Wrong bytesRead full buffer");
92                             addExpectedEvent(IOEvent.createSocketReadEvent(bytesRead, s));
93 
94                             // Try to read more, but writer have closed. Should
95                             // get EOF.
96                             readInt = is.read();
97                             assertEquals(readInt, -1, "Wrong readInt at EOF");
98                             addExpectedEvent(IOEvent.createSocketReadEvent(-1, s));
99                         }
100                     }
101                 });
102                 readerThread.start();
103 
104                 try (Socket s = new Socket()) {
105                     s.connect(ss.getLocalSocketAddress());
106                     try (OutputStream os = s.getOutputStream()) {
107                         os.write(writeInt);
108                         addExpectedEvent(IOEvent.createSocketWriteEvent(1, s));
109                         os.write(writeBuf, 0, 3);
110                         addExpectedEvent(IOEvent.createSocketWriteEvent(3, s));
111                         os.write(writeBuf);
112                         addExpectedEvent(IOEvent.createSocketWriteEvent(writeBuf.length, s));
113                     }
114                 }
115 
116                 readerThread.joinAndThrow();
117                 recording.stop();
118                 List<RecordedEvent> events = Events.fromRecording(recording);
119                 IOHelper.verifyEquals(events, expectedEvents);
120             }
121         }
122     }
123 }
124