|
1 package jdk.jfr.api.consumer.streaming; |
|
2 |
|
3 import java.nio.file.Files; |
|
4 import java.nio.file.Path; |
|
5 import java.nio.file.Paths; |
|
6 import java.time.Duration; |
|
7 import java.time.Instant; |
|
8 import java.util.concurrent.CountDownLatch; |
|
9 |
|
10 import jdk.jfr.Event; |
|
11 import jdk.jfr.Recording; |
|
12 import jdk.jfr.consumer.EventStream; |
|
13 import jdk.jfr.jcmd.JcmdHelper; |
|
14 |
|
15 /** |
|
16 * @test |
|
17 * @summary Verifies that is possible to stream from a repository that is being |
|
18 * moved. |
|
19 * @key jfr |
|
20 * @requires vm.hasJFR |
|
21 * @library /test/lib /test/jdk |
|
22 * @run main/othervm jdk.jfr.api.consumer.streaming.TestRepositoryMigration |
|
23 */ |
|
24 public class TestRepositoryMigration { |
|
25 static class MigrationEvent extends Event { |
|
26 int id; |
|
27 } |
|
28 |
|
29 public static void main(String... args) throws Exception { |
|
30 Path newRepository = Paths.get("new-repository"); |
|
31 CountDownLatch events = new CountDownLatch(2); |
|
32 try (EventStream es = EventStream.openRepository()) { |
|
33 es.setStartTime(Instant.EPOCH); |
|
34 es.onEvent(e -> { |
|
35 System.out.println(e); |
|
36 if (e.getInt("id") == 1) { |
|
37 events.countDown(); |
|
38 } |
|
39 if (e.getInt("id") == 2) { |
|
40 events.countDown(); |
|
41 } |
|
42 }); |
|
43 es.startAsync(); |
|
44 try (Recording r = new Recording()) { |
|
45 r.setFlushInterval(Duration.ofSeconds(1)); |
|
46 r.start(); |
|
47 // Chunk in default repository |
|
48 MigrationEvent e1 = new MigrationEvent(); |
|
49 e1.id = 1; |
|
50 e1.commit(); |
|
51 JcmdHelper.jcmd("JFR.configure", "repositorypath=" + newRepository.toAbsolutePath()); |
|
52 // Chunk in new repository |
|
53 MigrationEvent e2 = new MigrationEvent(); |
|
54 e2.id = 2; |
|
55 e2.commit(); |
|
56 r.stop(); |
|
57 events.await(); |
|
58 // Verify that it happened in new repository |
|
59 if (!Files.exists(newRepository)) { |
|
60 throw new AssertionError("Could not find repository " + newRepository); |
|
61 } |
|
62 System.out.println("Listing contents in new repository:"); |
|
63 boolean empty= true; |
|
64 for (Path p: Files.newDirectoryStream(newRepository)) { |
|
65 System.out.println(p.toAbsolutePath()); |
|
66 empty = false; |
|
67 } |
|
68 System.out.println(); |
|
69 if (empty) { |
|
70 throw new AssertionError("Could not find contents in new repository location " + newRepository); |
|
71 } |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 } |