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