Browse files

fix MockTimer to remove cancelled tasks from task list.

  • Loading branch information...
1 parent b74ddb0 commit 62914d21f7733d2201d0fc85bbf54f64f6ddd096 Wanli Yang committed Mar 21, 2011
View
4 finagle-core/src/test/scala/com/twitter/finagle/Timer.scala
@@ -5,11 +5,11 @@ import collection.mutable.ArrayBuffer
import com.twitter.util.{Time, Duration}
class MockTimer extends com.twitter.util.Timer {
- case class Task(when: Time, runner: Function0[Unit])
+ case class Task(var when: Time, runner: Function0[Unit])
extends com.twitter.util.TimerTask
{
var isCancelled = false
- def cancel() { isCancelled = true }
+ def cancel() { isCancelled = true; when = Time.now; tick() }
}
var isStopped = false
View
28 finagle-core/src/test/scala/com/twitter/finagle/service/ExpiringServiceSpec.scala
@@ -38,14 +38,12 @@ object ExpiringServiceSpec extends Specification with Mockito {
"cancel the timer when a request is issued" in {
service(123)
- timer.tasks must haveSize(1)
- timer.tasks.head.isCancelled must beTrue
+ timer.tasks must beEmpty
}
"restart the timer when the request finished" in {
service(123)
- timer.tasks must haveSize(1)
- timer.tasks.head.isCancelled must beTrue
+ timer.tasks must beEmpty
timeControl.advance(10.seconds)
timer.tick()
@@ -115,8 +113,7 @@ object ExpiringServiceSpec extends Specification with Mockito {
service.isAvailable must beFalse
there was one(underlying).release()
- timer.tasks must haveSize(1)
- timer.tasks.head.isCancelled must beTrue
+ timer.tasks must beEmpty
}
}
@@ -127,27 +124,24 @@ object ExpiringServiceSpec extends Specification with Mockito {
"expire after the given life time" in {
service(123)
- timer.tasks must haveSize(2)
- timer.tasks(0).isCancelled must beTrue
- timer.tasks(1).isCancelled must beFalse
+ timer.tasks must haveSize(1)
+ timer.tasks.head.isCancelled must beFalse
timeControl.advance(8.seconds)
timer.tick()
- timer.tasks must haveSize(2)
+ timer.tasks must haveSize(1)
+ timer.tasks.head.isCancelled must beFalse
+
promise() = Return(321)
-
- timer.tasks must haveSize(3)
- timer.tasks(0).isCancelled must beTrue
- timer.tasks(1).isCancelled must beFalse
- timer.tasks(2).isCancelled must beFalse
+ timer.tasks must haveSize(2)
+ timer.tasks forall(!_.isCancelled) must beTrue
there was no(underlying).release()
timeControl.advance(8.seconds)
timer.tick()
- timer.tasks must haveSize(1)
- timer.tasks forall(_.isCancelled) must beTrue
+ timer.tasks must beEmpty
service.isAvailable must beFalse
there was one(underlying).release()
}

0 comments on commit 62914d2

Please sign in to comment.