Skip to content

Commit

Permalink
tests: TMVar#take
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-py committed May 5, 2019
1 parent 708f687 commit 12090bf
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions shared/src/test/scala/com/olegpy/stm/misc/TMVarTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.olegpy.stm.misc

import cats.effect.IO
import cats.effect.concurrent.MVar
import cats.implicits._
import com.olegpy.stm.BaseIOSuite
import com.olegpy.stm.results._
import utest._
Expand Down Expand Up @@ -48,5 +50,22 @@ object TMVarTests extends TestSuite with BaseIOSuite {
_ <- mv2.tryPut(()).map(_ ==> false)
} yield ()
}

"TMVar#take" - {
def producer(mv: MVar[IO, Int]): IO[Unit] = mv.put(1) >> mv.put(2) >> mv.put(3)
def consumer(mv: MVar[IO, Int]): IO[List[Int]] = {
def loop(list: List[Int]): IO[List[Int]] = {
if (list.length == 3) IO.pure(list.reverse)
else nap >> mv.take.map(_ :: list) >>= loop
}
loop(Nil)
}

for {
mv <- TMVar.emptyIn[IO, Int].map(_.to[IO])
_ <- producer(mv).start
list <- consumer(mv)
} yield list ==> List(1, 2, 3)
}
}
}

0 comments on commit 12090bf

Please sign in to comment.