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