-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from softwaremill/restructure-source-tests
Extract some Source test cases to separate classes
- Loading branch information
Showing
5 changed files
with
226 additions
and
185 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
core/src/test/scala/ox/channels/SourceOpsFactoryMethodsTest.scala
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,40 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsFactoryMethodsTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source factory methods" | ||
|
||
it should "create a source from a fork" in { | ||
scoped { | ||
val f = fork(1) | ||
val c = Source.fromFork(f) | ||
c.toList shouldBe List(1) | ||
} | ||
} | ||
|
||
it should "create an iterating source" in { | ||
scoped { | ||
val c = Source.iterate(1)(_ + 1) | ||
c.take(3).toList shouldBe List(1, 2, 3) | ||
} | ||
} | ||
|
||
it should "unfold a function" in { | ||
scoped { | ||
val c = Source.unfold(0)(i => if i < 3 then Some((i, i + 1)) else None) | ||
c.toList shouldBe List(0, 1, 2) | ||
} | ||
} | ||
|
||
it should "produce a range" in { | ||
scoped { | ||
Source.range(1, 5, 1).toList shouldBe List(1, 2, 3, 4, 5) | ||
Source.range(1, 5, 2).toList shouldBe List(1, 3, 5) | ||
Source.range(1, 11, 3).toList shouldBe List(1, 4, 7, 10) | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/test/scala/ox/channels/SourceOpsForeachTest.scala
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,48 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsForeachTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.foreach" | ||
|
||
it should "iterate over a source" in { | ||
val c = Channel[Int](10) | ||
c.send(1) | ||
c.send(2) | ||
c.send(3) | ||
c.done() | ||
|
||
var r: List[Int] = Nil | ||
c.foreach(v => r = v :: r) | ||
|
||
r shouldBe List(3, 2, 1) | ||
} | ||
|
||
it should "iterate over a source using for-syntax" in { | ||
val c = Channel[Int](10) | ||
c.send(1) | ||
c.send(2) | ||
c.send(3) | ||
c.done() | ||
|
||
var r: List[Int] = Nil | ||
for { | ||
v <- c | ||
} r = v :: r | ||
|
||
r shouldBe List(3, 2, 1) | ||
} | ||
|
||
it should "convert source to a list" in { | ||
val c = Channel[Int](10) | ||
c.send(1) | ||
c.send(2) | ||
c.send(3) | ||
c.done() | ||
|
||
c.toList shouldBe List(1, 2, 3) | ||
} | ||
} |
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,69 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsMapTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.map" | ||
|
||
it should "map over a source" in { | ||
scoped { | ||
val c = Channel[Int]() | ||
fork { | ||
c.send(1) | ||
c.send(2) | ||
c.send(3) | ||
c.done() | ||
} | ||
|
||
val s = c.map(_ * 10) | ||
|
||
s.receive() shouldBe 10 | ||
s.receive() shouldBe 20 | ||
s.receive() shouldBe 30 | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
|
||
it should "map over a source (stress test)" in { | ||
// this demonstrated a race condition where a cell was added by select to the waiting list by T1, completed by T2, | ||
// which then subsequently completed the stream; only then T1 wakes up, and checks if no new elements have been added | ||
for (_ <- 1 to 100000) { | ||
scoped { | ||
val c = Channel[Int]() | ||
fork { | ||
c.send(1) | ||
c.done() | ||
} | ||
|
||
val s = c.map(_ * 10) | ||
|
||
s.receive() shouldBe 10 | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
} | ||
|
||
it should "map over a source using for-syntax" in { | ||
scoped { | ||
val c = Channel[Int]() | ||
fork { | ||
c.send(1) | ||
c.send(2) | ||
c.send(3) | ||
c.done() | ||
} | ||
|
||
val s = for { | ||
v <- c | ||
} yield v * 2 | ||
|
||
s.receive() shouldBe 2 | ||
s.receive() shouldBe 4 | ||
s.receive() shouldBe 6 | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} | ||
} |
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
Oops, something went wrong.