Skip to content

Commit

Permalink
Realistically test behaviour of mergePreferred
Browse files Browse the repository at this point in the history
  • Loading branch information
shagoon committed Jul 11, 2023
1 parent 83fcff2 commit b8a3cc5
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions core/shared/src/test/scala/fs2/StreamMergeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,29 @@ class StreamMergeSuite extends Fs2Suite {
}
}

test("mergePreferred prefers") {
forAllF { (leftStream: Stream[Pure, Int], rightStream: Stream[Pure, Int]) =>
val leftTagged = leftStream.covary[IO]
val rightTagged = rightStream.covary[IO].delayBy(10.milli)
leftTagged
.mergePreferred(rightTagged)
.assertEmitsSameAs(leftStream ++ rightStream)
}
test("mergePreferred prefers this over that") {

val units = Stream.unit.covary[IO].repeat
val left = units.map(Left(_))
val right = units.map(Right(_))

val stream = left.mergePreferred(right)

stream
.take(10000)
.fold((0L, 0L)) {
case ((left, right), Left(_)) => (left + 1, right)
case ((left, right), Right(_)) => (left, right + 1)
}
.compile
.lastOrError
.map { case (left, right) =>
val relLeft = left.toDouble / (left + right).toDouble
// Tolerate up to 2% elements of the non preferred stream.
// Increase the value, if the test (ocassionally) reports false positives.
val delta = 0.02d
assertEqualsDouble(relLeft, 1.0d, delta)
}
}

test("mergePreferred fully consumes this") {
Expand Down

0 comments on commit b8a3cc5

Please sign in to comment.