Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/timer/MissingNotificationTest.java
38839 views
/*1* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 680932226* @summary Test for missing notifications in a high concurrency environment27* @author Jaroslav Bachorik28* @run clean MissingNotificationTest29* @run build MissingNotificationTest30* @run main MissingNotificationTest31* @key randomness32*/3334import java.util.Date;35import java.util.Random;36import java.util.concurrent.ExecutorService;37import java.util.concurrent.Executors;38import java.util.concurrent.TimeUnit;39import javax.management.timer.Timer;40import javax.management.Notification;41import javax.management.NotificationListener;4243public class MissingNotificationTest {44private static int TASK_COUNT = 10000;45private static long fixedDelay = 0;// anything bigger than 100 and no alarms remain unfired4647private static class NotifListener implements NotificationListener {4849int count;5051public synchronized void handleNotification(Notification notification, Object handback) {52count++;53}5455synchronized int getCount() {56return count;57}58}5960public static void main(String[] args) throws Exception {61System.out.println(62">>> Test for missing notifications.");6364System.out.println(">>> Create a Timer object.");65final Timer timer = new Timer();6667timer.start();6869NotifListener listener = new NotifListener();70timer.addNotificationListener(listener, null, null);7172ExecutorService executor = Executors.newFixedThreadPool(100);73final Random rand = new Random();747576for (int i = 0; i < TASK_COUNT; i++) {77executor.execute(new Runnable() {78public void run() {79long dateMillis = System.currentTimeMillis() + fixedDelay + rand.nextInt(2000);80Date date = new Date(dateMillis);81timer.addNotification("type", "msg", "userData", date);82}83});8485}8687executor.shutdown();88executor.awaitTermination(20, TimeUnit.SECONDS);8990waitForNotificationsToEnd(listener);9192timer.stop();9394if (listener.count < TASK_COUNT) {95throw new RuntimeException("Not fired: " + (TASK_COUNT - listener.count));96} else {97System.out.println(">>> All notifications handled OK");98}99100System.out.println(">>> Bye bye!");101}102103/**104* Will return when all notifications are handled or after 10sec. of no new105* notifications106*107* @param listener108* @throws InterruptedException109*/110private static void waitForNotificationsToEnd(NotifListener listener)111throws InterruptedException {112int oldCout = listener.getCount();113int noChangeCounter = 1;114while (listener.getCount() < TASK_COUNT) {115Thread.sleep(1000);116System.out.print('.');117if (oldCout == listener.getCount())//no change118{119if (++noChangeCounter > 10) {120break;121}122} else {123noChangeCounter = 1;124}125126oldCout = listener.getCount();127}128System.out.println();129}130}131132133