author | egahlin |
Mon, 21 Oct 2019 20:09:06 +0200 | |
branch | JEP-349-branch |
changeset 58724 | 3d0a172353fc |
parent 58687 | 222f727e9b05 |
child 58774 | 141412e96b12 |
permissions | -rw-r--r-- |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
1 |
/* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
4 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
10 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
15 |
* accompanied this code). |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
16 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
20 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
23 |
* questions. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
24 |
*/ |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
25 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
26 |
package jdk.jfr.api.consumer.streaming; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
27 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
28 |
import java.util.concurrent.CountDownLatch; |
58687 | 29 |
import java.util.concurrent.atomic.AtomicBoolean; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
30 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
31 |
import jdk.jfr.Event; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
32 |
import jdk.jfr.Name; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
33 |
import jdk.jfr.Recording; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
34 |
import jdk.jfr.consumer.EventStream; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
35 |
import jdk.jfr.consumer.RecordingStream; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
36 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
37 |
/** |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
38 |
* @test |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
39 |
* @summary Verifies that a stream from a repository starts at the latest event |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
40 |
* @key jfr |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
41 |
* @requires vm.hasJFR |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
42 |
* @library /test/lib |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
43 |
* @run main/othervm jdk.jfr.api.consumer.streaming.TestLatestEvent |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
44 |
*/ |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
45 |
public class TestLatestEvent { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
46 |
|
58687 | 47 |
@Name("NotLatest") |
48 |
static class NotLatestEvent extends Event { |
|
49 |
} |
|
50 |
||
51 |
@Name("Latest") |
|
52 |
static class LatestEvent extends Event { |
|
53 |
} |
|
54 |
||
55 |
@Name("MakeChunks") |
|
56 |
static class MakeChunks extends Event { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
57 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
58 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
59 |
public static void main(String... args) throws Exception { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
60 |
try (RecordingStream r = new RecordingStream()) { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
61 |
r.startAsync(); |
58687 | 62 |
MakeChunks e = new MakeChunks(); |
63 |
e.commit(); |
|
64 |
CountDownLatch beginChunks = new CountDownLatch(1); |
|
65 |
r.onEvent("MakeChunks", event-> { |
|
66 |
beginChunks.countDown(); |
|
67 |
}); |
|
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
68 |
System.out.println("Waitning for first chunk"); |
58687 | 69 |
beginChunks.await(); |
70 |
// Create 5 chunks with events in the repository |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
71 |
for (int i = 0; i < 5; i++) { |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
72 |
System.out.println("Creating empty chunk"); |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
73 |
try (Recording r1 = new Recording()) { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
74 |
r1.start(); |
58687 | 75 |
NotLatestEvent notLatest = new NotLatestEvent(); |
76 |
notLatest.commit(); |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
77 |
r1.stop(); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
78 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
79 |
} |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
80 |
System.out.println("All empty chunks created"); |
58687 | 81 |
// Create an event in a segment, typically the first. |
82 |
NotLatestEvent notLatest = new NotLatestEvent(); |
|
83 |
notLatest.commit(); |
|
84 |
try (EventStream s = EventStream.openRepository()) { |
|
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
85 |
System.out.println("EventStream opened"); |
58687 | 86 |
awaitFlush(r); // ensure that NotLatest is included |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
87 |
s.startAsync(); |
58687 | 88 |
AtomicBoolean foundLatest = new AtomicBoolean(); |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
89 |
System.out.println("Added onEvent handler"); |
58687 | 90 |
s.onEvent(event -> { |
91 |
String name = event.getEventType().getName(); |
|
92 |
System.out.println("Found event " + name); |
|
93 |
foundLatest.set(name.equals("Latest")); |
|
94 |
s.close(); |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
95 |
}); |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
96 |
// Emit the latest event |
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
97 |
LatestEvent latest = new LatestEvent(); |
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
98 |
latest.commit(); |
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
99 |
System.out.println("Latest event emitted"); |
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
100 |
System.out.println("Waiting for termination"); |
58687 | 101 |
s.awaitTermination(); |
102 |
if (!foundLatest.get()) { |
|
103 |
throw new Exception("Didn't find latest event!"); |
|
104 |
} |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
105 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
106 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
107 |
} |
58687 | 108 |
|
109 |
private static void awaitFlush(RecordingStream r) throws InterruptedException { |
|
110 |
CountDownLatch latch = new CountDownLatch(1); |
|
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
111 |
System.out.println("Waiting for flush..."); |
58687 | 112 |
r.onFlush(() -> { |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
113 |
System.out.println("Flush arrived!"); |
58687 | 114 |
latch.countDown(); |
115 |
}); |
|
116 |
latch.await(); |
|
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
117 |
|
58687 | 118 |
} |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
119 |
} |