Path: blob/master/test/jdk/javax/management/timer/StartTest.java
41152 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 665921526* @summary Test on timer start method with past notifications27* @author Shanliang JIANG28*29* @run clean StartTest30* @run build StartTest31* @run main StartTest32*/3334import java.util.Date;35import javax.management.timer.Timer;36import javax.management.Notification;37import javax.management.NotificationListener;3839public class StartTest {40public static void main(String[] args) throws Exception {41System.out.println(42">>> Test on timer start method with past notifications.");4344System.out.println(">>> Create a Timer object.");45Timer timer = new Timer();4647System.out.println(48">>> Set the flag (setSendPastNotification) to true.");49timer.setSendPastNotifications(true);5051timer.addNotificationListener(myListener, null, null);5253System.out.println(">>> Add notifications: " + SENT);5455Date date = new Date();56for (int i = 0; i < SENT; i++) {57timer.addNotification(58"testType" + i, "testMsg" + i, "testData" + i, date);59}6061System.out.println(">>> The notifications should be sent at " + date);62System.out.println(">>> Sleep 100 ms to have past notifications.");63Thread.sleep(100);6465System.out.println(">>> Start the timer at " + new Date());66timer.start();6768System.out.println(">>> Stop the timer.");69Thread.sleep(100);70stopping = true;71timer.stop();7273if (received != SENT) {74throw new RuntimeException(75"Expected to receive " + SENT + " but got " + received);76}7778System.out.println(">>> Received all expected notifications.");7980System.out.println(">>> Bye bye!");81}8283private static NotificationListener myListener =84new NotificationListener() {85public void handleNotification(Notification n, Object hb) {86if (!stopping) {87received++;88System.out.println(89">>> myListener-handleNotification: received " +90n.getSequenceNumber());91}92}93};9495private static int SENT = 10;96private static volatile int received = 0;97private static volatile boolean stopping = false;98}99100101