小编cca*_*ers的帖子

如何测试计时器?

我想为一个方法编写测试,该方法在特定的intervall中调用观察者,以便他们执行一个方法.timer-object在自己的线程中运行.

要测试的计时器的方法
private long waitTime;

public Metronome(int bpm) {
    this.bpm = bpm;
    this.waitTime = calculateWaitTime();
    this.running = false;
}

public void run() {
    long startTime = 0, estimatedTime = 0, threadSleepTime = 0;

    running = true;

    while (running) {
        startTime = System.nanoTime();

        tick();// notify observers here

        estimatedTime = System.nanoTime() - startTime;
        threadSleepTime = waitTime -estimatedTime;
        threadSleepTime = threadSleepTime < 0 ? 0 : threadSleepTime;

        try {
            Thread.sleep(threadSleepTime / 1000000l);

        } catch (InterruptedException e) {
                // sth went wrong
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java junit timer

11
推荐指数
1
解决办法
5097
查看次数

标签 统计

java ×1

junit ×1

timer ×1