【发布时间】:2020-03-29 10:22:05
【问题描述】:
我是 ZIO 和 ZIO Test 的新手,我想测试我在 ZIO v1.0.0RC17 下编写的调度服务:
服务:
import zio.{RIO, Schedule}
import zio.clock.Clock
import zio.duration._
trait ModuleA {
def moduleA: ModuleA.Service
}
object ModuleA {
trait Service {
def schedule(arg: Int): RIO[Clock, Unit]
}
}
trait ModuleALive extends ModuleA {
def moduleB: ModuleB.Service
override def moduleA: ModuleA.Service = new ModuleA.Service {
override def schedule(arg: Int): RIO[Clock, Unit] = {
moduleB.run(arg).repeat(Schedule.spaced(1 day)).map(_ => ())
}
}
}
trait ModuleB {
def moduleB: ModuleB.Service
}
object ModuleB {
trait Service {
def run(arg: Int): RIO[Clock, Unit]
}
}
ModuleA 的服务基本上应该每天运行一次 ModuleB 的 Service 方法,并将参数输入 ModuleA.Service.run。
我想写的测试:
import java.util.concurrent.atomic.AtomicInteger
import zio.clock.Clock
import zio.duration._
import zio.test.environment.TestClock
import zio.test.{DefaultRunnableSpec, assertCompletes, suite, testM}
import zio.{RIO, Task, ZIO}
object ExampleSpec extends DefaultRunnableSpec(ExampleSuite.suite1)
object ExampleSuite {
val counter: AtomicInteger = new AtomicInteger(0)
trait ModuleBTest extends ModuleB {
override def moduleB: ModuleB.Service = new ModuleB.Service {
override def run(arg: Int): RIO[Clock, Unit] = ZIO.effectTotal(counter.incrementAndGet())
}
}
object ModuleATest extends ModuleALive with ModuleBTest
def verifyExpectedInvocationCount(expectedInvocationCount: Int): Task[Unit] = {
val actualInvocations = counter.get()
if (counter.get() == expectedInvocationCount)
ZIO.succeed(())
else
throw new Exception(s"expected invocation count: $expectedInvocationCount but was $actualInvocations")
}
val suite1 = suite("a")(
testM("a should correctly schedule b") {
for {
_ <- ModuleATest.moduleA.schedule(42).fork
_ <- TestClock.adjust(12 hours)
_ <- verifyExpectedInvocationCount(1)
_ <- TestClock.adjust(12 hours)
_ <- verifyExpectedInvocationCount(2)
} yield assertCompletes
}
)
}
我使用计数器简化了测试,实际上我想使用 mockito 来验证调用计数以及正确的参数。但是,此测试不起作用。据我了解,这是因为https://zio.dev/docs/howto/howto_test_effects#testing-clock 中描述的计时开销引入了竞争条件。
现在,有一些示例说明如何使用 Promise 解决此问题。我尝试用这样的承诺替换计数器:
import java.util.concurrent.atomic.AtomicInteger
import zio.test.{DefaultRunnableSpec, assertCompletes, suite, testM}
import zio.{Promise, Task, UIO, ZIO}
object ExampleSpec extends DefaultRunnableSpec(ExampleSuite.suite1)
object ExampleSuite {
val counter: AtomicInteger = new AtomicInteger(0)
var promise: UIO[Promise[Unit, Int]] = Promise.make[Unit, Int]
trait ModuleBTest extends ModuleB {
override def moduleB: ModuleB.Service = new ModuleB.Service {
override def run(arg: Int) = promise.map(_.succeed(counter.incrementAndGet))
}
}
object ModuleATest extends ModuleALive with ModuleBTest
def verifyExpectedInvocationCount(expectedInvocationCount: Int, actualInvocations: Int): Task[Unit] = {
if (actualInvocations == expectedInvocationCount)
ZIO.succeed(())
else
throw new Exception(s"expected invocation count: $expectedInvocationCount but was $actualInvocations")
}
val suite1 = suite("a")(
testM("a should correctly schedule b") {
for {
_ <- ModuleATest.moduleA.schedule(42).fork
p <- promise
actualInvocationCount <- p.await
_ <- verifyExpectedInvocationCount(expectedInvocationCount = 1, actualInvocationCount)
} yield assertCompletes
}
)
}
使用它,测试不会终止。但是,我很确定我错误地使用了 Promise。
如何正确处理这个测试场景?
【问题讨论】:
标签: scala unit-testing mockito zio zio-test