|
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.streaming; |
|
27 |
|
28 import java.util.concurrent.CountDownLatch; |
|
29 import java.util.concurrent.atomic.AtomicBoolean; |
|
30 |
|
31 import jdk.jfr.Event; |
|
32 import jdk.jfr.Recording; |
|
33 import jdk.jfr.consumer.RecordingStream; |
|
34 |
|
35 /** |
|
36 * @test |
|
37 * @summary Verifies that it is possible to start a stream when there are |
|
38 * already chunk in the repository |
|
39 * @key jfr |
|
40 * @requires vm.hasJFR |
|
41 * @library /test/lib |
|
42 * @run main/othervm jdk.jfr.api.consumer.streaming.TestRecordingBefore |
|
43 */ |
|
44 public class TestRecordingBefore { |
|
45 |
|
46 static class SnakeEvent extends Event { |
|
47 int id; |
|
48 } |
|
49 |
|
50 public static void main(String... args) throws Exception { |
|
51 |
|
52 try (Recording r1 = new Recording()) { |
|
53 r1.start(); |
|
54 emitSnakeEvent(1); |
|
55 emitSnakeEvent(2); |
|
56 emitSnakeEvent(3); |
|
57 // Force a chunk rotation |
|
58 try (Recording r2 = new Recording()) { |
|
59 r2.start(); |
|
60 emitSnakeEvent(4); |
|
61 emitSnakeEvent(5); |
|
62 emitSnakeEvent(6); |
|
63 r2.stop(); |
|
64 } |
|
65 r1.stop(); |
|
66 // Two chunks should now exist in the repository |
|
67 AtomicBoolean fail = new AtomicBoolean(false); |
|
68 CountDownLatch lastEvent = new CountDownLatch(1); |
|
69 try (RecordingStream rs = new RecordingStream()) { |
|
70 rs.onEvent(e -> { |
|
71 long id = e.getLong("id"); |
|
72 if (id < 7) { |
|
73 System.out.println("Found unexpected id " + id); |
|
74 fail.set(true); |
|
75 } |
|
76 if (id == 9) { |
|
77 lastEvent.countDown(); |
|
78 } |
|
79 }); |
|
80 rs.startAsync(); |
|
81 emitSnakeEvent(7); |
|
82 emitSnakeEvent(8); |
|
83 emitSnakeEvent(9); |
|
84 lastEvent.await(); |
|
85 if (fail.get()) { |
|
86 throw new Exception("Found events from a previous recording"); |
|
87 } |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 static void emitSnakeEvent(int id) { |
|
93 SnakeEvent e = new SnakeEvent(); |
|
94 e.id = id; |
|
95 e.commit(); |
|
96 } |
|
97 |
|
98 } |