本文介绍了为什么当我执行Await.result时,我的Scala异步测试从未完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我创建了一个简单的测试方案来显示此内容:

I have created a simple test scenario that shows this:

class Test extends AsyncFunSuite {

  test("async test") {
    val f = Future {
      val thread = new Thread {
        override def run(): Unit = {
          println("OKAYY")
        }
      }
      thread.start()
    }

    Await.result(f, Duration.Inf)
    Future {
      assert(true)
    }
  }

}

执行此操作时,测试将永远运行并且永远不会完成.

When executing this, the test runs forever and never completes.

推荐答案

在ScalaTest中,所有异步测试(AsyncFeatureSpec,AsyncFlatSpec,AsyncFreeSpec,AsyncFunSpec,AsyncFunSuite,AsyncWordSpec)中都会看到相同的行为.

You will see the same behavior with all asynchronous testing in ScalaTest (AsyncFeatureSpec, AsyncFlatSpec, AsyncFreeSpec, AsyncFunSpec, AsyncFunSuite, AsyncWordSpec).

这样做的原因是ScalaTest中的默认执行上下文是串行执行上下文.您可以在此处了解更多信息: https://www.scalatest.org/user_guide/async_testing.我总结了以下要点.

The reason for this is that the default execution context in ScalaTest is serial execution context. You can read more about this here: https://www.scalatest.org/user_guide/async_testing. I have summarized the important points below.

但是,此线程限制策略的确意味着,当您在JVM上使用默认执行上下文时,必须确保不要在测试主体中阻塞,以等待执行上下文完成任务.如果您阻止,则测试将永远无法完成.

This thread confinement strategy does mean, however, that when you are using the default execution context on the JVM, you must be sure to never block in the test body waiting for a task to be completed by the execution context. If you block, your test will never complete.

解决方案1:覆盖执行上下文

implicit override def executionContext = scala.concurrent.ExecutionContext.Implicits.global

解决方案2:链接

class Test extends AsyncFunSuite {

  test("async test") {
    val f = Future {
      val thread = new Thread {
        override def run(): Unit = {
          println("OKAYY")
        }
      }
      thread.start()
    }

    f.map { _ =>
      assert(true)
    }
  }

}

这篇关于为什么当我执行Await.result时,我的Scala异步测试从未完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:59