Skip to content

Instantly share code, notes, and snippets.

@msulima
Created March 30, 2013 16:09
Show Gist options
  • Save msulima/5277258 to your computer and use it in GitHub Desktop.
Save msulima/5277258 to your computer and use it in GitHub Desktop.
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