Scala ';测试使用时间,但不提前测试时钟';内齐奥试验
将测试迁移到RC18后,我收到以下警告,测试挂起:Scala ';测试使用时间,但不提前测试时钟';内齐奥试验,scala,zio,zio-test,Scala,Zio,Zio Test,将测试迁移到RC18后,我收到以下警告,测试挂起: Warning: A test is using time, but is not advancing the test clock, which may result in the test hanging. Use TestClock.adjust to manually advance the time. 我有以下测试: val testLayer: ZLayer[Live, Nothing, Loggings with Blockin
Warning: A test is using time, but is not advancing the test clock, which may result in the test hanging. Use TestClock.adjust to manually advance the time.
我有以下测试:
val testLayer: ZLayer[Live, Nothing, Loggings with Blocking with Clock] = (Console.live >>> loggings.consoleLogger) ++ Blocking.live ++ TestClock.default
testM("curl on invalid URL") {
for {
fork <- composer.curl("https://bad.xx", 1).flip.fork
_ <- TestClock.adjust(3.second * 2)
r <- fork.join
} yield
assert(r)(isSubtype[DockerComposerException](hasField("msg", _.msg.trim, equalTo("https://bad.xx could not be reached!"))))
}.provideCustomLayer(testLayer)
所以我想快进
ZIO.sleep(3.s)
你需要在adjust(duration)
之后调用sleep(duration)
,以便在调用currentTime
时反映调整后的时间。因此,上述示例的正确版本为:
testM(“一个人可以快速移动时间”){
为了{
开始时睡眠(3秒)*>效果(n-1)
def spec=套件(“ExampleSpec”){
testM(“test”){
为了{
fiber与您的问题完全无关,但STTP现在有很好的ZIO支持,因此无需向curl
提供支持。谢谢。我参与了创建自己的层的工作。这是否可能,我对时钟做了一些事情。例如,如果我在自定义层中需要时钟?Clock.live>>MyLayerYou肯定可以。要保留的东西请记住,如果您的自定义层需要时钟,那么来自“默认”环境的时钟将已经可用,因此,如果您希望提供不同的时钟服务实现,您只需要自己执行此操作。
testM("One can move time very fast") {
for {
startTime <- currentTime(TimeUnit.SECONDS)
_ <- TestClock.adjust(Duration.fromScala(1 minute))
endTime <- currentTime(TimeUnit.SECONDS)
} yield assert(endTime - startTime)(isGreaterThanEqualTo(60L))
}
def curl(host: String, attempt: Int = 200): ZIO[Loggings with Clock, Throwable, Unit] = {
ZIO.effect(
Process(Seq("curl", "--output", "/dev/null", "--silent", "--head", "--fail", host)).!!
).flatMap(r =>
info(s"\n$host is ready to use") *> ZIO.succeed()
).catchAll(t =>
if (attempt == 0)
ZIO.fail(DockerComposerException(s"\n$host could not be reached!", Some(t)))
else
info(s"still waiting ;(") *>
ZIO.sleep(3.second) *>
curl(host, attempt - 1)
)
}