diff options
author | Heather Miller <heather.miller@epfl.ch> | 2012-08-05 12:43:02 +0200 |
---|---|---|
committer | Heather Miller <heather.miller@epfl.ch> | 2012-08-05 12:43:02 +0200 |
commit | 505cc713ec4546c5f9be1065e5fa3f7065451b8f (patch) | |
tree | 1a1b5606bdfdd52dd1be83e682114cb7bab19253 /test/files | |
parent | cb6066ee6136d78ceb8c3b5c06cefac998966dd0 (diff) | |
parent | 5b82a9702de3ffd5b131caf8c550877b476e8f9c (diff) | |
download | scala-505cc713ec4546c5f9be1065e5fa3f7065451b8f.tar.gz scala-505cc713ec4546c5f9be1065e5fa3f7065451b8f.tar.bz2 scala-505cc713ec4546c5f9be1065e5fa3f7065451b8f.zip |
Merge branch 'try-based-futures' of https://github.com/heathermiller/scala into try-based-futures
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/jvm/scala-concurrent-tck.scala | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test/files/jvm/scala-concurrent-tck.scala b/test/files/jvm/scala-concurrent-tck.scala index 36ab910593..976d98a337 100644 --- a/test/files/jvm/scala-concurrent-tck.scala +++ b/test/files/jvm/scala-concurrent-tck.scala @@ -926,6 +926,66 @@ trait CustomExecutionContext extends TestBase { testCallbackChainCustomEC() } +trait ExecutionContextPrepare extends TestBase { + val theLocal = new ThreadLocal[String] { + override protected def initialValue(): String = "" + } + + class PreparingExecutionContext extends ExecutionContext { + def delegate = ExecutionContext.global + + override def execute(runnable: Runnable): Unit = + delegate.execute(runnable) + + override def prepare[T, U](f: Try[T] => U): ExecutionContext = { + // save object stored in ThreadLocal storage + val localData = theLocal.get + new PreparingExecutionContext { + override def execute(runnable: Runnable): Unit = { + val wrapper = new Runnable { + override def run(): Unit = { + // now we're on the new thread + // put localData into theLocal + theLocal.set(localData) + runnable.run() + } + } + delegate.execute(wrapper) + } + } + } + + override def reportFailure(t: Throwable): Unit = + delegate.reportFailure(t) + } + + implicit val ec = new PreparingExecutionContext + + def testOnComplete(): Unit = once { + done => + theLocal.set("secret") + val fut = future { 42 } + fut onComplete { + case _ => + assert(theLocal.get == "secret") + done() + } + } + + def testMap(): Unit = once { + done => + theLocal.set("secret2") + val fut = future { 42 } + fut map { x => + assert(theLocal.get == "secret2") + done() + } + } + + testOnComplete() + testMap() +} + object Test extends App with FutureCallbacks @@ -935,6 +995,7 @@ with Promises with BlockContexts with Exceptions with CustomExecutionContext +with ExecutionContextPrepare { System.exit(0) } |