test/jdk/jdk/jfr/api/consumer/filestream/TestReuse.java
branchJEP-349-branch
changeset 57378 2e35a025ebee
parent 57377 528d85d2fce5
child 57380 6a7e7743b82f
equal deleted inserted replaced
57377:528d85d2fce5 57378:2e35a025ebee
    26 package jdk.jfr.api.consumer.filestream;
    26 package jdk.jfr.api.consumer.filestream;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    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.HashMap;
       
    32 import java.util.IdentityHashMap;
    31 import java.util.IdentityHashMap;
    33 import java.util.Map;
    32 import java.util.Map;
       
    33 import java.util.concurrent.atomic.AtomicBoolean;
    34 
    34 
    35 import jdk.jfr.Event;
    35 import jdk.jfr.Event;
    36 import jdk.jfr.Recording;
    36 import jdk.jfr.Recording;
    37 import jdk.jfr.consumer.EventStream;
    37 import jdk.jfr.consumer.EventStream;
    38 import jdk.jfr.consumer.RecordedEvent;
    38 import jdk.jfr.consumer.RecordedEvent;
    55 
    55 
    56         testSetReuseTrue(p);
    56         testSetReuseTrue(p);
    57         testSetReuseFalse(p);
    57         testSetReuseFalse(p);
    58     }
    58     }
    59 
    59 
    60     private static void testSetReuseFalse(Path p) throws IOException {
    60     private static void testSetReuseFalse(Path p) throws Exception {
       
    61         AtomicBoolean fail = new AtomicBoolean(false);
    61         Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>();
    62         Map<RecordedEvent, RecordedEvent> identity = new IdentityHashMap<>();
    62         try (EventStream es = EventStream.openFile(p)) {
    63         try (EventStream es = EventStream.openFile(p)) {
    63             es.setReuse(false);
    64             es.setReuse(false);
    64             es.onEvent(e -> {
    65             es.onEvent(e -> {
    65                 if (identity.containsKey(e)) {
    66                 if (identity.containsKey(e)) {
       
    67                     fail.set(true);
    66                     throw new Error("Unexpected reuse!");
    68                     throw new Error("Unexpected reuse!");
    67                 }
    69                 }
    68                 identity.put(e,e);
    70                 identity.put(e,e);
    69             });
    71             });
    70             es.start();
    72             es.start();
    71         }
    73         }
       
    74         if (fail.get()) {
       
    75             throw new Exception("Unexpected resued");
       
    76         }
    72     }
    77     }
    73 
    78 
    74     private static void testSetReuseTrue(Path p) throws IOException {
    79     private static void testSetReuseTrue(Path p) throws Exception {
       
    80         AtomicBoolean fail = new AtomicBoolean(false);
    75         try (EventStream es = EventStream.openFile(p)) {
    81         try (EventStream es = EventStream.openFile(p)) {
    76             es.setReuse(true);
    82             es.setReuse(true);
    77             RecordedEvent[] events = new RecordedEvent[1];
    83             RecordedEvent[] events = new RecordedEvent[1];
    78             es.onEvent(e -> {
    84             es.onEvent(e -> {
    79                 if (events[0] == null) {
    85                 if (events[0] == null) {
    80                     events[0] = e;
    86                     events[0] = e;
    81                 } else {
    87                 } else {
    82                     if (e != events[0]) {
    88                     if (e != events[0]) {
       
    89                         fail.set(true);
    83                         throw new Error("No reuse");
    90                         throw new Error("No reuse");
    84                     }
    91                     }
    85                 }
    92                 }
    86             });
    93             });
    87             es.start();
    94             es.start();
       
    95         }
       
    96         if (fail.get()) {
       
    97             throw new Exception("No reuse");
    88         }
    98         }
    89     }
    99     }
    90 
   100 
    91     private static Path makeRecording() throws IOException {
   101     private static Path makeRecording() throws IOException {
    92         try (Recording r = new Recording()) {
   102         try (Recording r = new Recording()) {