Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/timer/StartTest.java
38839 views
/*1* Copyright (c) 2008, 2011, 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 665921526* @summary Test on timer start method with past notifications27* @author Shanliang JIANG28* @run clean StartTest29* @run build StartTest30* @run main StartTest31*/3233import java.util.Date;34import javax.management.timer.Timer;35import javax.management.Notification;36import javax.management.NotificationListener;3738public class StartTest {39public static void main(String[] args) throws Exception {40System.out.println(41">>> Test on timer start method with past notifications.");4243System.out.println(">>> Create a Timer object.");44Timer timer = new Timer();4546System.out.println(47">>> Set the flag (setSendPastNotification) to true.");48timer.setSendPastNotifications(true);4950timer.addNotificationListener(myListener, null, null);5152System.out.println(">>> Add notifications: " + SENT);5354Date date = new Date();55for (int i = 0; i < SENT; i++) {56timer.addNotification(57"testType" + i, "testMsg" + i, "testData" + i, date);58}5960System.out.println(">>> The notifications should be sent at " + date);61System.out.println(">>> Sleep 100 ms to have past notifications.");62Thread.sleep(100);6364System.out.println(">>> Start the timer at " + new Date());65timer.start();6667System.out.println(">>> Stop the timer.");68Thread.sleep(100);69stopping = true;70timer.stop();7172if (received != SENT) {73throw new RuntimeException(74"Expected to receive " + SENT + " but got " + received);75}7677System.out.println(">>> Received all expected notifications.");7879System.out.println(">>> Bye bye!");80}8182private static NotificationListener myListener =83new NotificationListener() {84public void handleNotification(Notification n, Object hb) {85if (!stopping) {86received++;87System.out.println(88">>> myListener-handleNotification: received " +89n.getSequenceNumber());90}91}92};9394private static int SENT = 10;95private static volatile int received = 0;96private static volatile boolean stopping = false;97}9899100