|
Scala example source code file (TimerTest.scala)
The TimerTest.scala Scala example source codepackage scalaz package concurrent import ConcurrentTest._ import scalaz.syntax.either._ object TimerTest extends SpecLite { def withTimer[T](expression: Timer => T): T = { val timer = new Timer(10) try { expression(timer) } finally { timer.stop() } } "Timer" should { "stop normally" in { withTimer(timer => ()) } "handle stop being called repeatedly" in { withTimer{timer => timer.stop() } } "valueWait produces a value after the specified timeout" in { withTimer{timer => val start = System.currentTimeMillis withTimeout(5000){ timer.valueWait("Test", 100).unsafePerformSync must_== "Test" (System.currentTimeMillis - start) >= 100 } } } "withTimeout(Future...) produces a Timeout if the timeout is exceeded" in { withTimer{timer => val future = timer.withTimeout(Future{Thread.sleep(500); "Test"}, 100) withTimeout(5000){ future.unsafePerformSync must_== Timeout.left } } } "produces the result of the Future if the timeout is not exceeded" in { withTimer{timer => val future = timer.withTimeout(Future{Thread.sleep(50); "Test"}, 200) withTimeout(5000){ future.unsafePerformSync must_== "Test".right } } } } } Other Scala examples (source code examples)Here is a short list of links related to this Scala TimerTest.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.