1 /*
2  * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.jfr.api.consumer.streaming;
27 
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.time.Duration;
32 import java.time.Instant;
33 import java.util.concurrent.CountDownLatch;
34 
35 import jdk.jfr.Event;
36 import jdk.jfr.Recording;
37 import jdk.jfr.consumer.EventStream;
38 import jdk.jfr.jcmd.JcmdHelper;
39 
40 /**
41  * @test
42  * @summary Verifies that is possible to stream from an in-process repository
43  *          that is being moved.
44  * @key jfr
45  * @requires vm.hasJFR
46  * @library /test/lib /test/jdk
47  * @run main/othervm jdk.jfr.api.consumer.streaming.TestInProcessMigration
48  */
49 public class TestInProcessMigration {
50     static class MigrationEvent extends Event {
51         int id;
52     }
53 
main(String... args)54     public static void main(String... args) throws Exception {
55         Path newRepository = Paths.get("new-repository");
56         CountDownLatch event1 = new CountDownLatch(1);
57         CountDownLatch event2 = new CountDownLatch(1);
58 
59         try (EventStream es = EventStream.openRepository()) {
60             es.setStartTime(Instant.EPOCH);
61             es.onEvent(e -> {
62                 System.out.println(e);
63                 if (e.getInt("id") == 1) {
64                     event1.countDown();
65                 }
66                 if (e.getInt("id") == 2) {
67                     event2.countDown();
68                 }
69             });
70             es.startAsync();
71             System.out.println("Started es.startAsync()");
72 
73             try (Recording r = new Recording()) {
74                 r.start();
75                 // Chunk in default repository
76                 MigrationEvent e1 = new MigrationEvent();
77                 e1.id = 1;
78                 e1.commit();
79                 event1.await();
80                 System.out.println("Passed the event1.await()");
81                 JcmdHelper.jcmd("JFR.configure", "repositorypath=" + newRepository.toAbsolutePath());
82                 // Chunk in new repository
83                 MigrationEvent e2 = new MigrationEvent();
84                 e2.id = 2;
85                 e2.commit();
86                 r.stop();
87                 event2.await();
88                 System.out.println("Passed the event2.await()");
89                 // Verify that it happened in new repository
90                 if (!Files.exists(newRepository)) {
91                     throw new AssertionError("Could not find repository " + newRepository);
92                 }
93                 System.out.println("Listing contents in new repository:");
94                 boolean empty = true;
95                 for (Path p : Files.newDirectoryStream(newRepository)) {
96                     System.out.println(p.toAbsolutePath());
97                     empty = false;
98                 }
99                 System.out.println();
100                 if (empty) {
101                     throw new AssertionError("Could not find contents in new repository location " + newRepository);
102                 }
103             }
104         }
105     }
106 
107 }
108