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  * 
  49  * @library /lib /
  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 
  57     private List<IOEvent> expectedEvents = new ArrayList<>();
  58     private synchronized void addExpectedEvent(IOEvent event) {
  59         expectedEvents.add(event);
  60     }
  61 
  62     public static void main(String[] args) throws Throwable {
  63         new TestSocketEvents().test();
  64     }
  65 
  66     private void test() throws Throwable {
  67         Recording recording = new Recording();
  68 
  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 get EOF.
  95                         readInt = is.read();
  96                         assertEquals(readInt, -1, "Wrong readInt at EOF");
  97                         addExpectedEvent(IOEvent.createSocketReadEvent(-1, s));
  98                    }
  99                 }
 100             });
 101             readerThread.start();
 102 
 103             try (Socket s = new Socket()) {
 104                 s.connect(ss.getLocalSocketAddress());
 105                 try (OutputStream os = s.getOutputStream()) {
 106                     os.write(writeInt);
 107                     addExpectedEvent(IOEvent.createSocketWriteEvent(1, s));
 108                     os.write(writeBuf, 0, 3);
 109                     addExpectedEvent(IOEvent.createSocketWriteEvent(3, s));
 110                     os.write(writeBuf);
 111                     addExpectedEvent(IOEvent.createSocketWriteEvent(writeBuf.length, s));
 112                 }
 113             }
 114 
 115             readerThread.joinAndThrow();
 116             recording.stop();
 117             List<RecordedEvent> events = Events.fromRecording(recording);
 118             IOHelper.verifyEquals(events, expectedEvents);
 119         }
 120     }
 121 }