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.File; 31 import java.io.FileInputStream; 32 import java.io.FileOutputStream; 33 import java.time.Duration; 34 import java.util.ArrayList; 35 import java.util.List; 36 37 import jdk.jfr.Recording; 38 import jdk.jfr.consumer.RecordedEvent; 39 import jdk.test.lib.jfr.Events; 40 41 /** 42 * @test TestFileStreamEvents 43 * @key jfr 44 * 45 * @library /lib / 46 * @run main/othervm jdk.jfr.event.io.TestFileStreamEvents 47 */ 48 49 public class TestFileStreamEvents { 50 public static void main(String[] args) throws Throwable { 51 File tmp = File.createTempFile("TestFileStreamEvents", ".tmp", new File(".")); 52 tmp.deleteOnExit(); 53 Recording recording = new Recording(); 54 List<IOEvent> expectedEvents = new ArrayList<>(); 55 56 try(FileOutputStream fos = new FileOutputStream(tmp); FileInputStream fis = new FileInputStream(tmp);) { 57 recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0)); 58 recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0)); 59 recording.start(); 60 61 int writeByte = 47; 62 byte[] writeBuf = {11, 12, 13, 14}; 63 64 // Write 65 fos.write(writeByte); 66 expectedEvents.add(IOEvent.createFileWriteEvent(1, tmp)); 67 fos.write(writeBuf); 68 expectedEvents.add(IOEvent.createFileWriteEvent(writeBuf.length, tmp)); 69 fos.write(writeBuf, 0, 2); 70 expectedEvents.add(IOEvent.createFileWriteEvent(2, tmp)); 71 72 // Read 73 int readByte = fis.read(); 74 assertEquals(readByte, writeByte, "Wrong byte read"); 75 expectedEvents.add(IOEvent.createFileReadEvent(1, tmp)); 76 77 byte[] readBuf = new byte[writeBuf.length]; 78 long size = fis.read(readBuf); 79 assertEquals(size, (long)writeBuf.length, "Wrong size when reading byte[]"); 80 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 81 82 size = fis.read(readBuf, 0, 2); 83 assertEquals(size, 2L, "Wrong size when reading 2 bytes"); 84 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 85 86 // We are at EOF. Read more and verify we get size -1. 87 size = fis.read(readBuf); 88 assertEquals(size, -1L, "Size should be -1 at EOF"); 89 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 90 91 recording.stop(); 92 List<RecordedEvent> events = Events.fromRecording(recording); 93 IOHelper.verifyEqualsInOrder(events, expectedEvents); 94 } 95 } 96 }