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 import static jdk.test.lib.Asserts.assertTrue; 30 31 import java.util.List; 32 import java.util.stream.Collectors; 33 34 import jdk.jfr.event.io.IOEvent.EventType; 35 36 import jdk.jfr.consumer.RecordedEvent; 37 import jdk.test.lib.jfr.Events; 38 39 40 // Helper class to match actual RecordedEvents to expected events. 41 public class IOHelper { 42 43 public static void verifyEqualsInOrder(List<RecordedEvent> events, List<IOEvent> expectedEvents) throws Throwable { 44 List<IOEvent> actualEvents = getTestEvents(events, expectedEvents); 45 try { 46 assertEquals(actualEvents.size(), expectedEvents.size(), "Wrong number of events."); 47 for (int i = 0; i < actualEvents.size(); ++i) { 48 assertEquals(actualEvents.get(i), expectedEvents.get(i), "Wrong event at pos " + i); 49 } 50 } catch (Throwable t) { 51 logEvents(actualEvents, expectedEvents); 52 throw t; 53 } 54 } 55 56 57 public static void verifyEquals(List<RecordedEvent> events, List<IOEvent> expectedEvents) throws Throwable { 58 List<IOEvent> actualEvents = getTestEvents(events, expectedEvents); 59 try { 60 assertEquals(actualEvents.size(), expectedEvents.size(), "Wrong number of events"); 61 assertTrue(actualEvents.containsAll(expectedEvents), "Not all expected events received"); 62 assertTrue(expectedEvents.containsAll(actualEvents), "Received unexpected events"); 63 } catch (Throwable t) { 64 logEvents(actualEvents, expectedEvents); 65 throw t; 66 } 67 } 68 69 70 private static List<IOEvent> getTestEvents(List<RecordedEvent> events, List<IOEvent> expectedEvents) throws Throwable { 71 // Log all events 72 for (RecordedEvent event : events) { 73 String msg = event.getEventType().getName(); 74 boolean isSocket = IOEvent.EVENT_SOCKET_READ.equals(msg) || IOEvent.EVENT_SOCKET_WRITE.equals(msg); 75 boolean isFile = IOEvent.EVENT_FILE_FORCE.equals(msg) || IOEvent.EVENT_FILE_READ.equals(msg) || IOEvent.EVENT_FILE_WRITE.equals(msg); 76 boolean isFileReadOrWrite = IOEvent.EVENT_FILE_READ.equals(msg) || IOEvent.EVENT_FILE_WRITE.equals(msg); 77 boolean isRead = IOEvent.EVENT_FILE_READ.equals(msg) || IOEvent.EVENT_SOCKET_READ.equals(msg); 78 if (isFile) { 79 msg += " : " + Events.assertField(event, "path").getValue(); 80 } else if (isSocket) { 81 msg += " - " + Events.assertField(event, "host").getValue(); 82 msg += "." + Events.assertField(event, "address").getValue(); 83 msg += "." + Events.assertField(event, "port").getValue(); 84 } 85 if (isSocket || isFileReadOrWrite) { 86 String field = isRead ? "bytesRead" : "bytesWritten"; 87 msg += " : " + Events.assertField(event, field).getValue(); 88 } 89 System.out.println(msg); 90 } 91 92 return events.stream() 93 .filter(event -> isTestEvent(event, expectedEvents)) 94 .map(event -> IOEvent.createTestEvent(event)) 95 .collect(Collectors.toList()); 96 } 97 98 // A recording may contain extra events that are not part of the test. 99 // This function filters out events that not belong to the test. 100 public static boolean isTestEvent(RecordedEvent event, List<IOEvent> testEvents) { 101 EventType eventType = IOEvent.getEventType(event); 102 if (eventType == EventType.UnknownEvent) { 103 return false; 104 } 105 106 // Only care about threads in the expected threads. 107 final String threadName = event.getThread().getJavaName(); 108 if (testEvents.stream().noneMatch(te -> te.thread.equals(threadName))) { 109 return false; 110 } 111 112 // Only care about files and sockets in expected events. 113 final String address = IOEvent.getEventAddress(event); 114 if (testEvents.stream().noneMatch(te -> te.address.equals(address))) { 115 return false; 116 } 117 return true; 118 } 119 120 private static void logEvents(List<IOEvent> actualEvents, List<IOEvent> expectedEvents) { 121 for (int i = 0; i < actualEvents.size(); ++i) { 122 System.out.println("actual event[" + i + "] = " + actualEvents.get(i)); 123 } 124 for (int i = 0; i < expectedEvents.size(); ++i) { 125 System.out.println("expected event[" + i + "] = " + expectedEvents.get(i)); 126 } 127 } 128 129 }