小编Fab*_*eto的帖子

用于在Java中断言多线程代码的库

作为TDD从业者,我想测试我编码的所有内容.

在过去的几年里,我一直在编写许多多线程代码,而且我的测试中有一部分一直困扰着我.

当我必须断言在run()循环期间可能发生的事情时,我最终得到了一些像这样的断言:

assertEventually(timeout, assertion)
Run Code Online (Sandbox Code Playgroud)

我知道Mockito有一个解决方案,但仅限于验证电话.我也知道JUnit有一个超时属性,可以避免挂起(或永久)测试.但我想要的是能够让我断言可能随着时间变成真实的东西.

所以我的问题是,有没有人知道提供此功能的库?

这是我到目前为止的解决方案:

private void assertEventually(int timeoutInMilliseconds, Runnable assertion){
    long begin = System.currentTimeMillis();
    long now = begin;
    Throwable lastException = null;
    do{
        try{
            assertion.run();
            return;
        }catch(RuntimeException e){
            lastException = e;
        }catch(AssertionError e){
            lastException = e;
        }
        now = System.currentTimeMillis(); 
    }while((now - begin) < timeoutInMilliseconds);
    throw new RuntimeException(lastException);
}
Run Code Online (Sandbox Code Playgroud)

使用它最终会像这样:

assertEvetuallyTrue(1000, new Runnable() {
        public void run() {
            assertThat(Thread.activeCount()).isEqualTo(before);
        }
    });
Run Code Online (Sandbox Code Playgroud)

java multithreading unit-testing

7
推荐指数
2
解决办法
947
查看次数

App上的XMLHttpRequest失败

我正在尝试使用Ionic框架和Parse.com在我的移动应用程序上运行一个简单的单例示例.代码很简单如下:

Parse.initialize(APP_KEY, JS_KEY);
Parse.User.signUp("my.user", "123456", {}, {
  success: function(user) {
    // Hooray! Let them use the app now.
    console.log('yuhuuu ' + user)
  },
  error: function(user, error) {
    // Show the error message somewhere and let the user try again.
    alert("Error: " + error.code + " " + error.message);
  }
});
Run Code Online (Sandbox Code Playgroud)

当我在浏览器上测试它时,此代码有效,但当我在手机上运行时,我收到错误代码,100并显示以下消息:

在此输入图像描述

我已经尝试过改变我单挑的方式(在对象中使用而不是直接传递用户和密码).还检查了Android应用程序是否具有访问Web资源的适当权限(没关系).

cordova parse-platform ionic-framework

4
推荐指数
1
解决办法
6900
查看次数