zio / zio-intellij

A companion IntelliJ IDEA plugin for the ZIO library ecosystem.
Apache License 2.0
212 stars 40 forks source link

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

Open mostr opened 3 years ago

mostr commented 3 years ago

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)
    }
  )
}
vigoo commented 3 years ago

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

hmemcpy commented 3 years ago

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!