-
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.
Fix the tick source to emit values at regular intervals, accommodatin…
…g for slow consumers (#82)
- Loading branch information
Showing
5 changed files
with
87 additions
and
30 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
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.concurrent.Eventually | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
import java.util.concurrent.ConcurrentLinkedQueue | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import scala.concurrent.duration.DurationInt | ||
import scala.jdk.CollectionConverters.* | ||
|
||
class SourceOpsTickTest extends AnyFlatSpec with Matchers with Eventually { | ||
it should "tick regularly" in { | ||
supervised { | ||
val start = System.currentTimeMillis() | ||
val c = Source.tick(100.millis) | ||
c.receive() shouldBe () | ||
(System.currentTimeMillis() - start) shouldBe >=(0L) | ||
(System.currentTimeMillis() - start) shouldBe <=(50L) | ||
|
||
c.receive() shouldBe () | ||
(System.currentTimeMillis() - start) shouldBe >=(100L) | ||
(System.currentTimeMillis() - start) shouldBe <=(150L) | ||
|
||
c.receive() shouldBe () | ||
(System.currentTimeMillis() - start) shouldBe >=(200L) | ||
(System.currentTimeMillis() - start) shouldBe <=(250L) | ||
} | ||
} | ||
|
||
it should "tick immediately in case of a slow consumer, and then resume normal " in { | ||
supervised { | ||
val start = System.currentTimeMillis() | ||
val c = Source.tick(100.millis) | ||
|
||
// simulating a slow consumer | ||
Thread.sleep(200) | ||
c.receive() shouldBe () // a tick should be waiting | ||
(System.currentTimeMillis() - start) shouldBe >=(200L) | ||
(System.currentTimeMillis() - start) shouldBe <=(250L) | ||
|
||
c.receive() shouldBe () // and immediately another, as the interval between send-s has passed | ||
(System.currentTimeMillis() - start) shouldBe >=(200L) | ||
(System.currentTimeMillis() - start) shouldBe <=(250L) | ||
} | ||
} | ||
} |