50113
|
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.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 |
* @library /test/lib /test/jdk
|
|
49 |
* @run main/othervm jdk.jfr.event.io.TestSocketEvents
|
|
50 |
*/
|
|
51 |
public class TestSocketEvents {
|
|
52 |
|
|
53 |
private static final int writeInt = 'A';
|
|
54 |
private static final byte[] writeBuf = { 'B', 'C', 'D', 'E' };
|
|
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 TestSocketEvents().test();
|
|
63 |
}
|
|
64 |
|
|
65 |
private void test() throws Throwable {
|
|
66 |
Recording recording = new Recording();
|
|
67 |
|
|
68 |
try (ServerSocket ss = new ServerSocket()) {
|
|
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.setReuseAddress(true);
|
|
74 |
ss.bind(null);
|
|
75 |
|
|
76 |
TestThread readerThread = new TestThread(new XRun() {
|
|
77 |
@Override
|
|
78 |
public void xrun() throws IOException {
|
|
79 |
byte[] bs = new byte[4];
|
|
80 |
try (Socket s = ss.accept(); InputStream is = s.getInputStream()) {
|
|
81 |
int readInt = is.read();
|
|
82 |
assertEquals(readInt, writeInt, "Wrong readInt");
|
|
83 |
addExpectedEvent(IOEvent.createSocketReadEvent(1, s));
|
|
84 |
|
|
85 |
int bytesRead = is.read(bs, 0, 3);
|
|
86 |
assertEquals(bytesRead, 3, "Wrong bytesRead partial buffer");
|
|
87 |
addExpectedEvent(IOEvent.createSocketReadEvent(bytesRead, s));
|
|
88 |
|
|
89 |
bytesRead = is.read(bs);
|
|
90 |
assertEquals(bytesRead, writeBuf.length, "Wrong bytesRead full buffer");
|
|
91 |
addExpectedEvent(IOEvent.createSocketReadEvent(bytesRead, s));
|
|
92 |
|
|
93 |
// Try to read more, but writer have closed. Should get EOF.
|
|
94 |
readInt = is.read();
|
|
95 |
assertEquals(readInt, -1, "Wrong readInt at EOF");
|
|
96 |
addExpectedEvent(IOEvent.createSocketReadEvent(-1, s));
|
|
97 |
}
|
|
98 |
}
|
|
99 |
});
|
|
100 |
readerThread.start();
|
|
101 |
|
|
102 |
try (Socket s = new Socket()) {
|
|
103 |
s.connect(ss.getLocalSocketAddress());
|
|
104 |
try (OutputStream os = s.getOutputStream()) {
|
|
105 |
os.write(writeInt);
|
|
106 |
addExpectedEvent(IOEvent.createSocketWriteEvent(1, s));
|
|
107 |
os.write(writeBuf, 0, 3);
|
|
108 |
addExpectedEvent(IOEvent.createSocketWriteEvent(3, s));
|
|
109 |
os.write(writeBuf);
|
|
110 |
addExpectedEvent(IOEvent.createSocketWriteEvent(writeBuf.length, s));
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
readerThread.joinAndThrow();
|
|
115 |
recording.stop();
|
|
116 |
List<RecordedEvent> events = Events.fromRecording(recording);
|
|
117 |
IOHelper.verifyEquals(events, expectedEvents);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
}
|