Path: blob/master/test/jdk/javax/management/timer/MissingNotificationTest.java
41149 views
/*1* Copyright (c) 2008, 2015, 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* @key randomness27* @summary Test for missing notifications in a high concurrency environment28* @author Jaroslav Bachorik29*30* @run clean MissingNotificationTest31* @run build MissingNotificationTest32* @run main MissingNotificationTest33*/3435import java.util.Date;36import java.util.Random;37import java.util.concurrent.ExecutorService;38import java.util.concurrent.Executors;39import java.util.concurrent.TimeUnit;40import javax.management.timer.Timer;41import javax.management.Notification;42import javax.management.NotificationListener;4344public class MissingNotificationTest {45private static int TASK_COUNT = 10000;46private static long fixedDelay = 0;// anything bigger than 100 and no alarms remain unfired4748private static class NotifListener implements NotificationListener {4950int count;5152public synchronized void handleNotification(Notification notification, Object handback) {53count++;54}5556synchronized int getCount() {57return count;58}59}6061public static void main(String[] args) throws Exception {62System.out.println(63">>> Test for missing notifications.");6465System.out.println(">>> Create a Timer object.");66final Timer timer = new Timer();6768timer.start();6970NotifListener listener = new NotifListener();71timer.addNotificationListener(listener, null, null);7273ExecutorService executor = Executors.newFixedThreadPool(100);74final Random rand = new Random();757677for (int i = 0; i < TASK_COUNT; i++) {78executor.execute(new Runnable() {79public void run() {80long dateMillis = System.currentTimeMillis() + fixedDelay + rand.nextInt(2000);81Date date = new Date(dateMillis);82timer.addNotification("type", "msg", "userData", date);83}84});8586}8788executor.shutdown();89executor.awaitTermination(20, TimeUnit.SECONDS);9091waitForNotificationsToEnd(listener);9293timer.stop();9495if (listener.count < TASK_COUNT) {96throw new RuntimeException("Not fired: " + (TASK_COUNT - listener.count));97} else {98System.out.println(">>> All notifications handled OK");99}100101System.out.println(">>> Bye bye!");102}103104/**105* Will return when all notifications are handled or after 10sec. of no new106* notifications107*108* @param listener109* @throws InterruptedException110*/111private static void waitForNotificationsToEnd(NotifListener listener)112throws InterruptedException {113int oldCout = listener.getCount();114int noChangeCounter = 1;115while (listener.getCount() < TASK_COUNT) {116Thread.sleep(1000);117System.out.print('.');118if (oldCout == listener.getCount())//no change119{120if (++noChangeCounter > 10) {121break;122}123} else {124noChangeCounter = 1;125}126127oldCout = listener.getCount();128}129System.out.println();130}131}132133134