|
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.api.recording.event; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.time.Duration; |
|
30 import java.time.Instant; |
|
31 import java.util.List; |
|
32 |
|
33 import jdk.jfr.FlightRecorder; |
|
34 import jdk.jfr.Recording; |
|
35 import jdk.jfr.consumer.RecordedEvent; |
|
36 import jdk.test.lib.Asserts; |
|
37 import jdk.test.lib.jfr.Events; |
|
38 import jdk.test.lib.jfr.SimpleEvent; |
|
39 |
|
40 /* |
|
41 * @test |
|
42 * @summary Test periodic setting that involves chunks. |
|
43 * @key jfr |
|
44 * @library /test/lib |
|
45 * @run main/othervm jdk.jfr.api.recording.event.TestChunkPeriod |
|
46 */ |
|
47 public class TestChunkPeriod { |
|
48 |
|
49 // Margin of error is to avoid issues where JFR and |
|
50 // System.currentMillis take the clock differently |
|
51 private static final Duration MARGIN_OF_ERROR = Duration.ofNanos(1_000_000_000); // 1 s |
|
52 |
|
53 public static void main(String[] args) throws Throwable { |
|
54 FlightRecorder.addPeriodicEvent(SimpleEvent.class, () -> { |
|
55 SimpleEvent pe = new SimpleEvent(); |
|
56 pe.commit(); |
|
57 }); |
|
58 testBeginChunk(); |
|
59 testEndChunk(); |
|
60 testEveryChunk(); |
|
61 } |
|
62 |
|
63 private static void testBeginChunk() throws IOException { |
|
64 Recording r = new Recording(); |
|
65 r.enable(SimpleEvent.class).with("period", "beginChunk"); |
|
66 Instant beforeStart = Instant.now().minus(MARGIN_OF_ERROR); |
|
67 r.start(); |
|
68 Instant afterStart = Instant.now().plus(MARGIN_OF_ERROR); |
|
69 r.stop(); |
|
70 List<RecordedEvent> events = Events.fromRecording(r); |
|
71 Asserts.assertEquals(events.size(), 1, "Expected one event with beginChunk"); |
|
72 RecordedEvent event = events.get(0); |
|
73 Asserts.assertGreaterThanOrEqual(event.getStartTime(), beforeStart); |
|
74 Asserts.assertGreaterThanOrEqual(afterStart, event.getStartTime()); |
|
75 r.close(); |
|
76 } |
|
77 |
|
78 private static void testEndChunk() throws IOException { |
|
79 Recording r = new Recording(); |
|
80 r.enable(SimpleEvent.class).with("period", "endChunk"); |
|
81 r.start(); |
|
82 Instant beforeStop = Instant.now().minus(MARGIN_OF_ERROR); |
|
83 r.stop(); |
|
84 Instant afterStop = Instant.now().plus(MARGIN_OF_ERROR); |
|
85 List<RecordedEvent> events = Events.fromRecording(r); |
|
86 Asserts.assertEquals(events.size(), 1, "Expected one event with endChunk"); |
|
87 RecordedEvent event = events.get(0); |
|
88 Asserts.assertGreaterThanOrEqual(event.getStartTime(), beforeStop); |
|
89 Asserts.assertGreaterThanOrEqual(afterStop, event.getStartTime()); |
|
90 r.close(); |
|
91 } |
|
92 |
|
93 private static void testEveryChunk() throws IOException { |
|
94 Recording r = new Recording(); |
|
95 r.enable(SimpleEvent.class).with("period", "everyChunk"); |
|
96 r.start(); |
|
97 r.stop(); |
|
98 List<RecordedEvent> events = Events.fromRecording(r); |
|
99 Asserts.assertEquals(events.size(), 2, "Expected two events with everyChunk"); |
|
100 r.close(); |
|
101 } |
|
102 } |