jdk/test/javax/management/timer/MissingNotificationTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 30046
diff changeset
     2
 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     4
 *
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     8
 *
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    13
 * accompanied this code).
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    14
 *
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    18
 *
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    21
 * questions.
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    22
 */
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    23
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    24
/*
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    25
 * @test
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    26
 * @bug 6809322
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    27
 * @summary Test for missing notifications in a high concurrency environment
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    28
 * @author Jaroslav Bachorik
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 30046
diff changeset
    29
 * @modules java.management
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    30
 * @run clean MissingNotificationTest
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    31
 * @run build MissingNotificationTest
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    32
 * @run main MissingNotificationTest
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 14189
diff changeset
    33
 * @key randomness
14189
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    34
 */
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    35
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    36
import java.util.Date;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    37
import java.util.Random;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    38
import java.util.concurrent.ExecutorService;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    39
import java.util.concurrent.Executors;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    41
import javax.management.timer.Timer;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    42
import javax.management.Notification;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    43
import javax.management.NotificationListener;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    44
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    45
public class MissingNotificationTest {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    46
    private static int TASK_COUNT = 10000;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    47
    private static long fixedDelay = 0;// anything bigger than 100 and no alarms remain unfired
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    48
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    49
    private static class NotifListener implements NotificationListener {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    50
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    51
        int count;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    52
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    53
        public synchronized void handleNotification(Notification notification, Object handback) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    54
            count++;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    55
        }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    56
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    57
        synchronized int getCount() {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    58
            return count;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    59
        }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    60
    }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    61
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    63
        System.out.println(
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    64
            ">>> Test for missing notifications.");
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    65
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    66
        System.out.println(">>> Create a Timer object.");
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    67
        final Timer timer = new Timer();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    68
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    69
        timer.start();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    70
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    71
        NotifListener listener = new NotifListener();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    72
        timer.addNotificationListener(listener, null, null);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    73
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    74
        ExecutorService executor = Executors.newFixedThreadPool(100);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    75
        final Random rand = new Random();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    76
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    77
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    78
        for (int i = 0; i < TASK_COUNT; i++) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    79
            executor.execute(new Runnable() {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    80
                public void run() {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    81
                    long dateMillis = System.currentTimeMillis() + fixedDelay + rand.nextInt(2000);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    82
                    Date date = new Date(dateMillis);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    83
                    timer.addNotification("type", "msg", "userData", date);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    84
                }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    85
            });
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    86
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    87
        }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    88
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    89
        executor.shutdown();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    90
        executor.awaitTermination(20, TimeUnit.SECONDS);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    91
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    92
        waitForNotificationsToEnd(listener);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    93
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    94
        timer.stop();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    95
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    96
        if (listener.count < TASK_COUNT) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    97
            throw new RuntimeException("Not fired: " + (TASK_COUNT - listener.count));
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    98
        } else {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
    99
            System.out.println(">>> All notifications handled OK");
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   100
        }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   101
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   102
        System.out.println(">>> Bye bye!");
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   103
    }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   104
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   105
    /**
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   106
     * Will return when all notifications are handled or after 10sec. of no new
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   107
     * notifications
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   108
     *
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   109
     * @param listener
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   110
     * @throws InterruptedException
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   111
     */
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   112
    private static void waitForNotificationsToEnd(NotifListener listener)
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   113
            throws InterruptedException {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   114
        int oldCout = listener.getCount();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   115
        int noChangeCounter = 1;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   116
        while (listener.getCount() < TASK_COUNT) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   117
            Thread.sleep(1000);
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   118
            System.out.print('.');
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   119
            if (oldCout == listener.getCount())//no change
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   120
            {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   121
                if (++noChangeCounter > 10) {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   122
                    break;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   123
                }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   124
            } else {
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   125
                noChangeCounter = 1;
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   126
            }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   127
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   128
            oldCout = listener.getCount();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   129
        }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   130
        System.out.println();
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   131
    }
f9cabe313865 6809322: javax.management.timer.Timer does not fire all notifications
dsamersoff
parents:
diff changeset
   132
}