|
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.util.concurrent.CompletableFuture; |
|
29 import java.util.concurrent.CountDownLatch; |
|
30 import java.util.concurrent.atomic.AtomicLong; |
|
31 |
|
32 import jdk.jfr.Event; |
|
33 import jdk.jfr.consumer.RecordingStream; |
|
34 |
|
35 /** |
|
36 * @test |
|
37 * @summary Tests RecordingStream::close() |
|
38 * @key jfr |
|
39 * @requires vm.hasJFR |
|
40 * @library /test/lib |
|
41 * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestClose |
|
42 */ |
|
43 public class TestClose { |
|
44 |
|
45 private static class CloseEvent extends Event { |
|
46 } |
|
47 |
|
48 public static void main(String... args) throws Exception { |
|
49 testCloseUnstarted(); |
|
50 testCloseStarted(); |
|
51 testCloseTwice(); |
|
52 testCloseStreaming(); |
|
53 testCloseMySelf(); |
|
54 } |
|
55 |
|
56 private static void testCloseMySelf() throws Exception { |
|
57 CountDownLatch l1 = new CountDownLatch(1); |
|
58 CountDownLatch l2 = new CountDownLatch(1); |
|
59 RecordingStream r = new RecordingStream(); |
|
60 r.onEvent(e -> { |
|
61 try { |
|
62 l1.await(); |
|
63 r.close(); |
|
64 l2.countDown(); |
|
65 } catch (InterruptedException ie) { |
|
66 throw new Error(ie); |
|
67 } |
|
68 }); |
|
69 r.startAsync(); |
|
70 CloseEvent c = new CloseEvent(); |
|
71 c.commit(); |
|
72 l1.countDown(); |
|
73 l2.await(); |
|
74 } |
|
75 |
|
76 private static void testCloseStreaming() throws Exception { |
|
77 CountDownLatch streaming = new CountDownLatch(1); |
|
78 RecordingStream r = new RecordingStream(); |
|
79 AtomicLong count = new AtomicLong(); |
|
80 r.onEvent(e -> { |
|
81 if (count.incrementAndGet() == 100) { |
|
82 streaming.countDown(); |
|
83 } |
|
84 }); |
|
85 r.startAsync(); |
|
86 var streamingLoop = CompletableFuture.runAsync(() -> { |
|
87 while (true) { |
|
88 CloseEvent c = new CloseEvent(); |
|
89 c.commit(); |
|
90 } |
|
91 }); |
|
92 streaming.await(); |
|
93 r.close(); |
|
94 streamingLoop.cancel(true); |
|
95 } |
|
96 |
|
97 private static void testCloseStarted() { |
|
98 RecordingStream r = new RecordingStream(); |
|
99 r.startAsync(); |
|
100 r.close(); |
|
101 } |
|
102 |
|
103 private static void testCloseUnstarted() { |
|
104 RecordingStream r = new RecordingStream(); |
|
105 r.close(); |
|
106 } |
|
107 |
|
108 private static void testCloseTwice() { |
|
109 RecordingStream r = new RecordingStream(); |
|
110 r.startAsync(); |
|
111 r.close(); |
|
112 r.close(); |
|
113 } |
|
114 |
|
115 } |