author | egahlin |
Thu, 30 May 2019 23:04:50 +0200 | |
branch | JEP-349-branch |
changeset 57380 | 6a7e7743b82f |
parent 57378 | 2e35a025ebee |
child 57381 | ce265e404c64 |
permissions | -rw-r--r-- |
57377 | 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.filestream; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.file.Files; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.util.IdentityHashMap; |
|
32 |
import java.util.Map; |
|
57378 | 33 |
import java.util.concurrent.atomic.AtomicBoolean; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
34 |
import java.util.concurrent.atomic.AtomicReference; |
57377 | 35 |
|
36 |
import jdk.jfr.Event; |
|
37 |
import jdk.jfr.Recording; |
|
38 |
import jdk.jfr.consumer.EventStream; |
|
39 |
import jdk.jfr.consumer.RecordedEvent; |
|
40 |
||
41 |
/** |
|
42 |
* @test |
|
43 |
* @summary Test EventStream::setReuse(...) |
|
44 |
* @key jfr |
|
45 |
* @requires vm.hasJFR |
|
46 |
* @library /test/lib |
|
47 |
* @run main/othervm jdk.jfr.api.consumer.filestream.TestReuse |
|
48 |
*/ |
|
49 |
public class TestReuse { |
|
50 |
||
51 |
static class ReuseEvent extends Event { |
|
52 |
} |
|
53 |
||
54 |
public static void main(String... args) throws Exception { |
|
55 |
Path p = makeRecording(); |
|
56 |
||
57 |
testSetReuseTrue(p); |
|
58 |
testSetReuseFalse(p); |
|
59 |
} |
|
60 |
||
57378 | 61 |
private static void testSetReuseFalse(Path p) throws Exception { |
62 |
AtomicBoolean fail = new AtomicBoolean(false); |
|
57377 | 63 |
Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>(); |
64 |
try (EventStream es = EventStream.openFile(p)) { |
|
65 |
es.setReuse(false); |
|
66 |
es.onEvent(e -> { |
|
67 |
if (identity.containsKey(e)) { |
|
57378 | 68 |
fail.set(true); |
57377 | 69 |
throw new Error("Unexpected reuse!"); |
70 |
} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
71 |
identity.put(e, e); |
57377 | 72 |
}); |
73 |
es.start(); |
|
74 |
} |
|
57378 | 75 |
if (fail.get()) { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
76 |
throw new Exception("Unexpected reuse!"); |
57378 | 77 |
} |
57377 | 78 |
} |
79 |
||
57378 | 80 |
private static void testSetReuseTrue(Path p) throws Exception { |
81 |
AtomicBoolean fail = new AtomicBoolean(false); |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
82 |
AtomicReference<RecordedEvent> event = new AtomicReference<RecordedEvent>(null); |
57377 | 83 |
try (EventStream es = EventStream.openFile(p)) { |
84 |
es.setReuse(true); |
|
85 |
es.onEvent(e -> { |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
86 |
if (event.get() == null) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
87 |
event.set(e); |
57377 | 88 |
} else { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
89 |
if (e != event.get()) { |
57378 | 90 |
fail.set(true); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
91 |
throw new Error("No reuse!"); |
57377 | 92 |
} |
93 |
} |
|
94 |
}); |
|
95 |
es.start(); |
|
96 |
} |
|
57378 | 97 |
if (fail.get()) { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57378
diff
changeset
|
98 |
throw new Exception("No reuse!"); |
57378 | 99 |
} |
57377 | 100 |
} |
101 |
||
102 |
private static Path makeRecording() throws IOException { |
|
103 |
try (Recording r = new Recording()) { |
|
104 |
r.start(); |
|
105 |
for (int i = 0; i < 2_000_000; i++) { |
|
106 |
ReuseEvent e = new ReuseEvent(); |
|
107 |
e.commit(); |
|
108 |
} |
|
109 |
r.stop(); |
|
110 |
Path p = Files.createTempFile("recording", ".jfr"); |
|
111 |
r.dump(p); |
|
112 |
return p; |
|
113 |
} |
|
114 |
} |
|
115 |
} |