test/jdk/jdk/jfr/api/consumer/streaming/TestRepositoryMigration.java
changeset 58863 c16ac7a2eba4
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     1 /*
       
     2  * Copyright (c) 2019, 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 a repository that is being
       
    43  *          moved.
       
    44  * @key jfr
       
    45  * @requires vm.hasJFR
       
    46  * @library /test/lib /test/jdk
       
    47  * @run main/othervm jdk.jfr.api.consumer.streaming.TestRepositoryMigration
       
    48  */
       
    49 public class TestRepositoryMigration {
       
    50     static class MigrationEvent extends Event {
       
    51         int id;
       
    52     }
       
    53 
       
    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.setFlushInterval(Duration.ofSeconds(1));
       
    75                 r.start();
       
    76                 // Chunk in default repository
       
    77                 MigrationEvent e1 = new MigrationEvent();
       
    78                 e1.id = 1;
       
    79                 e1.commit();
       
    80                 event1.await();
       
    81                 System.out.println("Passed the event1.await()");
       
    82                 JcmdHelper.jcmd("JFR.configure", "repositorypath=" + newRepository.toAbsolutePath());
       
    83                 // Chunk in new repository
       
    84                 MigrationEvent e2 = new MigrationEvent();
       
    85                 e2.id = 2;
       
    86                 e2.commit();
       
    87                 r.stop();
       
    88                 event2.await();
       
    89                 System.out.println("Passed the event2.await()");
       
    90                 // Verify that it happened in new repository
       
    91                 if (!Files.exists(newRepository)) {
       
    92                     throw new AssertionError("Could not find repository " + newRepository);
       
    93                 }
       
    94                 System.out.println("Listing contents in new repository:");
       
    95                 boolean empty = true;
       
    96                 for (Path p : Files.newDirectoryStream(newRepository)) {
       
    97                     System.out.println(p.toAbsolutePath());
       
    98                     empty = false;
       
    99                 }
       
   100                 System.out.println();
       
   101                 if (empty) {
       
   102                     throw new AssertionError("Could not find contents in new repository location " + newRepository);
       
   103                 }
       
   104             }
       
   105         }
       
   106     }
       
   107 
       
   108 }