|
1 /* |
|
2 * Copyright (c) 2019, 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.consumer.recordingstream; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.time.Duration; |
|
30 import java.time.Instant; |
|
31 import java.util.concurrent.atomic.AtomicBoolean; |
|
32 |
|
33 import jdk.jfr.Event; |
|
34 import jdk.jfr.Name; |
|
35 import jdk.jfr.Recording; |
|
36 import jdk.jfr.StackTrace; |
|
37 import jdk.jfr.consumer.EventStream; |
|
38 import jdk.jfr.consumer.RecordingStream; |
|
39 |
|
40 /** |
|
41 * @test |
|
42 * @summary Tests EventStream::setStartTime |
|
43 * @key jfr |
|
44 * @requires vm.hasJFR |
|
45 * @library /test/lib |
|
46 * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestSetStartTime |
|
47 */ |
|
48 public final class TestSetStartTime { |
|
49 |
|
50 private final static int SLEEP_TIME_MS = 100; |
|
51 |
|
52 @Name("Mark") |
|
53 @StackTrace(false) |
|
54 public final static class Mark extends Event { |
|
55 public boolean before; |
|
56 } |
|
57 |
|
58 public static void main(String... args) throws Exception { |
|
59 testEventStream(); |
|
60 testRecordingStream(); |
|
61 } |
|
62 |
|
63 private static void testRecordingStream() throws InterruptedException { |
|
64 AtomicBoolean exit = new AtomicBoolean(); |
|
65 int attempt = 1; |
|
66 while (!exit.get()) { |
|
67 System.out.println("Testing RecordingStream:setStartTime(...). Attempt: " + attempt++); |
|
68 AtomicBoolean firstEvent = new AtomicBoolean(true); |
|
69 try (RecordingStream r2 = new RecordingStream()) { |
|
70 Instant t = Instant.now().plus(Duration.ofMillis(SLEEP_TIME_MS / 2)); |
|
71 System.out.println("Setting start time: " + t); |
|
72 r2.setStartTime(t); |
|
73 r2.onEvent(e -> { |
|
74 if (firstEvent.get()) { |
|
75 firstEvent.set(false); |
|
76 if (!e.getBoolean("before")) { |
|
77 // Skipped first event, let's exit |
|
78 exit.set(true); |
|
79 } |
|
80 r2.close(); |
|
81 } |
|
82 }); |
|
83 r2.startAsync(); |
|
84 Mark m1 = new Mark(); |
|
85 m1.before = true; |
|
86 m1.commit(); |
|
87 System.out.println("First event emitted: " + Instant.now()); |
|
88 Thread.sleep(SLEEP_TIME_MS); |
|
89 Mark m2 = new Mark(); |
|
90 m2.before = false; |
|
91 m2.commit(); |
|
92 System.out.println("Second event emitted: " + Instant.now()); |
|
93 r2.awaitTermination(); |
|
94 } |
|
95 System.out.println(); |
|
96 } |
|
97 } |
|
98 |
|
99 private static void testEventStream() throws InterruptedException, Exception, IOException { |
|
100 AtomicBoolean exit = new AtomicBoolean(); |
|
101 int attempt = 1; |
|
102 while (!exit.get()) { |
|
103 System.out.println("Testing EventStream:setStartTime(...). Attempt: " + attempt++); |
|
104 AtomicBoolean firstEvent = new AtomicBoolean(true); |
|
105 try (Recording r = new Recording()) { |
|
106 r.start(); |
|
107 Mark event1 = new Mark(); |
|
108 event1.before = true; |
|
109 event1.commit(); |
|
110 Instant t = Instant.now(); |
|
111 System.out.println("First event emitted: " + t); |
|
112 Thread.sleep(SLEEP_TIME_MS); |
|
113 Mark event2 = new Mark(); |
|
114 event2.before = false; |
|
115 event2.commit(); |
|
116 System.out.println("Second event emitted: " + Instant.now()); |
|
117 try (EventStream es = EventStream.openRepository()) { |
|
118 Instant startTime = t.plus(Duration.ofMillis(SLEEP_TIME_MS / 2)); |
|
119 es.setStartTime(startTime); |
|
120 System.out.println("Setting start time: " + startTime); |
|
121 es.onEvent(e -> { |
|
122 if (firstEvent.get()) { |
|
123 firstEvent.set(false); |
|
124 if (!e.getBoolean("before")) { |
|
125 // Skipped first event, let's exit |
|
126 exit.set(true); |
|
127 } |
|
128 es.close(); |
|
129 } |
|
130 }); |
|
131 es.startAsync(); |
|
132 es.awaitTermination(); |
|
133 } |
|
134 } |
|
135 System.out.println(); |
|
136 } |
|
137 } |
|
138 } |