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