Created
August 28, 2015 17:04
-
-
Save SRGOM/c84f9a06dd8462b9a863 to your computer and use it in GitHub Desktop.
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
// libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.0-M7" % Test | |
import scala.scalajs.js.Date | |
import org.scalajs.dom | |
import dom.setTimeout | |
import org.scalatest._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Promise | |
import scala.concurrent.duration._ | |
case class Delayer[T]( | |
val callback: ( T => Unit ), | |
val delay: scala.concurrent.duration.Duration | |
) | |
{ | |
var timer: Int = 0 | |
def execute( arg: T ): Unit = { | |
dom.clearTimeout( timer ) | |
timer = dom.setTimeout( | |
() => { | |
callback( arg ) | |
}, | |
delay.toMillis | |
) | |
} | |
} | |
class DelayerSpec extends AsyncFunSuite{ | |
def executionContext = scala.concurrent.ExecutionContext.global | |
case class DelayedExecution( | |
callId: Int, | |
callTime: Date, | |
executionTime: Date | |
) | |
var executionList: List[ DelayedExecution ] = List.empty | |
def executionCallback( callParams: (Int, Date) ): Unit = | |
executionList = DelayedExecution( callParams._1, callParams._2, new Date ) :: executionList | |
test( "No skipping if properly displaced" ){ | |
executionList = List.empty | |
val delayTime = 100.millis | |
val delayer = new Delayer( executionCallback _, delayTime ) | |
delayer.execute( (1, new Date ) ) | |
setTimeout( () => delayer.execute( (2, new Date ) ), delayTime.toMillis ) | |
setTimeout( () => delayer.execute( (3, new Date ) ), delayTime.toMillis * 2.2 ) | |
//Mark the test as complete | |
val testCompletePromise = Promise[ Boolean ]() | |
setTimeout( | |
() => testCompletePromise.success( true ), | |
delayTime.toMillis * 3.3 | |
) | |
testCompletePromise.future map{ | |
_x => { | |
assert( executionList.size == 3 ) | |
assert( executionList.map( _.callId ).sum == 6 ) | |
assert( executionList.map( | |
e => { | |
val diff = (e.executionTime.valueOf() - e.callTime.valueOf()).toLong | |
if(diff >= delayTime.toMillis) 1 else 0 | |
} | |
).sum == 3 ) | |
} | |
} | |
} | |
test( "skip if in quick succession" ){ | |
executionList = List.empty | |
val delayTime = 400.millis | |
val delayer = new Delayer( executionCallback _, delayTime ) | |
delayer.execute( ( 10, new Date ) ) | |
setTimeout( () => delayer.execute( (20, new Date ) ), delayTime.toMillis / 4 ) | |
//gets in because #40 is (1+h) delayTime units after this (#30) | |
setTimeout( () => delayer.execute( (30, new Date ) ), delayTime.toMillis / 2 ) | |
//gets in because there is no next. | |
setTimeout( () => delayer.execute( (40, new Date ) ), delayTime.toMillis * 1.525 ) | |
val testCompletePromise = Promise[ Boolean ]() | |
setTimeout( | |
() => testCompletePromise.success( true ), | |
delayTime.toMillis * 2.55 //(1+h) delayTime after #40 | |
) | |
testCompletePromise.future map{ | |
_x => { | |
assert( executionList.size == 2 ) | |
assert( executionList.map( _.callId ).sum == 70 ) | |
assert( executionList.map( | |
e => { | |
val diff = (e.executionTime.valueOf() - e.callTime.valueOf()).toLong | |
if(diff >= delayTime.toMillis) 1 else 0 | |
} | |
).sum == 2 ) | |
} | |
} | |
} | |
override def newInstance: Suite with OneInstancePerTest = new DelayerSpec | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment