test/jdk/jdk/jfr/event/io/TestRandomAccessFileEvents.java
changeset 50113 caf115bb98ad
child 51214 67736b4846a0
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     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.RandomAccessFile;
       
    32 import java.time.Duration;
       
    33 import java.util.ArrayList;
       
    34 import java.util.List;
       
    35 
       
    36 import jdk.jfr.Recording;
       
    37 import jdk.jfr.consumer.RecordedEvent;
       
    38 import jdk.test.lib.jfr.Events;
       
    39 
       
    40 /*
       
    41  * @test
       
    42  * @key jfr
       
    43  * @library /test/lib /test/jdk
       
    44  * @run main/othervm jdk.jfr.event.io.TestRandomAccessFileEvents
       
    45  */
       
    46 public class TestRandomAccessFileEvents {
       
    47 
       
    48     public static void main(String[] args) throws Throwable {
       
    49         File tmp = File.createTempFile("TestRandomAccessFileEvents", ".tmp", new File("."));
       
    50         tmp.deleteOnExit();
       
    51         Recording recording = new Recording();
       
    52         List<IOEvent> expectedEvents = new ArrayList<>();
       
    53 
       
    54         recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0));
       
    55         recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0));
       
    56         recording.start();
       
    57 
       
    58         RandomAccessFile ras = new RandomAccessFile(tmp, "rw");
       
    59         int writeInt = 47;
       
    60         byte[] writeBuffer = {10,11,12,13};
       
    61 
       
    62         // Write an int and a buffer.
       
    63         ras.write(writeInt);
       
    64         expectedEvents.add(IOEvent.createFileWriteEvent(1, tmp));
       
    65         ras.write(writeBuffer);
       
    66         expectedEvents.add(IOEvent.createFileWriteEvent(writeBuffer.length, tmp));
       
    67 
       
    68         ras.seek(0);
       
    69 
       
    70         // Read int and buffer
       
    71         int readInt = ras.read();
       
    72         assertEquals(readInt, writeInt, "wrong int read");
       
    73         expectedEvents.add(IOEvent.createFileReadEvent(1, tmp));
       
    74         byte[] readBuffer = new byte [writeBuffer.length];
       
    75         int size = ras.read(readBuffer);
       
    76         verifyBufferEquals(readBuffer, writeBuffer);
       
    77         expectedEvents.add(IOEvent.createFileReadEvent(readBuffer.length, tmp));
       
    78 
       
    79         // Read beyond EOF
       
    80         readInt = ras.read();
       
    81         assertEquals(-1, readInt, "wrong read after EOF");
       
    82         expectedEvents.add(IOEvent.createFileReadEvent(-1, tmp));
       
    83 
       
    84         // Seek to beginning and verify we can read after EOF.
       
    85         ras.seek(0);
       
    86         readInt = ras.read();
       
    87         assertEquals(readInt, writeInt, "wrong int read after seek(0)");
       
    88         expectedEvents.add(IOEvent.createFileReadEvent(1, tmp));
       
    89 
       
    90         // seek beyond EOF and verify we get EOF when reading.
       
    91         ras.seek(10);
       
    92         readInt = ras.read();
       
    93         assertEquals(-1, readInt, "wrong read after seek beyond EOF");
       
    94         expectedEvents.add(IOEvent.createFileReadEvent(-1, tmp));
       
    95 
       
    96         // Read partial buffer.
       
    97         int partialSize = writeBuffer.length - 2;
       
    98         ras.seek(ras.length()-partialSize);
       
    99         size = ras.read(readBuffer);
       
   100         assertEquals(size, partialSize, "Wrong size partial buffer read");
       
   101         expectedEvents.add(IOEvent.createFileReadEvent(size, tmp));
       
   102 
       
   103         ras.close();
       
   104         recording.stop();
       
   105         List<RecordedEvent> events = Events.fromRecording(recording);
       
   106         IOHelper.verifyEqualsInOrder(events, expectedEvents);
       
   107     }
       
   108 
       
   109     private static void verifyBufferEquals(byte[] a, byte[] b) {
       
   110         assertEquals(a.length, b.length, "Wrong buffer size");
       
   111         for (int i = 0; i < a.length; ++i) {
       
   112             assertEquals(a[i], b[i], "Wrong buffer content at pos " + i);
       
   113         }
       
   114     }
       
   115 }