Java Code Examples for jdk.testlibrary.Asserts#assertGreaterThan()

The following examples show how to use jdk.testlibrary.Asserts#assertGreaterThan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStateDuration.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Duration duration = Duration.ofSeconds(2);
    Recording r = new Recording();
    r.setDuration(duration);
    CommonHelper.verifyRecordingState(r, RecordingState.NEW);
    Instant start = Instant.now();
    System.out.println("Recording with duration " + duration + " started at " + start);
    r.start();

    // Wait for recording to stop automatically
    System.out.println("Waiting for recording to reach STOPPED state");
    CommonHelper.waitForRecordingState(r, RecordingState.STOPPED);
    Instant stop = Instant.now();
    Duration measuredDuration = Duration.between(start, stop);
    System.out.println("Recording stopped at " + stop + ". Measured duration " + measuredDuration);
    // Timer task uses System.currentMillis, and java.time uses other source.
    Duration deltaDueToClockNotInSync = Duration.ofMillis(100);
    Asserts.assertGreaterThan(measuredDuration.plus(deltaDueToClockNotInSync), duration);
    verifyIllegalState(() -> r.start(), "start() after stop()");
    r.close();
    CommonHelper.verifyRecordingState(r, RecordingState.CLOSED);
}
 
Example 2
Source File: TestDumpState.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void checkEvents(Recording r, List<Integer> expectedIds) throws Exception {
    Path path = Paths.get(".", String.format("%d.jfr", recordingCounter++));
    r.dump(path);
    Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);

    int index = 0;

    for (RecordedEvent event : RecordingFile.readAllEvents(path)) {
        Events.isEventType(event, SimpleEvent.class.getName());
        Integer id = Events.assertField(event, "id").getValue();
        System.out.println("Got event with id " + id);
        Asserts.assertGreaterThan(expectedIds.size(), index, "Too many Events found");
        Asserts.assertEquals(id, expectedIds.get(index), "Wrong id at index " + index);
        ++index;
    }
    Asserts.assertEquals(index, expectedIds.size(), "Too few Events found");
}
 
Example 3
Source File: TestGetTypeId.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType type = EventType.getEventType(CustomEvent.class);

    SettingDescriptor plain = Events.getSetting(type, "plain");
    long plainId = plain.getTypeId();
    Asserts.assertGreaterThan(plainId, 0L);

    SettingDescriptor annotatedType = Events.getSetting(type, "annotatedType");
    long annotatedId = annotatedType.getTypeId();
    Asserts.assertGreaterThan(annotatedId, 0L);

    Asserts.assertNotEquals(annotatedId, plainId);

    SettingDescriptor newName = Events.getSetting(type, "newName");
    Asserts.assertEquals(newName.getTypeId(), annotatedId);

    SettingDescriptor overridden = Events.getSetting(type, "overridden");
    Asserts.assertEquals(overridden.getTypeId(), plainId);

    SettingDescriptor protectedBase = Events.getSetting(type, "protectedBase");
    Asserts.assertEquals(protectedBase.getTypeId(), plainId);

    SettingDescriptor publicBase = Events.getSetting(type, "publicBase");
    Asserts.assertEquals(publicBase.getTypeId(), annotatedId);

    SettingDescriptor packageProtectedBase = Events.getSetting(type, "packageProtectedBase");
    Asserts.assertEquals(packageProtectedBase.getTypeId(), plainId);

    CustomEvent.assertOnDisk((x, y) -> Long.compare(x.getTypeId(), y.getTypeId()));
}
 
Example 4
Source File: TestSnapshot.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testOngoing(boolean disk) throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    try (Recording r = new Recording()) {
        r.setToDisk(disk);
        r.enable(SimpleEvent.class);
        r.start();
        SimpleEvent se = new SimpleEvent();
        se.commit();

        try (Recording snapshot = recorder.takeSnapshot()) {

            Asserts.assertGreaterThan(snapshot.getSize(), 0L);
            Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), r.getStartTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getStopTime(), r.getStartTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
            assertStaticOptions(snapshot);
            try (InputStream is = snapshot.getStream(null, null)) {
                Asserts.assertNotNull(is);
            }

            List<RecordedEvent> events = Events.fromRecording(snapshot);
            Events.hasEvents(events);
            Asserts.assertEquals(events.size(), 1);
            Asserts.assertEquals(events.get(0).getEventType().getName(), SimpleEvent.class.getName());
        }

        r.stop();
    }
}
 
Example 5
Source File: TestSnapshot.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testEmpty() throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    Instant before = Instant.now().minusNanos(1);
    try (Recording snapshot = recorder.takeSnapshot()) {
        Instant after = Instant.now().plusNanos(1);
        Asserts.assertEquals(snapshot.getSize(), 0L);
        Asserts.assertLessThan(before, snapshot.getStartTime());
        Asserts.assertGreaterThan(after, snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getStartTime(), snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getDuration(), Duration.ZERO);
        assertStaticOptions(snapshot);
        Asserts.assertEquals(snapshot.getStream(null, null), null);
    }
}
 
Example 6
Source File: TestStartDelayRunning.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.out.println("Test started at " + Instant.now());
    Recording r = StartupHelper.getRecording("TestStartDelay");
    CommonHelper.waitForRecordingState(r, RecordingState.RUNNING);
    System.out.println("Recording startTime = " + r.getStartTime());
    Asserts.assertNotNull(r.getStartTime(), "StartTime was null after delay");
    Asserts.assertGreaterThan(Instant.now(), r.getStartTime(), "Current time should exceed start time");
    r.stop();
    r.close();
}
 
Example 7
Source File: HeapSummaryEventAllGcs.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static int checkGcId(RecordedEvent event, int currGcId) {
    int gcId = Events.assertField(event, "gcId").getValue();
    String when = Events.assertField(event, "when").notEmpty().getValue();
    if ("Before GC".equals(when)) {
        Asserts.assertGreaterThan(gcId, currGcId, "gcId should be increasing");
    } else {
        Asserts.assertEquals(gcId, currGcId, "After should have same gcId as last Before event");
    }
    return gcId;
}
 
Example 8
Source File: HeapSummaryEventAllGcs.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static int checkGcId(RecordedEvent event, int currGcId) {
    int gcId = Events.assertField(event, "gcId").getValue();
    String when = Events.assertField(event, "when").notEmpty().getValue();
    if ("Before GC".equals(when)) {
        Asserts.assertGreaterThan(gcId, currGcId, "gcId should be increasing");
    } else {
        Asserts.assertEquals(gcId, currGcId, "After should have same gcId as last Before event");
    }
    return gcId;
}
 
Example 9
Source File: FileHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void verifyRecording(File file) throws Exception {
    Asserts.assertTrue(file.exists(), file.getAbsolutePath() + " does not exist");
    Asserts.assertTrue(file.isFile(), file.getAbsolutePath() + " is not a file");
    Asserts.assertGreaterThan(file.length(), 0L, "Size of recording is 0.");
    List<RecordedEvent> events = RecordingFile.readAllEvents(file.toPath());
    for (RecordedEvent event : events) {
        System.out.printf("First event in recording '%s':%n%s", file.getName(), event);
        return;
    }
    Asserts.fail("No events in file " + file.getName());
}
 
Example 10
Source File: TestGetStackTraceId.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void assertValid(long value) {
    Asserts.assertGreaterThan(value, 0L, "Stack trace id must be greater than 0, was " + value);
}
 
Example 11
Source File: TestRecordedClassLoader.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withoutStackTrace();
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isDefined = false;
    System.out.println(events);
    for (RecordedEvent event : events) {
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            System.out.println(event);

            // get the RecordedClassLoader from the RecordedClass, the "definedClass"
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");

            // invoke RecordedClassLoader.getType() in order to validate the type of the RecordedClassLoader
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");

            // verify matching types
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());

            // get a RecordedClassLoader directly from the "definingClassLoader" field as well
            RecordedClassLoader definingClassLoaderFromField = event.getValue("definingClassLoader");
            Asserts.assertNotNull(definingClassLoaderFromField,
                "Defining Class Loader instantatiated from field should not be null");

            // ensure that the class loader instance used in the test actually has a name
            /** unsupport now
            Asserts.assertNotNull(cl.getName(),
                "Expected a valid name for the TestClassLoader");

            // invoke RecordedClassLoader.getName() to get the name of the class loader instance
            Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
                "Defining Class Loader should have the same name as the original class loader");
            Asserts.assertEquals(definingClassLoaderFromField.getName(), definingClassLoader.getName(),
                "Defining Class Loader representations should have the same class loader name");
            */

            // invoke uniqueID()
            Asserts.assertGreaterThan(definingClassLoader.getId(), 0L, "Invalid id assignment");

            // second order class loader information ("check class loader of the class loader")
            RecordedClassLoader classLoaderOfDefClassLoader = definingClassLoaderType.getClassLoader();
            Asserts.assertNotNull(classLoaderOfDefClassLoader,
                "The class loader for the definining class loader should not be null");
            /** unsupport now
            Asserts.assertEquals(cl.getClass().getClassLoader().getName(), classLoaderOfDefClassLoader.getName(),
                "Expected class loader name " + cl.getClass().getClassLoader().getName() + ", got name " + classLoaderOfDefClassLoader.getName());
            */

            RecordedClass classLoaderOfDefClassLoaderType = classLoaderOfDefClassLoader.getType();
            Asserts.assertNotNull(classLoaderOfDefClassLoaderType,
                "The class loader type for the defining class loader should not be null");
            Asserts.assertEquals(cl.getClass().getClassLoader().getClass().getName(), classLoaderOfDefClassLoaderType.getName(),
                "Expected type " + cl.getClass().getClassLoader().getClass().getName() + ", got type " + classLoaderOfDefClassLoaderType.getName());

            Asserts.assertGreaterThan(definingClassLoader.getId(), classLoaderOfDefClassLoader.getId(),
                "expected id assignment invariant broken for Class Loaders");

            isDefined = true;
        }
    }
    Asserts.assertTrue(isDefined, "No class define event found to verify RecordedClassLoader");
}
 
Example 12
Source File: TestPeriod.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void verifyDelta(long actual, long expected) {
    System.out.printf("verifyDelta: actaul=%d, expected=%d (errorMargin=%d)%n", actual, expected, ERROR_MARGIN);
    Asserts.assertGreaterThan(actual, expected - ERROR_MARGIN, "period delta too short");
}
 
Example 13
Source File: TestEvacuationInfoEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Throwable {
    final long g1HeapRegionSize = 1024 * 1024;
    Recording recording = new Recording();
    recording.enable(EVENT_INFO_NAME).withThreshold(Duration.ofMillis(0));
    recording.enable(EVENT_FAILED_NAME).withThreshold(Duration.ofMillis(0));
    recording.start();
    allocate();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    Asserts.assertFalse(events.isEmpty(), "No events found");
    for (RecordedEvent event : events) {
        if (!Events.isEventType(event, EVENT_INFO_NAME)) {
            continue;
        }
        System.out.println("Event: " + event);

        int setRegions = Events.assertField(event, "cSetRegions").atLeast(0).getValue();
        long setUsedAfter = Events.assertField(event, "cSetUsedAfter").atLeast(0L).getValue();
        long setUsedBefore = Events.assertField(event, "cSetUsedBefore").atLeast(setUsedAfter).getValue();
        int allocationRegions = Events.assertField(event, "allocationRegions").atLeast(0).getValue();
        long allocRegionsUsedBefore = Events.assertField(event, "allocRegionsUsedBefore").atLeast(0L).getValue();
        long allocRegionsUsedAfter = Events.assertField(event, "allocRegionsUsedAfter").atLeast(0L).getValue();
        long bytesCopied = Events.assertField(event, "bytesCopied").atLeast(0L).getValue();
        int regionsFreed = Events.assertField(event, "regionsFreed").atLeast(0).getValue();

        Asserts.assertEquals(allocRegionsUsedBefore + bytesCopied, allocRegionsUsedAfter, "allocRegionsUsedBefore + bytesCopied = allocRegionsUsedAfter");
        Asserts.assertGreaterThanOrEqual(setRegions, regionsFreed, "setRegions >= regionsFreed");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * allocationRegions, allocRegionsUsedAfter, "G1HeapRegionSize * allocationRegions >= allocationRegionsUsedAfter");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * setRegions, setUsedAfter, "G1HeapRegionSize * setRegions >= setUsedAfter");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * setRegions, setUsedBefore, "G1HeapRegionSize * setRegions >= setUsedBefore");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize, allocRegionsUsedBefore, "G1HeapRegionSize >= allocRegionsUsedBefore");

        int gcId = Events.assertField(event, "gcId").getValue();
        boolean isEvacuationFailed = containsEvacuationFailed(events, gcId);
        if (isEvacuationFailed) {
            Asserts.assertGreaterThan(setUsedAfter, 0L, "EvacuationFailure -> setUsedAfter > 0");
            Asserts.assertGreaterThan(setRegions, regionsFreed, "EvacuationFailure -> setRegions > regionsFreed");
        } else {
            Asserts.assertEquals(setUsedAfter, 0L, "No EvacuationFailure -> setUsedAfter = 0");
            Asserts.assertEquals(setRegions, regionsFreed, "No EvacuationFailure -> setRegions = regionsFreed");
        }
    }
}
 
Example 14
Source File: TestThreadCpuTimeEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void testCompareWithMXBean() throws Throwable {
    Recording recording = new Recording();

    recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofMillis(eventPeriodMillis));
    recording.start();

    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);

    // Run a single pass
    thread.start();
    barrier.await();
    barrier.await();

    recording.stop();
    List<RecordedEvent> beforeEvents = Events.fromRecording(recording);

    verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);

    // Run a second single pass
    barrier.await();
    barrier.await();

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    Duration cpuTime = Duration.ofNanos(bean.getThreadCpuTime(thread.getId()));
    Duration userTime = Duration.ofNanos(bean.getThreadUserTime(thread.getId()));

    // Check something that should hold even in the presence of unfortunate scheduling
    Asserts.assertGreaterThanOrEqual(cpuTime.toMillis(), eventPeriodMillis);
    Asserts.assertGreaterThanOrEqual(userTime.toMillis(), eventPeriodMillis);

    Duration systemTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "system");
    Duration userTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "user");
    Duration cpuTimeBefore = userTimeBefore.plus(systemTimeBefore);

    Asserts.assertLessThan(cpuTimeBefore, cpuTime);
    Asserts.assertLessThan(userTimeBefore, userTime);
    Asserts.assertGreaterThan(cpuTimeBefore, Duration.ZERO);

    thread.interrupt();
    thread.join();
}