-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fs2 | ||
package async | ||
|
||
import cats.effect.IO | ||
import cats.implicits._ | ||
|
||
class RefSpec extends Fs2Spec with EventuallySupport { | ||
|
||
"Ref" - { | ||
|
||
"concurrent modifications" in { | ||
val finalValue = 10 | ||
// Cannot use streams, parallelSequence or Promise since they are implemented with Ref | ||
val r = refOf[IO, Int](0).unsafeRunSync | ||
|
||
List.fill(finalValue) { | ||
fork(r.modify(_ + 1)) | ||
}.sequence.unsafeRunSync | ||
|
||
eventually { r.get.unsafeRunSync shouldBe finalValue } | ||
} | ||
|
||
"successful access" in { | ||
val op = for { | ||
r <- refOf[IO, Int](0) | ||
valueAndSetter <- r.access | ||
(value, setter) = valueAndSetter | ||
success <- setter(value + 1) | ||
result <- r.get | ||
} yield success && result == 1 | ||
|
||
op.unsafeRunSync shouldBe true | ||
} | ||
|
||
"failed access" in { | ||
val op = for { | ||
r <- refOf[IO, Int](0) | ||
valueAndSetter <- r.access | ||
(value, setter) = valueAndSetter | ||
_ <- r.setSync(5) | ||
success <- setter(value + 1) | ||
result <- r.get | ||
} yield !success && result == 5 | ||
|
||
op.unsafeRunSync shouldBe true | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package fs2 | ||
package async | ||
|
||
import cats.effect.IO | ||
import cats.implicits._ | ||
|
||
import scala.concurrent.duration._ | ||
|
||
class PromiseSpec extends AsyncFs2Spec { | ||
|
||
"Promise" - { | ||
|
||
"setSync" in { | ||
promise[IO, Int].flatMap { p => | ||
p.setSync(0) *> p.get | ||
}.unsafeToFuture.map { _ shouldBe 0 } | ||
} | ||
|
||
"setSync is only successful once" in { | ||
promise[IO, Int].flatMap { p => | ||
p.setSync(0) *> p.setSync(1) *> p.get | ||
}.unsafeToFuture.map { _ shouldBe 0 } | ||
} | ||
|
||
"get blocks until set" in { | ||
val op = for { | ||
state <- refOf[IO, Int](0) | ||
modifyGate <- promise[IO, Unit] | ||
readGate <- promise[IO, Unit] | ||
_ <- fork { | ||
modifyGate.get *> state.modify(_ * 2) *> readGate.setSync(()) | ||
} | ||
_ <- fork { | ||
state.setSync(1) *> modifyGate.setSync(()) | ||
} | ||
_ <- readGate.get | ||
res <- state.get | ||
} yield res | ||
|
||
op.unsafeToFuture.map(_ shouldBe 2) | ||
} | ||
|
||
"timedGet" in { | ||
mkScheduler.evalMap { scheduler => | ||
for { | ||
p <- async.promise[IO,Int] | ||
first <- p.timedGet(100.millis, scheduler) | ||
_ <- p.setSync(42) | ||
second <- p.timedGet(100.millis, scheduler) | ||
} yield List(first, second) | ||
}.runLog.unsafeToFuture.map(_.flatten shouldBe Vector(None, Some(42))) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.