author | egahlin |
Fri, 31 May 2019 20:44:28 +0200 | |
branch | JEP-349-branch |
changeset 57385 | 7d9d4f629f6e |
parent 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; |
57377 | 34 |
|
35 |
import jdk.jfr.Event; |
|
36 |
import jdk.jfr.Recording; |
|
37 |
import jdk.jfr.consumer.EventStream; |
|
38 |
import jdk.jfr.consumer.RecordedEvent; |
|
39 |
||
40 |
/** |
|
41 |
* @test |
|
42 |
* @summary Test EventStream::setReuse(...) |
|
43 |
* @key jfr |
|
44 |
* @requires vm.hasJFR |
|
45 |
* @library /test/lib |
|
46 |
* @run main/othervm jdk.jfr.api.consumer.filestream.TestReuse |
|
47 |
*/ |
|
48 |
public class TestReuse { |
|
49 |
||
50 |
static class ReuseEvent extends Event { |
|
51 |
} |
|
52 |
||
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
53 |
private static final boolean[] BOOLEAN_STATES = { false, true }; |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
54 |
|
57377 | 55 |
public static void main(String... args) throws Exception { |
56 |
Path p = makeRecording(); |
|
57 |
||
58 |
testSetReuseTrue(p); |
|
59 |
testSetReuseFalse(p); |
|
60 |
} |
|
61 |
||
57378 | 62 |
private static void testSetReuseFalse(Path p) throws Exception { |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
63 |
for (boolean ordered : BOOLEAN_STATES) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
64 |
AtomicBoolean fail = new AtomicBoolean(false); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
65 |
Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
66 |
try (EventStream es = EventStream.openFile(p)) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
67 |
es.setOrdered(ordered); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
68 |
es.setReuse(false); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
69 |
es.onEvent(e -> { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
70 |
if (identity.containsKey(e)) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
71 |
fail.set(true); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
72 |
es.close(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
73 |
} |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
74 |
identity.put(e, e); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
75 |
}); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
76 |
es.start(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
77 |
} |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
78 |
if (fail.get()) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
79 |
throw new Exception("Unexpected reuse! Ordered = " + ordered); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
80 |
} |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
81 |
|
57378 | 82 |
} |
57377 | 83 |
} |
84 |
||
57378 | 85 |
private static void testSetReuseTrue(Path p) throws Exception { |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
86 |
for (boolean ordered : BOOLEAN_STATES) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
87 |
AtomicBoolean success = new AtomicBoolean(false); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
88 |
Map<RecordedEvent, RecordedEvent> events = new IdentityHashMap<>(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
89 |
try (EventStream es = EventStream.openFile(p)) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
90 |
es.setOrdered(ordered); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
91 |
es.setReuse(true); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
92 |
es.onEvent(e -> { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
93 |
if(events.containsKey(e)) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
94 |
success.set(true);; |
57381 | 95 |
es.close(); |
57377 | 96 |
} |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
97 |
events.put(e,e); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
98 |
}); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
99 |
es.start(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
100 |
} |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
101 |
if (!success.get()) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
102 |
throw new Exception("No reuse! Ordered = " + ordered); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
103 |
} |
57377 | 104 |
} |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
105 |
|
57377 | 106 |
} |
107 |
||
108 |
private static Path makeRecording() throws IOException { |
|
109 |
try (Recording r = new Recording()) { |
|
110 |
r.start(); |
|
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
111 |
for (int i = 0; i < 5; i++) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
112 |
ReuseEvent e = new ReuseEvent(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
113 |
e.commit(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
114 |
} |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
115 |
Recording rotation = new Recording(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
116 |
rotation.start(); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
117 |
for (int i = 0; i < 5; i++) { |
57377 | 118 |
ReuseEvent e = new ReuseEvent(); |
119 |
e.commit(); |
|
120 |
} |
|
121 |
r.stop(); |
|
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57381
diff
changeset
|
122 |
rotation.close(); |
57377 | 123 |
Path p = Files.createTempFile("recording", ".jfr"); |
124 |
r.dump(p); |
|
125 |
return p; |
|
126 |
} |
|
127 |
} |
|
128 |
} |