如何测试“定时器代码”在GWTTestCase中? [英] How to test "Timer code" in a GWTTestCase?

查看:174
本文介绍了如何测试“定时器代码”在GWTTestCase中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GWT编写代码,并且创建了一个java.util.Timer emulation ,我可以用它来测试。在Java中,我将创建一个Timer,在其上启动一个任务,等待任务执行完毕,然后成功返回单元测试方法(或者在失败后超时)。

我对JavaScript没有太多了解(这就是为什么我使用GWT),但我已经看到了使用JavaScriptnative定时器函数setTimeout()/ setInterval()的示例,其中处理函数/回调, 使用了一个标志来确保它在执行时不被调用。换句话说,某些示例表示/可能是可能,而回调/处理程序需要很长时间才能执行,然后再次调用 ,因为间隔耗尽了一次更多。所以这些例子使用了一些标志,回调被检查以确保它没有任何作用,如果最后一次调用没有结束的话。



这让我认为我可以使用setInterval()(间接通过GWT)获得一些回调,以便在我的代码正在等待时执行。由于JavaScript中没有睡眠,我使用了一个繁忙循环,但回调从未得到执行。



那么,我是否假设 async回调是错误的(有时,总是?),或者GWT Timer类(或者htmlunit本身)以某种方式阻止了这种行为(idk GWT Timer是如何在内部实现的)?

更重要的是,如何在GWTTestCase中测试我的java.util.Timer仿真,如果在测试方法调用中创建的任何TimerTask只是在测试方法调用返回时才执行?



在真实应用程序中,将会有一个游戏循环,并且计时器任务可以在游戏循环之间自由运行。



GWT代码如下所示:

  public class TimerTest extends GWTTestCase {
public void testScheduleLong() {
最终计时器计时器=新计时器(测试);
final boolean [] marker = new boolean [] {false};
new com.google.gwt.user.client.Timer(){
@Override
public void run(){
marker [0] = true;
}
} .schedule(10);

assertFalse(marker [0],marker [0]);
double value = Long.MAX_VALUE;
//浪费时间,查看标记是否变化
final long start = System.currentTimeMillis(); (!marker [0]&&(System.currentTimeMillis() - start< 1000)){
value = Math.sqrt(value);
if(value> 1){
value = Long.MAX_VALUE;
}
}
assertTrue(marker [0],marker [0]);



$ div $解析方案

谢谢对@ColinAlworth的评论,基本上在对另一个问题的评论中回答了这个问题,我发现支持测试定时器: GWTTestCase.delayTestFinish(int timeoutMillis)+ finishTest()


I am writing code using GWT, and I have created a java.util.Timer emulation, which I could like to test. In Java, I would create a Timer, fire a task on it, wait until the task was executed, and then return from successfully the unit test method (or fail, after some timeout expired).

I don't know much about JavaScript (which is why I use GWT), but I have seen examples using the JavaScript "native" timer functions setTimeout()/setInterval(), where the handler/callback, used a flag to make sure it was not called "while executing". In other words, some example said/implied that it is possible that while the callback/handler takes long to execute, it then gets called again, as the interval ran out once more. And so the examples used some flag, which the callback checked to make sure it does nothing if the last call to it did not end yet.

This made me assume that I can use setInterval() (indirectly, through GWT), to get some callback to be executed while my code is waiting. Since there is no "sleep" in JavaScript, I used a "busy loop" instead, but the callback never got executed.

So, is it that my assumption about "async callback" is wrong (sometimes, always?), or that the GWT Timer class (or htmlunit itself) somehow prevents this behavior (idk how the GWT Timer is implemented internally)?

And more importantly, how can I then test my java.util.Timer emulation in a GWTTestCase, if any TimerTask I create within a test method call simply won't execute until the test method call returned?

In the "real application", there will be a "game loop", and the timer tasks are free to run between the game loop cycles.

GWT code is something like:

public class TimerTest extends GWTTestCase {
    public void testScheduleLong() {
        final Timer timer = new Timer("test");
        final boolean[] marker = new boolean[] { false };
        new com.google.gwt.user.client.Timer() {
            @Override
            public void run() {
                marker[0] = true;
            }
        }.schedule(10);

        assertFalse("marker[0]", marker[0]);
        double value = Long.MAX_VALUE;
        // Waste time, to see if marker changes
        final long start = System.currentTimeMillis();
        while (!marker[0] && (System.currentTimeMillis() - start < 1000)) {
            value = Math.sqrt(value);
            if (value > 1) {
                value = Long.MAX_VALUE;
            }
        }
        assertTrue("marker[0]", marker[0]);
    }
}

解决方案

Thanks to a comment for @ColinAlworth, which basically answered this question in a comment on another question, I found there is support for testing timers: GWTTestCase.delayTestFinish(int timeoutMillis) + finishTest()

这篇关于如何测试“定时器代码”在GWTTestCase中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆