|
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.time.Instant; |
|
29 import java.util.concurrent.CountDownLatch; |
|
30 import java.util.concurrent.atomic.AtomicInteger; |
|
31 |
|
32 import jdk.jfr.Event; |
|
33 import jdk.jfr.Recording; |
|
34 import jdk.jfr.consumer.EventStream; |
|
35 |
|
36 /** |
|
37 * @test |
|
38 * @summary Tests that a stream can gracefully handle chunk being removed in the |
|
39 * middle |
|
40 * @key jfr |
|
41 * @requires vm.hasJFR |
|
42 * @library /test/lib |
|
43 * @run main/othervm jdk.jfr.api.consumer.streaming.TestFilledChunks |
|
44 */ |
|
45 public class TestChunkGap { |
|
46 |
|
47 static class StartEvent extends Event { |
|
48 } |
|
49 |
|
50 static class TestGapEvent extends Event { |
|
51 } |
|
52 |
|
53 static class EndEvent extends Event { |
|
54 } |
|
55 |
|
56 private final static AtomicInteger count = new AtomicInteger(0); |
|
57 |
|
58 public static void main(String... args) throws Exception { |
|
59 |
|
60 CountDownLatch gap = new CountDownLatch(1); |
|
61 CountDownLatch receivedEvent = new CountDownLatch(1); |
|
62 |
|
63 try (EventStream s = EventStream.openRepository()) { |
|
64 try (Recording r1 = new Recording()) { |
|
65 s.setStartTime(Instant.EPOCH); |
|
66 s.onEvent(e -> { |
|
67 System.out.println(e); |
|
68 receivedEvent.countDown(); |
|
69 try { |
|
70 gap.await(); |
|
71 } catch (InterruptedException e1) { |
|
72 e1.printStackTrace(); |
|
73 } |
|
74 count.incrementAndGet(); |
|
75 if (e.getEventType().getName().equals(EndEvent.class.getName())) { |
|
76 s.close(); |
|
77 } |
|
78 }); |
|
79 s.startAsync(); |
|
80 r1.start(); |
|
81 StartEvent event1 = new StartEvent(); |
|
82 event1.commit(); |
|
83 receivedEvent.await(); |
|
84 r1.stop(); |
|
85 |
|
86 // create chunk that is removed |
|
87 try (Recording r2 = new Recording()) { |
|
88 r2.enable(TestGapEvent.class); |
|
89 r2.start(); |
|
90 TestGapEvent event2 = new TestGapEvent(); |
|
91 event2.commit(); |
|
92 r2.stop(); |
|
93 } |
|
94 gap.countDown(); |
|
95 try (Recording r3 = new Recording()) { |
|
96 r3.enable(EndEvent.class); |
|
97 r3.start(); |
|
98 EndEvent event3 = new EndEvent(); |
|
99 event3.commit(); |
|
100 r3.stop(); |
|
101 |
|
102 s.awaitTermination(); |
|
103 if (count.get() != 2) { |
|
104 throw new AssertionError("Expected 2 event, but got " + count); |
|
105 } |
|
106 } |
|
107 } |
|
108 } |
|
109 } |
|
110 |
|
111 } |