29 import java.nio.file.Files; |
29 import java.nio.file.Files; |
30 import java.nio.file.Path; |
30 import java.nio.file.Path; |
31 import java.util.IdentityHashMap; |
31 import java.util.IdentityHashMap; |
32 import java.util.Map; |
32 import java.util.Map; |
33 import java.util.concurrent.atomic.AtomicBoolean; |
33 import java.util.concurrent.atomic.AtomicBoolean; |
34 import java.util.concurrent.atomic.AtomicReference; |
|
35 |
34 |
36 import jdk.jfr.Event; |
35 import jdk.jfr.Event; |
37 import jdk.jfr.Recording; |
36 import jdk.jfr.Recording; |
38 import jdk.jfr.consumer.EventStream; |
37 import jdk.jfr.consumer.EventStream; |
39 import jdk.jfr.consumer.RecordedEvent; |
38 import jdk.jfr.consumer.RecordedEvent; |
49 public class TestReuse { |
48 public class TestReuse { |
50 |
49 |
51 static class ReuseEvent extends Event { |
50 static class ReuseEvent extends Event { |
52 } |
51 } |
53 |
52 |
|
53 private static final boolean[] BOOLEAN_STATES = { false, true }; |
|
54 |
54 public static void main(String... args) throws Exception { |
55 public static void main(String... args) throws Exception { |
55 Path p = makeRecording(); |
56 Path p = makeRecording(); |
56 |
57 |
57 testSetReuseTrue(p); |
58 testSetReuseTrue(p); |
58 testSetReuseFalse(p); |
59 testSetReuseFalse(p); |
59 } |
60 } |
60 |
61 |
61 private static void testSetReuseFalse(Path p) throws Exception { |
62 private static void testSetReuseFalse(Path p) throws Exception { |
62 AtomicBoolean fail = new AtomicBoolean(false); |
63 for (boolean ordered : BOOLEAN_STATES) { |
63 Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>(); |
64 AtomicBoolean fail = new AtomicBoolean(false); |
64 try (EventStream es = EventStream.openFile(p)) { |
65 Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>(); |
65 es.setReuse(false); |
66 try (EventStream es = EventStream.openFile(p)) { |
66 es.onEvent(e -> { |
67 es.setOrdered(ordered); |
67 if (identity.containsKey(e)) { |
68 es.setReuse(false); |
68 fail.set(true); |
69 es.onEvent(e -> { |
69 es.close(); |
70 if (identity.containsKey(e)) { |
70 } |
71 fail.set(true); |
71 identity.put(e, e); |
72 es.close(); |
72 }); |
73 } |
73 es.start(); |
74 identity.put(e, e); |
74 } |
75 }); |
75 if (fail.get()) { |
76 es.start(); |
76 throw new Exception("Unexpected reuse!"); |
77 } |
|
78 if (fail.get()) { |
|
79 throw new Exception("Unexpected reuse! Ordered = " + ordered); |
|
80 } |
|
81 |
77 } |
82 } |
78 } |
83 } |
79 |
84 |
80 private static void testSetReuseTrue(Path p) throws Exception { |
85 private static void testSetReuseTrue(Path p) throws Exception { |
81 AtomicBoolean fail = new AtomicBoolean(false); |
86 for (boolean ordered : BOOLEAN_STATES) { |
82 AtomicReference<RecordedEvent> event = new AtomicReference<RecordedEvent>(null); |
87 AtomicBoolean success = new AtomicBoolean(false); |
83 try (EventStream es = EventStream.openFile(p)) { |
88 Map<RecordedEvent, RecordedEvent> events = new IdentityHashMap<>(); |
84 es.setReuse(true); |
89 try (EventStream es = EventStream.openFile(p)) { |
85 es.onEvent(e -> { |
90 es.setOrdered(ordered); |
86 if (event.get() == null) { |
91 es.setReuse(true); |
87 event.set(e); |
92 es.onEvent(e -> { |
88 } else { |
93 if(events.containsKey(e)) { |
89 if (e != event.get()) { |
94 success.set(true);; |
90 fail.set(true); |
|
91 es.close(); |
95 es.close(); |
92 } |
96 } |
93 } |
97 events.put(e,e); |
94 }); |
98 }); |
95 es.start(); |
99 es.start(); |
|
100 } |
|
101 if (!success.get()) { |
|
102 throw new Exception("No reuse! Ordered = " + ordered); |
|
103 } |
96 } |
104 } |
97 if (fail.get()) { |
105 |
98 throw new Exception("No reuse!"); |
|
99 } |
|
100 } |
106 } |
101 |
107 |
102 private static Path makeRecording() throws IOException { |
108 private static Path makeRecording() throws IOException { |
103 try (Recording r = new Recording()) { |
109 try (Recording r = new Recording()) { |
104 r.start(); |
110 r.start(); |
105 for (int i = 0; i < 1_000; i++) { |
111 for (int i = 0; i < 5; i++) { |
|
112 ReuseEvent e = new ReuseEvent(); |
|
113 e.commit(); |
|
114 } |
|
115 Recording rotation = new Recording(); |
|
116 rotation.start(); |
|
117 for (int i = 0; i < 5; i++) { |
106 ReuseEvent e = new ReuseEvent(); |
118 ReuseEvent e = new ReuseEvent(); |
107 e.commit(); |
119 e.commit(); |
108 } |
120 } |
109 r.stop(); |
121 r.stop(); |
|
122 rotation.close(); |
110 Path p = Files.createTempFile("recording", ".jfr"); |
123 Path p = Files.createTempFile("recording", ".jfr"); |
111 r.dump(p); |
124 r.dump(p); |
112 return p; |
125 return p; |
113 } |
126 } |
114 } |
127 } |