test/jdk/jdk/jfr/api/consumer/streaming/TestJVMCrash.java
author egahlin
Fri, 22 Nov 2019 17:20:43 +0100
changeset 59226 a0f39cc47387
child 59246 fcad92f425c5
permissions -rw-r--r--
8233700: EventStream not closed Reviewed-by: mgronlun, mseledtsov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59226
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     1
/*
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     4
 *
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    10
 *
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    15
 * accompanied this code).
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    16
 *
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    20
 *
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    23
 * questions.
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    24
 */
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    25
package jdk.jfr.api.consumer.streaming;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    26
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    27
import java.time.Duration;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    28
import java.time.Instant;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicInteger;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    30
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    31
import jdk.jfr.consumer.EventStream;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    32
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    33
/**
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    34
 * @test
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    35
 * @summary Test that a stream ends/closes when an application crashes.
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    36
 * @key jfr
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    37
 * @requires vm.hasJFR
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    38
 * @library /test/lib /test/jdk
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    39
 * @modules jdk.jfr jdk.attach java.base/jdk.internal.misc
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    40
 *
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    41
 * @run main/othervm jdk.jfr.api.consumer.streaming.TestJVMCrash
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    42
 */
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    43
public class TestJVMCrash {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    44
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    45
    public static void main(String... args) throws Exception {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    46
        int id = 1;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    47
        while (true) {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    48
            TestProcess process = new TestProcess("crash-application-" + id++);
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    49
            AtomicInteger eventCounter = new AtomicInteger();
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    50
            try (EventStream es = EventStream.openRepository(process.getRepository())) {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    51
                // Start from first event in repository
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    52
                es.setStartTime(Instant.EPOCH);
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    53
                es.onEvent(e -> {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    54
                    if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    55
                        process.crash();
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    56
                    }
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    57
                });
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    58
                es.startAsync();
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    59
                // If crash corrupts chunk in repository, retry in 30 seconds
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    60
                es.awaitTermination(Duration.ofSeconds(30));
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    61
                if (eventCounter.get() == TestProcess.NUMBER_OF_EVENTS) {
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    62
                    return;
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    63
                }
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    64
                System.out.println("Incorrect event count. Retrying...");
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    65
            }
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    66
        }
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    67
    }
a0f39cc47387 8233700: EventStream not closed
egahlin
parents:
diff changeset
    68
}