1 /*
   2  * Copyright (c) 2018, 2019, 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.testlibrary.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.testlibrary.jfr.Events;
  42 import jdk.testlibrary.thread.TestThread;
  43 import jdk.testlibrary.thread.XRun;
  44 import jdk.testlibrary.jfr.IOEvent;
  45 import jdk.testlibrary.jfr.IOHelper;
  46 
  47 /*
  48  * @test
  49  * @key jfr
  50  * @library /lib/testlibrary
  51  * @run main/othervm jdk.jfr.event.io.TestSocketEvents
  52  */
  53 public class TestSocketEvents {
  54 
  55     private static final int writeInt = 'A';
  56     private static final byte[] writeBuf = { 'B', 'C', 'D', 'E' };
  57 
  58     private List<IOEvent> expectedEvents = new ArrayList<>();
  59     private synchronized void addExpectedEvent(IOEvent event) {
  60         expectedEvents.add(event);
  61     }
  62 
  63     public static void main(String[] args) throws Throwable {
  64         new TestSocketEvents().test();
  65     }
  66 
  67     private void test() throws Throwable {
  68         Recording recording = new Recording();
  69 
  70         try (ServerSocket ss = new ServerSocket()) {
  71             recording.enable(IOEvent.EVENT_SOCKET_READ).withThreshold(Duration.ofMillis(0));
  72             recording.enable(IOEvent.EVENT_SOCKET_WRITE).withThreshold(Duration.ofMillis(0));
  73             recording.start();
  74 
  75             ss.setReuseAddress(true);
  76             ss.bind(null);
  77 
  78             TestThread readerThread = new TestThread(new XRun() {
  79                 @Override
  80                 public void xrun() throws IOException {
  81                     byte[] bs = new byte[4];
  82                     try (Socket s = ss.accept(); InputStream is = s.getInputStream()) {
  83                         int readInt = is.read();
  84                         assertEquals(readInt, writeInt, "Wrong readInt");
  85                         addExpectedEvent(IOEvent.createSocketReadEvent(1, s));
  86 
  87                         int bytesRead = is.read(bs, 0, 3);
  88                         assertEquals(bytesRead, 3, "Wrong bytesRead partial buffer");
  89                         addExpectedEvent(IOEvent.createSocketReadEvent(bytesRead, s));
  90 
  91                         bytesRead = is.read(bs);
  92                         assertEquals(bytesRead, writeBuf.length, "Wrong bytesRead full buffer");
  93                         addExpectedEvent(IOEvent.createSocketReadEvent(bytesRead, s));
  94 
  95                         // Try to read more, but writer have closed. Should 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 }