These are chat archives for etorreborre/specs2

8th
Feb 2019
Mark de Jong
@Fristi
Feb 08 07:21

Is there a way to test for non-determinism in specs2 ? E.g.:

        // l = Either[Throwable, Map[UUID, NonEmptyList[Event[Json]]]]]
        // r = Either[Throwable, Map[UUID, NonEmptyList[Event[Json]]]]]
        val (l, r) = MVar
          .of[IO, Map[UUID, NonEmptyList[Event[Json]]]](Map.empty)
          .flatMap { mvar =>
            for {
              fiberA <- program(mvar).start
              fiberB <- program(mvar).start
              resA   <- fiberA.join.attempt
              resB   <- fiberB.join.attempt
            } yield (resA, resB)
          }
          .unsafeRunSync()

So we got a pair l and r. Both are Either[Throwable, Map[UUID, NonEmptyList[Event[Json]]]]]. One of them should have a Left(Throwable) and one other should have Right(Map(..)), however due concurrency it's not sure which variable contains it.. but we can assume that the property of having a Left and Right value

Mark de Jong
@Fristi
Feb 08 07:27

Now testing like this

(l, r) match {
          case (Left(err), Right(v)) =>
            err must beEqualTo(MultipleWritersForEventStream(userId))
            v must beEqualTo(Map(userId -> NonEmptyList.of(Event(1, now, Json.obj("_type" -> Json.fromString("WatchedIntroductionClip"))))))
          case (Right(v), Left(err)) =>
            err must beEqualTo(MultipleWritersForEventStream(userId))
            v must beEqualTo(Map(userId -> NonEmptyList.of(Event(1, now, Json.obj("_type" -> Json.fromString("WatchedIntroductionClip"))))))
          case _ =>
            failure("Non-deterministic property not met")
        }

But I wonder if you can do it more elegantly :-)