zio-intellij icon indicating copy to clipboard operation
zio-intellij copied to clipboard

Test fails in ZIO test runner but pass with `sbt test` or regular Intellij app runner

Open mostr opened this issue 3 years ago • 2 comments

I have the following tests (stripped version of my original ones). When run via IntelliJ ZIO test runner the first one passes but the second one hangs after logging first repetition, although when run either via sbt test or from IntelliJ using regular app runner both work fine.

import zio.clock.Clock
import zio.duration.durationInt
import zio.logging.Logging
import zio.test.Assertion.equalTo
import zio.test.environment.TestEnvironment
import zio.test.{DefaultRunnableSpec, ZSpec}
import zio.{Has, Promise, Ref, ZIO}

object SimpleSpec extends DefaultRunnableSpec {

  private val doWorkLogging = for {
    currentCount <- ZIO.accessM[Has[Ref[Long]]](_.get.updateAndGet(_ + 1))
    lock         <- ZIO.service[Promise[Nothing, Long]]
    now          <- zio.clock.instant
    _            <- zio.logging.log.info(s"Run $now")
    _            <- lock.completeWith(ZIO.succeed(currentCount)).when(currentCount == 3)
  } yield ()

  private val looper = doWorkLogging *> (zio.UIO.unit.delay(1.seconds) *> doWorkLogging).forever

  private val logging = Logging.console()

  override def spec: ZSpec[Environment, Any] = suite("simple test")(
    testM("Clock.live with promise") {
      val counter = Ref.make(0L).toLayer
      val lock    = Promise.make[Nothing, Long].toLayer
      (for {
        f     <- looper.fork
        count <- ZIO.accessM[Has[Promise[Nothing, Long]]](_.get.await)
        _     <- f.interrupt
      } yield zio.test.assert(count)(equalTo(3L))).provideSomeLayer[TestEnvironment](Clock.live ++ lock ++ logging ++ counter)
    },
    testM("TestClock with promise") {
      val counter = Ref.make(0L).toLayer
      val lock    = Promise.make[Nothing, Long].toLayer
      (for {
        f     <- looper.fork
        _     <- zio.test.environment.TestClock.adjust(5.seconds)
        count <- ZIO.accessM[Has[Promise[Nothing, Long]]](_.get.await)
        _     <- f.interrupt
      } yield zio.test.assert(count)(equalTo(3L))).provideSomeLayer[TestEnvironment](lock ++ logging ++ counter)
    }
  )
}

mostr avatar Apr 07 '21 11:04 mostr

Similar case: https://github.com/zio/zio/issues/4896

vigoo avatar Apr 07 '21 17:04 vigoo

Thank you very much for the repro(s)! I can indeed confirm this happens on my machine too with the runner! Will definitely try to fix soon!

hmemcpy avatar Apr 14 '21 22:04 hmemcpy