Created
March 30, 2013 16:09
-
-
Save msulima/5277258 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
import java.util.concurrent.Semaphore | |
import scala.concurrent.Future | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import org.scalatest.FunSuite | |
class CallbacksTest extends FunSuite { | |
test("Callback are complicated") { | |
var result: Int = 0; val semaphore = new Semaphore(1, true) | |
generateNumber(generatedNumber => addThree(generatedNumber, | |
addingResult => { | |
result = addingResult | |
semaphore.release() | |
})) | |
semaphore.acquire() | |
assert(result == 10) | |
} | |
private def generateNumber[A](onSuccess: Int => Unit) = Future { | |
Thread.sleep(100); onSuccess(7) | |
} | |
private def addThree[A](value: Int, onSuccess: Int => Unit): Unit = onSuccess(value + 3) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment