-
-
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: Delay operator now clears the pending queue when stopped
Previously, if you stopped the delayed stream and then immediately started it, delayed events scheduled before the stream was stopped would fire after it was re-started if their delays did not complete while the stream was stopped.
- Loading branch information
Showing
2 changed files
with
92 additions
and
2 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
75 changes: 75 additions & 0 deletions
75
src/test/scala/com/raquo/airstream/eventstream/DelayEventStreamSpec.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,75 @@ | ||
package com.raquo.airstream.eventstream | ||
|
||
import com.raquo.airstream.AsyncUnitSpec | ||
import com.raquo.airstream.core.Observer | ||
import com.raquo.airstream.eventbus.EventBus | ||
import com.raquo.airstream.fixtures.{Effect, TestableOwner} | ||
import org.scalatest.BeforeAndAfter | ||
|
||
import scala.collection.mutable | ||
|
||
class DelayEventStreamSpec extends AsyncUnitSpec with BeforeAndAfter { | ||
|
||
implicit val owner = new TestableOwner | ||
|
||
val effects = mutable.Buffer[Effect[Int]]() | ||
|
||
val obs1 = Observer[Int](effects += Effect("obs1", _)) | ||
|
||
before { | ||
owner.killSubscriptions() | ||
effects.clear() | ||
} | ||
|
||
|
||
it("events are delayed, and purged on stop") { | ||
val bus = new EventBus[Int] | ||
val stream = bus.events.delay(30) | ||
|
||
val sub = stream.addObserver(obs1) | ||
|
||
delay { | ||
effects shouldEqual mutable.Buffer() | ||
|
||
// -- | ||
|
||
bus.writer.onNext(1) | ||
|
||
effects shouldEqual mutable.Buffer() | ||
|
||
}.flatMap[Unit] { _ => | ||
delay(30) { | ||
effects shouldEqual mutable.Buffer(Effect("obs1", 1)) | ||
effects.clear() | ||
|
||
bus.writer.onNext(2) | ||
bus.writer.onNext(3) | ||
|
||
effects shouldEqual mutable.Buffer() | ||
} | ||
}.flatMap[Unit] { _ => | ||
delay(30) { | ||
effects shouldEqual mutable.Buffer(Effect("obs1", 2), Effect("obs1", 3)) | ||
effects.clear() | ||
|
||
bus.writer.onNext(4) | ||
bus.writer.onNext(5) | ||
|
||
sub.kill() // this kills pending events even if we immediately restart | ||
|
||
effects shouldEqual mutable.Buffer() | ||
|
||
stream.addObserver(obs1) | ||
|
||
bus.writer.onNext(6) | ||
} | ||
}.flatMap { _ => | ||
delay(40) { // a bit extra margin for the last check just to be sure that we caught any events | ||
effects shouldEqual mutable.Buffer(Effect("obs1", 6)) | ||
effects.clear() | ||
assert(true) | ||
} | ||
} | ||
} | ||
|
||
} |