test/jdk/jdk/jfr/api/consumer/filestream/TestReuse.java
branchJEP-349-branch
changeset 57380 6a7e7743b82f
parent 57378 2e35a025ebee
child 57381 ce265e404c64
equal deleted inserted replaced
57378:2e35a025ebee 57380:6a7e7743b82f
    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;
    34 
    35 
    35 import jdk.jfr.Event;
    36 import jdk.jfr.Event;
    36 import jdk.jfr.Recording;
    37 import jdk.jfr.Recording;
    37 import jdk.jfr.consumer.EventStream;
    38 import jdk.jfr.consumer.EventStream;
    38 import jdk.jfr.consumer.RecordedEvent;
    39 import jdk.jfr.consumer.RecordedEvent;
    65             es.onEvent(e -> {
    66             es.onEvent(e -> {
    66                 if (identity.containsKey(e)) {
    67                 if (identity.containsKey(e)) {
    67                     fail.set(true);
    68                     fail.set(true);
    68                     throw new Error("Unexpected reuse!");
    69                     throw new Error("Unexpected reuse!");
    69                 }
    70                 }
    70                 identity.put(e,e);
    71                 identity.put(e, e);
    71             });
    72             });
    72             es.start();
    73             es.start();
    73         }
    74         }
    74         if (fail.get()) {
    75         if (fail.get()) {
    75             throw new Exception("Unexpected resued");
    76             throw new Exception("Unexpected reuse!");
    76         }
    77         }
    77     }
    78     }
    78 
    79 
    79     private static void testSetReuseTrue(Path p) throws Exception {
    80     private static void testSetReuseTrue(Path p) throws Exception {
    80         AtomicBoolean fail = new AtomicBoolean(false);
    81         AtomicBoolean fail = new AtomicBoolean(false);
       
    82         AtomicReference<RecordedEvent> event = new AtomicReference<RecordedEvent>(null);
    81         try (EventStream es = EventStream.openFile(p)) {
    83         try (EventStream es = EventStream.openFile(p)) {
    82             es.setReuse(true);
    84             es.setReuse(true);
    83             RecordedEvent[] events = new RecordedEvent[1];
       
    84             es.onEvent(e -> {
    85             es.onEvent(e -> {
    85                 if (events[0] == null) {
    86                 if (event.get() == null) {
    86                     events[0] = e;
    87                     event.set(e);
    87                 } else {
    88                 } else {
    88                     if (e != events[0]) {
    89                     if (e != event.get()) {
    89                         fail.set(true);
    90                         fail.set(true);
    90                         throw new Error("No reuse");
    91                         throw new Error("No reuse!");
    91                     }
    92                     }
    92                 }
    93                 }
    93             });
    94             });
    94             es.start();
    95             es.start();
    95         }
    96         }
    96         if (fail.get()) {
    97         if (fail.get()) {
    97             throw new Exception("No reuse");
    98             throw new Exception("No reuse!");
    98         }
    99         }
    99     }
   100     }
   100 
   101 
   101     private static Path makeRecording() throws IOException {
   102     private static Path makeRecording() throws IOException {
   102         try (Recording r = new Recording()) {
   103         try (Recording r = new Recording()) {