/* * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// Ensure that metadata are written twice. try (Recording rotator = new Recording()) {
rotator.start();
rotator.stop();
}
r2.stop();
r2.dump(twoEventTypes);
}
FlightRecorder.register(Event3.class);
r1.stop();
r1.dump(threeEventTypes);
} try (RecordingFile f = new RecordingFile(twoEventTypes)) {
List<EventType> types = f.readEventTypes();
assertUniqueEventTypes(types);
assertHasEventType(types, "Event1");
assertHasEventType(types, "Event2");
assertMissingEventType(types, "Event3");
} try (RecordingFile f = new RecordingFile(threeEventTypes)) {
List<EventType> types = f.readEventTypes();
assertUniqueEventTypes(types);
assertHasEventType(types, "Event1");
assertHasEventType(types, "Event2");
assertHasEventType(types, "Event3");
}
}
privatestaticvoid assertMissingEventType(List<EventType> types,String name) throws Exception {
EventType type = findEventType(types, name); if (type != null) { thrownew Exception("Found unexpected event type " + name);
}
}
privatestaticvoid assertHasEventType(List<EventType> types,String name) throws Exception {
EventType type = findEventType(types, name); if (type == null) { thrownew Exception("Missing event type " + name);
}
}
privatestatic EventType findEventType(List<EventType> types, String name) { for (EventType t : types) { if (t.getName().equals(name)) { return t;
}
} returnnull;
}
privatestaticvoid assertUniqueEventTypes(List<EventType> types) {
HashSet<Long> ids = new HashSet<>(); for (EventType type : types) {
ids.add(type.getId());
}
Asserts.assertEquals(types.size(), ids.size(), "Event types repeated. " + types);
}
privatestatic Path createBrokenWIthZeros(Path valid) throws Exception { try {
Path broken = Utils.createTempFile("broken-events", ".jfr");
Files.delete(broken);
Files.copy(valid, broken);
RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
raf.seek(HEADER_SIZE); int size = (int) Files.size(broken); byte[] ones = newbyte[size - HEADER_SIZE]; for (int i = 0; i < ones.length; i++) {
ones[i] = (byte) 0xFF;
}
raf.write(ones, 0, ones.length);
raf.close(); return broken;
} catch (IOException ioe) { thrownew Exception("Could not produce a broken file " + valid, ioe);
}
}
privatestatic Path createBrokenMetadata(Path valid) throws Exception { try {
Path broken = Utils.createTempFile("broken-metadata", ".jfr");
Files.delete(broken);
Files.copy(valid, broken);
RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
raf.seek(METADATA_OFFSET); long metadataOffset = raf.readLong();
raf.seek(metadataOffset);
raf.writeLong(Long.MAX_VALUE);
raf.writeLong(Long.MAX_VALUE);
raf.close(); return broken;
} catch (IOException ioe) { thrownew Exception("Could not produce a broken EventSet from file " + valid, ioe);
}
}
privatestaticvoid testReadEventTypes(Path valid, Path broken) throws Exception { try (RecordingFile validFile = new RecordingFile(valid)) {
List<EventType> types = validFile.readEventTypes(); if (types.size() < EVENT_COUNT) { thrownew Exception("Expected at least " + EVENT_COUNT + " event type but got " + types.toString());
} int counter = 0; for (Class<?> testClass : Arrays.asList(TestEvent1.class, TestEvent2.class, TestEvent3.class)) { for (EventType t : types) { if (t.getName().equals(testClass.getName())) {
counter++;
}
}
} if (counter != 3) { thrownew Exception("Returned incorrect event types");
}
} try (RecordingFile brokenFile = new RecordingFile(broken)) {
brokenFile.readEventTypes(); thrownew Exception("Expected IOException when getting Event Types from broken recording");
} catch (IOException ise) { // OK
}
}
privatestaticvoid testNewRecordingFile(Path valid, Path broken) throws Exception { try (RecordingFile r = new RecordingFile(null)) { thrownew Exception("Expected NullPointerException");
} catch (NullPointerException npe) { // OK
} try (RecordingFile r = new RecordingFile(Paths.get("hjhjsdfhkjshdfkj.jfr"))) { thrownew Exception("Expected FileNotFoundException");
} catch (FileNotFoundException npe) { // OK
}
Path testFile = Utils.createTempFile("test-empty-file", ".jfr"); try (RecordingFile r = new RecordingFile(testFile)) { thrownew Exception("Expected IOException if file is empty");
} catch (IOException e) { // OK
}
FileWriter fr = new FileWriter(testFile.toFile());
fr.write("whatever");
fr.close(); try (RecordingFile r = new RecordingFile(Paths.get("hjhjsdfhkjshdfkj.jfr"))) { thrownew Exception("Expected IOException if magic is incorrect");
} catch (IOException e) { // OK
}
try (RecordingFile r = new RecordingFile(valid)) {
}
}
privatestaticvoid testClose(Path valid) throws Exception {
RecordingFile f = new RecordingFile(valid);
f.close();
try {
f.readEvent(); thrownew Exception("Should not be able to read event from closed recording");
} catch (IOException e) { if (!e.getMessage().equals("Stream Closed")) { thrownew Exception("Expected 'Stream Closed' in exception message for a closed stream. Got '" + e.getMessage() +"'.");
} // OK
} try {
f.readEventTypes(); thrownew Exception("Should not be able to read event from closed recording");
} catch (IOException e) { if (!e.getMessage().equals("Stream Closed")) { thrownew Exception("Expected 'Stream Closed' in exception message for a closed stream. Got '" + e.getMessage() +"'.");
} // OK
} // close twice
f.close();
}
privatestaticvoid testIterate(Path valid, Path broken) throws Exception { try (RecordingFile validFile = new RecordingFile(valid)) { for (int i = 0; i < EVENT_COUNT; i++) { if (!validFile.hasMoreEvents()) { thrownew Exception("Not all events available");
}
RecordedEvent r = validFile.readEvent(); if (r == null) { thrownew Exception("Missing event");
} if (!r.getEventType().getName().contains(TEST_CLASS_BASE)) { thrownew Exception("Incorrect event in recording file " + r);
}
} if (validFile.hasMoreEvents()) { thrownew Exception("Should not be more than " + EVENT_COUNT + " in recording");
}
} try (RecordingFile brokenFile = new RecordingFile(broken)) {
brokenFile.readEvent(); thrownew Exception("Expected IOException for broken recording");
} catch (IOException ise) { // OK
}
}
privatestaticvoid testReadAllEvents(Path valid, Path broken) throws Exception { try {
RecordingFile.readAllEvents(broken); thrownew Exception("Expected IOException when reading all events for broken recording");
} catch (IOException ioe) { // OK as expected
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.