rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/TimerTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 10 May 2016 04:52:05 +0200
changeset 1962 9d46ae9d4a2e
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Don't obfuscate names of fields in objects - otherwise fields provided by two modules may clash
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.util.Timer;
    21 import java.util.TimerTask;
    22 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    23 import org.apidesign.bck2brwsr.vmtest.VMTest;
    24 import org.testng.annotations.Factory;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 public class TimerTest {
    31     boolean started;
    32     int miss;
    33     int exec;
    34     
    35     public TimerTest() {
    36     }
    37     
    38     @BrwsrTest public void scheduleTick() throws Exception {
    39         if (!started) {
    40             started = true;
    41             Timer t = new Timer("MyTest");
    42             class TT extends TimerTask {
    43                 @Override
    44                 public void run() {
    45                     exec++;
    46                 }
    47             }
    48             TT task = new TT();
    49             t.schedule(task, 15);
    50         }
    51         
    52         if (exec == 0) {
    53             miss++;
    54             throw new InterruptedException();
    55         }
    56         
    57         assert exec == 1 : "One exec: " + exec;
    58         assert miss == 1 : "One miss: " + miss;
    59     }
    60     
    61     @BrwsrTest public void repeatedTicks() throws Exception {
    62         if (!started) {
    63             started = true;
    64             Timer t = new Timer("MyTest");
    65             class TT extends TimerTask {
    66                 @Override
    67                 public void run() {
    68                     if (++exec == 2) {
    69                         cancel();
    70                     }
    71                 }
    72             }
    73             TT task = new TT();
    74             t.scheduleAtFixedRate(task, 50, 70);
    75         }
    76         
    77         if (exec != 2) {
    78             miss++;
    79             throw new InterruptedException();
    80         }
    81         
    82         assert exec == 2 : "Two execs: " + exec;
    83         assert miss == 2 : "Two misses: " + miss;
    84     }
    85     
    86     @Factory public static Object[] create() {
    87         return VMTest.create(TimerTest.class);
    88     }
    89     
    90 }