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