diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2019-11-03 22:56:30 +0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2019-11-03 22:56:30 +0800 |
commit | 0c58de44a581acedc624ec118c8a2feb57334ad4 (patch) | |
tree | ad69495ecfe5afcf8e3dd15fc3c464d1d10196dc /cask/actor/test/src-jvm | |
parent | 27b5d45d919cbc8d37fcc0194bb9bd39603a532e (diff) | |
download | cask-0c58de44a581acedc624ec118c8a2feb57334ad4.tar.gz cask-0c58de44a581acedc624ec118c8a2feb57334ad4.tar.bz2 cask-0c58de44a581acedc624ec118c8a2feb57334ad4.zip |
add debouncing state machine to docs and unit tests
Diffstat (limited to 'cask/actor/test/src-jvm')
-rw-r--r-- | cask/actor/test/src-jvm/JvmActorsTest.scala | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/cask/actor/test/src-jvm/JvmActorsTest.scala b/cask/actor/test/src-jvm/JvmActorsTest.scala index 61c8e74..dc92555 100644 --- a/cask/actor/test/src-jvm/JvmActorsTest.scala +++ b/cask/actor/test/src-jvm/JvmActorsTest.scala @@ -122,5 +122,53 @@ object JvmActorsTest extends TestSuite{ decodeFile(oldPath) ==> Seq("Comes from liquids from my udder") decodeFile(logPath) ==> Seq("I am cow, I am cow", "Hear me moo, moooo") } + + test("debounce"){ + sealed trait Msg + case class Debounced() extends Msg + case class Text(value: String) extends Msg + + class Logger(log: os.Path, debounceTime: java.time.Duration) + (implicit ac: Context) extends StateMachineActor[Msg]{ + def initialState = Idle() + case class Idle() extends State({ + case Text(value) => + ac.scheduleMsg(this, Debounced(), debounceTime) + Buffering(Vector(value)) + }) + case class Buffering(buffer: Vector[String]) extends State({ + case Text(value) => Buffering(buffer :+ value) + case Debounced() => + os.write.append(log, buffer.mkString(" ") + "\n", createFolders = true) + Idle() + }) + } + + implicit val ac = new Context.Test() + + val logPath = os.pwd / "out" / "scratch" / "log.txt" + + val logger = new Logger(logPath, java.time.Duration.ofMillis(50)) + + logger.send(Text("I am cow")) + logger.send(Text("hear me moo")) + Thread.sleep(100) + logger.send(Text("I weight twice as much as you")) + logger.send(Text("And I look good on the barbecue")) + Thread.sleep(100) + logger.send(Text("Yoghurt curds cream cheese and butter")) + logger.send(Text("Comes from liquids from my udder")) + logger.send(Text("I am cow, I am cow")) + logger.send(Text("Hear me moo, moooo")) + + ac.waitForInactivity() + + os.read.lines(logPath) ==> Seq( + "I am cow hear me moo", + "I weight twice as much as you And I look good on the barbecue", + "Yoghurt curds cream cheese and butter Comes from liquids from my udder I am cow, I am cow Hear me moo, moooo", + ) + } + } }
\ No newline at end of file |