-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More flow ops: drain, onComplete, onDone, onError (#228)
- Loading branch information
Showing
6 changed files
with
144 additions
and
18 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
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,27 @@ | ||
package ox.flow | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
class FlowOpsDrainTest extends AnyFlatSpec with Matchers: | ||
behavior of "drain" | ||
|
||
it should "drain all elements" in: | ||
val f = Flow.fromValues(1, 2, 3) | ||
f.drain().runToList() shouldBe List.empty | ||
|
||
it should "run any side-effects that are part of the flow" in: | ||
val c = new AtomicInteger(0) | ||
val f = Flow.fromValues(1, 2, 3).tap(_ => c.incrementAndGet().discard) | ||
f.drain().runDrain() | ||
c.get() shouldBe 3 | ||
|
||
it should "merge with another flow" in: | ||
val c = new AtomicInteger(0) | ||
val f1 = Flow.fromValues(1, 2, 3).tap(_ => c.incrementAndGet().discard) | ||
val f2 = Flow.fromValues(4, 5, 6).tap(_ => c.incrementAndGet().discard) | ||
f1.drain().merge(f2).runToList() shouldBe List(4, 5, 6) | ||
c.get() shouldBe 6 | ||
end FlowOpsDrainTest |
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,65 @@ | ||
package ox.flow | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
class FlowOpsEnsureTest extends AnyFlatSpec with Matchers: | ||
behavior of "ensure.onComplete" | ||
|
||
it should "run in case of success" in: | ||
val didRun = new AtomicBoolean(false) | ||
val f = Flow.fromValues(1, 2, 3).onComplete(didRun.set(true)) | ||
|
||
didRun.get() shouldBe false | ||
f.runDrain() | ||
didRun.get() shouldBe true | ||
|
||
it should "run in case of error" in: | ||
val didRun = new AtomicBoolean(false) | ||
val f = Flow.fromValues(1, 2, 3).concat(Flow.failed(new RuntimeException)).onComplete(didRun.set(true)) | ||
|
||
didRun.get() shouldBe false | ||
intercept[RuntimeException]: | ||
f.runDrain() | ||
didRun.get() shouldBe true | ||
|
||
behavior of "ensure.onDone" | ||
|
||
it should "run in case of success" in: | ||
val didRun = new AtomicBoolean(false) | ||
val f = Flow.fromValues(1, 2, 3).onDone(didRun.set(true)) | ||
|
||
didRun.get() shouldBe false | ||
f.runDrain() | ||
didRun.get() shouldBe true | ||
|
||
it should "not run in case of error" in: | ||
val didRun = new AtomicBoolean(false) | ||
val f = Flow.fromValues(1, 2, 3).concat(Flow.failed(new RuntimeException)).onDone(didRun.set(true)) | ||
|
||
didRun.get() shouldBe false | ||
intercept[RuntimeException]: | ||
f.runDrain() | ||
didRun.get() shouldBe false | ||
|
||
behavior of "ensure.onError" | ||
|
||
it should "not run in case of success" in: | ||
val didRun = new AtomicBoolean(false) | ||
val f = Flow.fromValues(1, 2, 3).onError(_ => didRun.set(true)) | ||
|
||
didRun.get() shouldBe false | ||
f.runDrain() | ||
didRun.get() shouldBe false | ||
|
||
it should "run in case of error" in: | ||
val didRun = new AtomicBoolean(false) | ||
val f = Flow.fromValues(1, 2, 3).concat(Flow.failed(new RuntimeException)).onError(_ => didRun.set(true)) | ||
|
||
didRun.get() shouldBe false | ||
intercept[RuntimeException]: | ||
f.runDrain() | ||
didRun.get() shouldBe true | ||
end FlowOpsEnsureTest |
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