Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
1 deletion.
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
59 changes: 59 additions & 0 deletions
59
src/test/scala/com/tritondigital/counters/JitteredSchedulerTest.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,59 @@ | ||
package com.tritondigital.counters | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
import org.scalatest.WordSpec | ||
import _root_.akka.actor.ActorSystem | ||
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory} | ||
|
||
class JitteredSchedulerTest extends WordSpec with CustomMatchers { | ||
|
||
"The Jittered Scheduler" should { | ||
"schedule the given action with jitter" in withSut(ConfigFactory | ||
.load() | ||
.withValue("tritondigital_counters.publish_interval",ConfigValueFactory.fromAnyRef(100)) | ||
.withValue("tritondigital_counters.publish_interval_jitter", ConfigValueFactory.fromAnyRef(0.20))) { sut => | ||
sut.scheduler.start() | ||
val publish_interval = sut.system.settings.config.getDuration("tritondigital_counters.publish_interval", TimeUnit.MILLISECONDS) | ||
Thread.sleep(publish_interval * 10) | ||
sut.mock.count should be (10 +- 2) | ||
} | ||
|
||
"not crash" in withSut(ConfigFactory | ||
.load() | ||
.withValue("tritondigital_counters.publish_interval", ConfigValueFactory.fromAnyRef(100)) | ||
.withValue("tritondigital_counters.publish_interval_jitter", ConfigValueFactory.fromAnyRef(0.25))) { sut => | ||
sut.scheduler.start() | ||
val publish_interval = sut.system.settings.config.getDuration("tritondigital_counters.publish_interval", TimeUnit.MILLISECONDS) | ||
Thread.sleep(publish_interval * 10) | ||
sut.system.shutdown() | ||
Thread.sleep(1000) | ||
sut.mock.count should be (10 +- 3) | ||
} | ||
|
||
"raise an exception" in withSut(ConfigFactory | ||
.load() | ||
.withValue("tritondigital_counters.publish_interval", ConfigValueFactory.fromAnyRef(Long.MaxValue)) | ||
.withValue("tritondigital_counters.publish_interval_jitter", ConfigValueFactory.fromAnyRef(0.25))) { sut => | ||
sut.scheduler.start() | ||
a[IllegalArgumentException] should be thrownBy sut.scheduler.run() | ||
} | ||
} | ||
|
||
case class Sut(system: ActorSystem, mock: ActionMock, scheduler: JitteredScheduler) | ||
|
||
def withSut(config: Config)(f: Sut => Unit) = { | ||
val system = ActorSystem("JitteredScheduler", config) | ||
val mock = new ActionMock() | ||
val scheduler = new JitteredScheduler(system, mock.action()) | ||
f(Sut(system, mock, scheduler)) | ||
} | ||
|
||
class ActionMock { | ||
var count = 0 | ||
def action() = { | ||
count += 1 | ||
} | ||
} | ||
|
||
} |