Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #780: improve fixed rate scheduling on JS #790

Merged
merged 1 commit into from Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,7 +20,8 @@ package monix.execution.internal
import minitest.TestSuite
import monix.execution.atomic.Atomic
import monix.execution.schedulers.{AsyncScheduler, StandardContext}
import monix.execution.{ExecutionModel, Scheduler, UncaughtExceptionReporter}
import monix.execution.{Cancelable, ExecutionModel, Scheduler, UncaughtExceptionReporter}

import scala.concurrent.Promise
import scala.concurrent.duration._

Expand Down Expand Up @@ -88,6 +89,26 @@ object AsyncSchedulerSuite extends TestSuite[Scheduler] {
}
}

testAsync("scheduleWithFixedRate should compensate for scheduling inaccuracy") { implicit s =>
import concurrent.duration._
val p = Promise[Unit]()
val startAt = s.clockMonotonic(MILLISECONDS)
var count = 0
lazy val c: Cancelable = s.scheduleAtFixedRate(Duration.Zero, 20.millis) {
count += 1
if (count == 500) {
c.cancel()
p.success(())
}
}
c // trigger evaluation

for (_ <- p.future) yield {
val duration = s.clockMonotonic(MILLISECONDS) - startAt
assert(Math.abs(duration - 10000) <= 20, "Error <= 20ms")
}
}

test("clockRealTime") { s =>
val t1 = System.currentTimeMillis()
val t2 = s.clockRealTime(MILLISECONDS)
Expand Down
Expand Up @@ -57,12 +57,13 @@ trait ReferenceScheduler extends Scheduler {
override def scheduleAtFixedRate(initialDelay: Long, period: Long, unit: TimeUnit, r: Runnable): Cancelable = {
val sub = OrderedCancelable()

def loop(initialDelayMs: Long, periodMs: Long): Unit =
def loop(initialDelayMs: Long, periodMs: Long): Unit = {
// Measuring the duration of the task + possible scheduler lag
val startedAtMillis = clockMonotonic(MILLISECONDS) + initialDelayMs

if (!sub.isCanceled) {
sub := scheduleOnce(initialDelayMs, MILLISECONDS, new Runnable {
def run(): Unit = {
// Measuring the duration of the task
val startedAtMillis = clockMonotonic(MILLISECONDS)
r.run()

val delay = {
Expand All @@ -76,6 +77,7 @@ trait ReferenceScheduler extends Scheduler {
}
})
}
}

val initialMs = MILLISECONDS.convert(initialDelay, unit)
val periodMs = MILLISECONDS.convert(period, unit)
Expand Down
Expand Up @@ -392,7 +392,6 @@ object TestScheduler {

val newID = state.lastID + 1
SingleAssignCancelable()

val task = Task(newID, r, state.clock + delay)
val cancelable = new Cancelable {
def cancel(): Unit = cancelTask(task)
Expand Down
Expand Up @@ -39,6 +39,11 @@ object ReferenceSchedulerSuite extends SimpleTestSuite {
underlying.scheduleOnce(initialDelay, unit, r)
}

class DummyTimeScheduler extends DummyScheduler() {
override def clockRealTime(unit: TimeUnit): Long = underlying.clockRealTime(unit)
override def clockMonotonic(unit: TimeUnit): Long = underlying.clockMonotonic(unit)
}

test("clockRealTime") {
val s = new DummyScheduler
val ws = s.withExecutionModel(SynchronousExecution)
Expand Down Expand Up @@ -73,7 +78,7 @@ object ReferenceSchedulerSuite extends SimpleTestSuite {
}

test("schedule at fixed rate") {
val s = new DummyScheduler
val s = new DummyTimeScheduler
val ws = s.withExecutionModel(SynchronousExecution)

var effect = 0
Expand Down