我是ZIO和ZIO测试的新手,我想测试我在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
是,UIO[Promise[Unit, Int]]
因此您每次都创建一个新的Promise。结果,您完成的承诺与测试正在等待的承诺有所不同,从而导致无法终止。
要对此进行测试,您可以执行以下操作:
import zio.clock.Clock
import zio.duration._
import zio.test.environment.TestClock
import zio.test.{ assertCompletes, suite, testM, DefaultRunnableSpec }
import zio._
object ExampleSpec extends DefaultRunnableSpec {
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]
}
}
trait ModuleBTest extends ModuleB {
val counter: Ref[Int]
val invocations: Queue[Int]
override def moduleB: ModuleB.Service = new ModuleB.Service {
override def run(arg: Int): UIO[Unit] =
counter.updateAndGet(_ + 1).flatMap(invocations.offer).unit
}
}
object ModuleATest {
def apply(ref: Ref[Int], queue: Queue[Int]): ModuleALive with ModuleBTest =
new ModuleALive with ModuleBTest {
val counter = ref
val invocations = queue
}
}
def verifyExpectedInvocationCount(invocations: Queue[Int], expected: Int): Task[Unit] =
invocations.take.flatMap { actual =>
if (actual == expected)
ZIO.succeed(())
else
ZIO.fail(new Exception(s"expected invocation count: $expected but was $actual"))
}
def spec = suite("a")(
testM("a should correctly schedule b") {
for {
counter <- Ref.make(0)
invocations <- Queue.unbounded[Int]
moduleATest = ModuleATest(counter, invocations)
_ <- moduleATest.moduleA.schedule(42).fork
_ <- TestClock.adjust(12.hours)
_ <- verifyExpectedInvocationCount(invocations, 1)
_ <- TestClock.adjust(12.hours)
_ <- verifyExpectedInvocationCount(invocations, 2)
} yield assertCompletes
}
)
}
由于我们要等待多种效果完成,因此我使用Queue
来协调它们。还有两点需要注意:
verifyExpectedInvocationsCount
使用ZIO Test中的断言来代替该方法,以获得更好的错误报告。TestAspect#nonTermination
。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句