summaryrefslogtreecommitdiff
path: root/test/files/jvm/scala-concurrent-tck.scala
blob: 7197c1d8539497a66ff4bc2a27ebe089d83201f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
import scala.concurrent.{
  Future,
  Promise,
  TimeoutException,
  SyncVar,
  ExecutionException,
  ExecutionContext,
  CanAwait,
  Await
}
import scala.concurrent.blocking
import scala.util.{ Try, Success, Failure }
import scala.concurrent.duration.Duration
import scala.reflect.{ classTag, ClassTag }
import scala.tools.partest.TestUtil.intercept

trait TestBase {
  trait Done { def apply(proof: => Boolean): Unit }
  def once(body: Done => Unit) {
    import java.util.concurrent.{ LinkedBlockingQueue, TimeUnit }
    val q = new LinkedBlockingQueue[Try[Boolean]]
    body(new Done {
      def apply(proof: => Boolean): Unit = q offer Try(proof)
    })
    assert(q.poll(2000, TimeUnit.MILLISECONDS).get)
    // Check that we don't get more than one completion
    assert(q.poll(50, TimeUnit.MILLISECONDS) eq null)
  }
}


trait FutureCallbacks extends TestBase {
  import ExecutionContext.Implicits._

  def testOnSuccess(): Unit = once {
    done =>
    var x = 0
    val f = Future { x = 1 }
    f onSuccess { case _ => done(x == 1) }
  }

  def testOnSuccessWhenCompleted(): Unit = once {
    done =>
    var x = 0
    val f = Future { x = 1 }
    f onSuccess {
      case _ if x == 1 =>
      x = 2
      f onSuccess {  case _ => done(x == 2) }
    }
  }

  def testOnSuccessWhenFailed(): Unit = once {
    done =>
    val f = Future[Unit] { throw new Exception }
    f onSuccess { case _ => done(false) }
    f onFailure { case _ => done(true) }
  }

  def testOnFailure(): Unit = once {
    done =>
    val f = Future[Unit] { throw new Exception }
    f onSuccess { case _ => done(false) }
    f onFailure { case _ => done(true) }
  }

  def testOnFailureWhenSpecialThrowable(num: Int, cause: Throwable): Unit = once {
    done =>
    val f = Future[Unit] { throw cause }
    f onSuccess { case _ => done(false) }
    f onFailure {
      case e: ExecutionException if e.getCause == cause => done(true)
      case _ => done(false)
    }
  }

  def testOnFailureWhenTimeoutException(): Unit = once {
    done =>
    val f = Future[Unit] { throw new TimeoutException() }
    f onSuccess { case _ => done(false) }
    f onFailure {
      case e: TimeoutException => done(true)
      case _ => done(false)
    }
  }

  def testThatNestedCallbacksDoNotYieldStackOverflow(): Unit = {
    val promise = Promise[Int]
    (0 to 10000).map(Future(_)).foldLeft(promise.future)((f1, f2) => f2.flatMap(i => f1))
    promise.success(-1)
  }

  def stressTestNumberofCallbacks(): Unit = once {
    done =>
      val promise = Promise[Unit]
      val otherPromise = Promise[Unit]
      def attachMeaninglessCallbacksTo(f: Future[Any]): Unit = (1 to 1000).foreach(_ => f.onComplete(_ => ()))
      attachMeaninglessCallbacksTo(promise.future)
      val future = promise.future.flatMap { _ =>
        attachMeaninglessCallbacksTo(otherPromise.future)
        otherPromise.future
      }
      val numbers = new java.util.concurrent.ConcurrentHashMap[Int, Unit]()
      (0 to 10000) foreach { x => numbers.put(x, ()) }
      Future.sequence((0 to 10000) map { x => future.andThen({ case _ => numbers.remove(x) }) }) onComplete {
        _ => done(numbers.isEmpty)
      }
      promise.success(())
      otherPromise.success(())
  }

  testOnSuccess()
  testOnSuccessWhenCompleted()
  testOnSuccessWhenFailed()
  testOnFailure()
  testOnFailureWhenSpecialThrowable(5, new Error)
  // testOnFailureWhenSpecialThrowable(6, new scala.util.control.ControlThrowable { })
  //TODO: this test is currently problematic, because NonFatal does not match InterruptedException
  //testOnFailureWhenSpecialThrowable(7, new InterruptedException)
  testThatNestedCallbacksDoNotYieldStackOverflow()
  testOnFailureWhenTimeoutException()
  stressTestNumberofCallbacks()
}


trait FutureCombinators extends TestBase {
  import ExecutionContext.Implicits._

  def testMapSuccess(): Unit = once {
    done =>
      val f = Future { 5 }
      val g = f map { x => "result: " + x }
      g onSuccess { case s => done(s == "result: 5") }
      g onFailure { case _ => done(false) }
  }

  def testMapFailure(): Unit = once {
    done =>
      val f = Future[Unit] { throw new Exception("exception message") }
      val g = f map { x => "result: " + x }
      g onSuccess { case _ => done(false) }
      g onFailure { case t => done(t.getMessage() == "exception message") }
  }

  def testMapSuccessPF(): Unit = once {
    done =>
      val f = Future { 5 }
      val g = f map { case r => "result: " + r }
      g onSuccess { case s => done(s == "result: 5") }
      g onFailure { case _ => done(false) }
  }

  def testTransformSuccess(): Unit = once {
    done =>
      val f = Future { 5 }
      val g = f.transform(r => "result: " + r, identity)
      g onSuccess { case s => done(s == "result: 5") }
      g onFailure { case _ => done(false) }
  }

  def testTransformSuccessPF(): Unit = once {
    done =>
      val f = Future { 5 }
      val g = f.transform( { case r => "result: " + r }, identity)
      g onSuccess { case s => done(s == "result: 5") }
      g onFailure { case _ => done(false) }
  }

def testTransformFailure(): Unit = once {
    done =>
      val transformed = new Exception("transformed")
      val f = Future { throw new Exception("expected") }
      val g = f.transform(identity, _ => transformed)
      g onSuccess { case _ => done(false) }
      g onFailure { case e => done(e eq transformed) }
  }

  def testTransformFailurePF(): Unit = once {
    done =>
      val e = new Exception("expected")
      val transformed = new Exception("transformed")
      val f = Future[Unit] { throw e }
      val g = f.transform(identity, { case `e` => transformed })
      g onSuccess { case _ => done(false) }
      g onFailure { case e => done(e eq transformed) }
  }

  def testTransformResultToResult(): Unit = once {
    done =>
      Future("foo").transform {
        case Success(s) => Success(s.toUpperCase)
        case Failure(f) => throw new Exception("test failed")
      } onComplete {
        case Success("FOO") => done(true)
        case _ => done(false)
      }
  }

  def testTransformResultToFailure(): Unit = once {
    done => 
      val e = new Exception("expected")
      Future("foo").transform {
        case Success(s) => Failure(e)
        case Failure(f) => throw new Exception("test failed")
      } onComplete {
        case Failure(`e`) => done(true)
        case _ => done(false)
      }
  }

  def testTransformFailureToResult(): Unit = once {
    done =>
    val e = "foo"
      Future(throw new Exception("initial")).transform {
        case Success(s) => throw new Exception("test failed")
        case Failure(f) => Success(e)
      } onComplete {
        case Success(`e`) => done(true)
        case _ => done(false)
      }
  }

  def testTransformFailureToFailure(): Unit = once {
    done =>
      val e = new Exception("expected")
      Future(throw new Exception("initial")).transform {
        case Success(s) => throw new Exception("test failed")
        case Failure(f) => Failure(e)
      } onComplete {
        case Failure(`e`) => done(true)
        case _ => done(false)
      }
  }

    def testTransformWithResultToResult(): Unit = once {
    done =>
      Future("foo").transformWith {
        case Success(s) => Future(s.toUpperCase)
        case Failure(f) => throw new Exception("test failed")
      } onComplete {
        case Success("FOO") => done(true)
        case _ => done(false)
      }
  }

  def testTransformWithResultToFailure(): Unit = once {
    done => 
      val e = new Exception("expected")
      Future("foo").transformWith {
        case Success(s) => Future(throw e)
        case Failure(f) => throw new Exception("test failed")
      } onComplete {
        case Failure(`e`) => done(true)
        case _ => done(false)
      }
  }

  def testTransformWithFailureToResult(): Unit = once {
    done =>
    val e = "foo"
      Future(throw new Exception("initial")).transformWith {
        case Success(s) => throw new Exception("test failed")
        case Failure(f) => Future(e)
      } onComplete {
        case Success(`e`) => done(true)
        case _ => done(false)
      }
  }

  def testTransformWithFailureToFailure(): Unit = once {
    done =>
      val e = new Exception("expected")
      Future(throw new Exception("initial")).transformWith {
        case Success(s) => throw new Exception("test failed")
        case Failure(f) => Future(throw e)
      } onComplete {
        case Failure(`e`) => done(true)
        case _ => done(false)
      }
  }

  def testFoldFailure(): Unit = once {
    done =>
      val f = Future[Unit] { throw new Exception("expected") }
      val g = f.transform(r => "result: " + r, identity)
      g onSuccess { case _ => done(false) }
      g onFailure { case t => done(t.getMessage() == "expected") }
  }

  def testFlatMapSuccess(): Unit = once {
    done =>
      val f = Future { 5 }
      val g = f flatMap { _ => Future { 10 } }
      g onSuccess { case x => done(x == 10) }
      g onFailure { case _ => done(false) }
  }

  def testFlatMapFailure(): Unit = once {
    done =>
      val f = Future[Unit] { throw new Exception("expected") }
      val g = f flatMap { _ => Future { 10 } }
      g onSuccess { case _ => done(false) }
      g onFailure { case t => done(t.getMessage() == "expected") }
  }

  def testFlatMapDelayed(): Unit = once {
    done =>
      val f = Future { 5 }
      val p = Promise[Int]
      val g = f flatMap { _ => p.future }
      g onSuccess { case x => done(x == 10) }
      g onFailure { case _ => done(false) }
      p.success(10)
  }

  def testFilterSuccess(): Unit = once {
    done =>
      val f = Future { 4 }
      val g = f filter { _ % 2 == 0 }
      g onSuccess { case x: Int => done(x == 4) }
      g onFailure { case _ => done(false) }
  }

  def testFilterFailure(): Unit = once {
    done =>
      val f = Future { 4 }
      val g = f filter { _ % 2 == 1 }
      g onSuccess { case x: Int => done(false) }
      g onFailure {
        case e: NoSuchElementException => done(true)
        case _ => done(false)
      }
  }

  def testCollectSuccess(): Unit = once {
    done =>
      val f = Future { -5 }
      val g = f collect { case x if x < 0 => -x }
      g onSuccess { case x: Int => done(x == 5) }
      g onFailure { case _ => done(false) }
  }

  def testCollectFailure(): Unit = once {
    done =>
      val f = Future { -5 }
      val g = f collect { case x if x > 0 => x * 2 }
      g onSuccess { case _ => done(false) }
      g onFailure {
        case e: NoSuchElementException => done(true)
        case _ => done(false)
      }
  }

  /* TODO: Test for NonFatal in collect (more of a regression test at this point).
   */

  def testForeachSuccess(): Unit = once {
    done =>
      val p = Promise[Int]()
      val f = Future[Int] { 5 }
      f foreach { x => p.success(x * 2) }
      val g = p.future

      g.onSuccess { case res: Int => done(res == 10) }
      g.onFailure { case _ => done(false) }
  }

  def testForeachFailure(): Unit = once {
    done =>
      val p = Promise[Int]()
      val f = Future[Int] { throw new Exception }
      f foreach { x => p.success(x * 2) }
      f onFailure { case _ => p.failure(new Exception) }
      val g = p.future

      g.onSuccess { case _ => done(false) }
      g.onFailure { case _ => done(true) }
  }

  def testRecoverSuccess(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future {
      throw cause
    } recover {
      case re: RuntimeException =>
        "recovered" }
    f onSuccess { case x => done(x == "recovered") }
    f onFailure { case any => done(false) }
  }

  def testRecoverFailure(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future {
      throw cause
    } recover {
      case te: TimeoutException => "timeout"
    }
    f onSuccess { case _ => done(false) }
    f onFailure { case any => done(any == cause) }
  }

  def testRecoverWithSuccess(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future {
      throw cause
    } recoverWith {
      case re: RuntimeException =>
        Future { "recovered" }
    }
    f onSuccess { case x => done(x == "recovered") }
    f onFailure { case any => done(false) }
  }

  def testRecoverWithFailure(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future {
      throw cause
    } recoverWith {
      case te: TimeoutException =>
        Future { "timeout" }
    }
    f onSuccess { case x => done(false) }
    f onFailure { case any => done(any == cause) }
  }

  def testZipSuccess(): Unit = once {
    done =>
    val f = Future { 5 }
    val g = Future { 6 }
    val h = f zip g
    h onSuccess { case (l: Int, r: Int) => done(l+r == 11) }
    h onFailure { case _ => done(false) }
  }

  def testZipFailureLeft(): Unit = once {
    done =>
    val cause = new Exception("expected")
    val f = Future { throw cause }
    val g = Future { 6 }
    val h = f zip g
    h onSuccess { case _ => done(false) }
    h onFailure { case e: Exception => done(e.getMessage == "expected") }
  }

  def testZipFailureRight(): Unit = once {
    done =>
    val cause = new Exception("expected")
    val f = Future { 5 }
    val g = Future { throw cause }
    val h = f zip g
    h onSuccess { case _ => done(false) }
    h onFailure { case e: Exception => done(e.getMessage == "expected") }
  }

  def testFallbackTo(): Unit = once {
    done =>
    val f = Future { sys.error("failed") }
    val g = Future { 5 }
    val h = f fallbackTo g
    h onSuccess { case x: Int => done(x == 5) }
    h onFailure { case _ => done(false) }
  }

  def testFallbackToFailure(): Unit = once {
    done =>
    val cause = new Exception
    val f = Future { throw cause }
    val g = Future { sys.error("failed") }
    val h = f fallbackTo g

    h onSuccess { case _ => done(false) }
    h onFailure { case e => done(e eq cause) }
  }

  def testFallbackToThis(): Unit = {
    def check(f: Future[Int]) = assert((f fallbackTo f) eq f)

    check(Future { 1 })
    check(Future.successful(1))
    check(Future.failed[Int](new Exception))
  }

  testMapSuccess()
  testMapFailure()
  testFlatMapSuccess()
  testFlatMapFailure()
  testFlatMapDelayed()
  testFilterSuccess()
  testFilterFailure()
  testCollectSuccess()
  testCollectFailure()
  testForeachSuccess()
  testForeachFailure()
  testRecoverSuccess()
  testRecoverFailure()
  testRecoverWithSuccess()
  testRecoverWithFailure()
  testZipSuccess()
  testZipFailureLeft()
  testZipFailureRight()
  testFallbackTo()
  testFallbackToFailure()
  testTransformSuccess()
  testTransformSuccessPF()
  testTransformFailure()
  testTransformFailurePF()
  testTransformResultToResult()
  testTransformResultToFailure()
  testTransformFailureToResult()
  testTransformFailureToFailure()
  testTransformWithResultToResult()
  testTransformWithResultToFailure()
  testTransformWithFailureToResult()
  testTransformWithFailureToFailure()
}


trait FutureProjections extends TestBase {
  import ExecutionContext.Implicits._

  def testFailedFailureOnComplete(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future { throw cause }
    f.failed onComplete {
      case Success(t) => done(t == cause)
      case Failure(t) => done(false)
    }
  }

  def testFailedFailureOnSuccess(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future { throw cause }
    f.failed onSuccess { case t => done(t == cause) }
  }

  def testFailedSuccessOnComplete(): Unit = once {
    done =>
    val f = Future { 0 }
    f.failed onComplete {
      case Failure(_: NoSuchElementException) => done(true)
      case _ => done(false)
    }
  }

  def testFailedSuccessOnFailure(): Unit = once {
    done =>
    val f = Future { 0 }
    f.failed onFailure {
      case e: NoSuchElementException => done(true)
      case _ => done(false)
    }
    f.failed onSuccess { case _ => done(false) }
  }

  def testFailedFailureAwait(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future { throw cause }
    done(Await.result(f.failed, Duration(500, "ms")) == cause)
  }

  def testFailedSuccessAwait(): Unit = once {
    done =>
    val f = Future { 0 }
    try {
      Await.result(f.failed, Duration(500, "ms"))
      done(false)
    } catch {
      case nsee: NoSuchElementException => done(true)
      case _: Throwable => done(false)
    }
  }

  def testAwaitPositiveDuration(): Unit = once { done =>
    val p = Promise[Int]()
    val f = p.future
    Future {
      intercept[IllegalArgumentException] { Await.ready(f, Duration.Undefined) }
      p.success(0)
      Await.ready(f, Duration.Zero)
      Await.ready(f, Duration(500, "ms"))
      Await.ready(f, Duration.Inf)
      done(true)
    } onFailure { case x => done(throw x) }
  }

  def testAwaitNegativeDuration(): Unit = once { done =>
    val f = Promise().future
    Future {
      intercept[TimeoutException] { Await.ready(f, Duration.Zero) }
      intercept[TimeoutException] { Await.ready(f, Duration.MinusInf) }
      intercept[TimeoutException] { Await.ready(f, Duration(-500, "ms")) }
      done(true)
    } onFailure { case x => done(throw x) }
  }

  testFailedFailureOnComplete()
  testFailedFailureOnSuccess()
  testFailedSuccessOnComplete()
  testFailedSuccessOnFailure()
  testFailedFailureAwait()
  testFailedSuccessAwait()
  testAwaitPositiveDuration()
  testAwaitNegativeDuration()
}


trait Blocking extends TestBase {
  import ExecutionContext.Implicits._

  def testAwaitSuccess(): Unit = once {
    done =>
    val f = Future { 0 }
    done(Await.result(f, Duration(500, "ms")) == 0)
  }

  def testAwaitFailure(): Unit = once {
    done =>
    val cause = new RuntimeException
    val f = Future { throw cause }
    try {
      Await.result(f, Duration(500, "ms"))
      done(false)
    } catch {
      case  t: Throwable => done(t == cause)
    }
  }

  def testFQCNForAwaitAPI(): Unit = once {
    done =>
    done(classOf[CanAwait].getName == "scala.concurrent.CanAwait" &&
         Await.getClass.getName == "scala.concurrent.Await")
  }

  testAwaitSuccess()
  testAwaitFailure()
  testFQCNForAwaitAPI()
}

trait BlockContexts extends TestBase {
  import ExecutionContext.Implicits._
  import scala.concurrent.{ Await, Awaitable, BlockContext }

  private def getBlockContext(body: => BlockContext): BlockContext = {
    Await.result(Future { body }, Duration(500, "ms"))
  }

  // test outside of an ExecutionContext
  def testDefaultOutsideFuture(): Unit = {
    val bc = BlockContext.current
    assert(bc.getClass.getName.contains("DefaultBlockContext"))
  }

  // test BlockContext in our default ExecutionContext
  def testDefaultFJP(): Unit = {
    val bc = getBlockContext(BlockContext.current)
    assert(bc.isInstanceOf[java.util.concurrent.ForkJoinWorkerThread])
  }

  // test BlockContext inside BlockContext.withBlockContext
  def testPushCustom(): Unit = {
    val orig = BlockContext.current
    val customBC = new BlockContext() {
      override def blockOn[T](thunk: =>T)(implicit permission: CanAwait): T = orig.blockOn(thunk)
    }

    val bc = getBlockContext({
      BlockContext.withBlockContext(customBC) {
        BlockContext.current
      }
    })

    assert(bc eq customBC)
  }

  // test BlockContext after a BlockContext.push
  def testPopCustom(): Unit = {
    val orig = BlockContext.current
    val customBC = new BlockContext() {
      override def blockOn[T](thunk: =>T)(implicit permission: CanAwait): T = orig.blockOn(thunk)
    }

    val bc = getBlockContext({
      BlockContext.withBlockContext(customBC) {}
      BlockContext.current
    })

    assert(bc ne customBC)
  }

  testDefaultOutsideFuture()
  testDefaultFJP()
  testPushCustom()
  testPopCustom()
}

trait Promises extends TestBase {
  import ExecutionContext.Implicits._

  def testSuccess(): Unit = once {
    done =>
    val p = Promise[Int]()
    val f = p.future

    f onSuccess { case x => done(x == 5) }
    f onFailure { case any => done(false) }

    p.success(5)
  }

  def testFailure(): Unit = once {
    done =>
    val e = new Exception("expected")
    val p = Promise[Int]()
    val f = p.future

    f onSuccess { case x => done(false) }
    f onFailure { case any => done(any eq e) }

    p.failure(e)
  }

  testSuccess()
  testFailure()
}


trait Exceptions extends TestBase {
  import ExecutionContext.Implicits._

}

trait GlobalExecutionContext extends TestBase {
  def testNameOfGlobalECThreads(): Unit = once {
    done => Future({
        val expectedName = "scala-execution-context-global-"+ Thread.currentThread.getId
        done(expectedName == Thread.currentThread.getName)
      })(ExecutionContext.global)
  }

  testNameOfGlobalECThreads()
}

trait CustomExecutionContext extends TestBase {
  import scala.concurrent.{ ExecutionContext, Awaitable }

  def defaultEC = ExecutionContext.global

  val inEC = new java.lang.ThreadLocal[Int]() {
    override def initialValue = 0
  }

  def enterEC() = inEC.set(inEC.get + 1)
  def leaveEC() = inEC.set(inEC.get - 1)
  def assertEC() = assert(inEC.get > 0)
  def assertNoEC() = assert(inEC.get == 0)

  class CountingExecutionContext extends ExecutionContext {
    val _count = new java.util.concurrent.atomic.AtomicInteger(0)
    def count = _count.get

    def delegate = ExecutionContext.global

    override def execute(runnable: Runnable) = {
      _count.incrementAndGet()
      val wrapper = new Runnable() {
        override def run() = {
          enterEC()
          try {
            runnable.run()
          } finally {
            leaveEC()
          }
        }
      }
      delegate.execute(wrapper)
    }

    override def reportFailure(t: Throwable): Unit = {
      System.err.println("Failure: " + t.getClass.getSimpleName + ": " + t.getMessage)
      delegate.reportFailure(t)
    }
  }

  def countExecs(block: (ExecutionContext) => Unit): Int = {
    val context = new CountingExecutionContext()
    block(context)
    context.count
  }

  def testOnSuccessCustomEC(): Unit = {
    val count = countExecs { implicit ec =>
      blocking {
        once { done =>
          val f = Future(assertNoEC())(defaultEC)
          f onSuccess {
            case _ =>
              assertEC()
              done(true)
          }
          assertNoEC()
        }
      }
    }

    // should be onSuccess, but not future body
    assert(count == 1)
  }

  def testKeptPromiseCustomEC(): Unit = {
    val count = countExecs { implicit ec =>
      blocking {
        once { done =>
          val f = Promise.successful(10).future
          f onSuccess {
            case _ =>
              assertEC()
              done(true)
          }
        }
      }
    }

    // should be onSuccess called once in proper EC
    assert(count == 1)
  }

  def testCallbackChainCustomEC(): Unit = {
    val count = countExecs { implicit ec =>
      blocking {
        once { done =>
          assertNoEC()
          val addOne = { x: Int => assertEC(); x + 1 }
          val f = Promise.successful(10).future
          f.map(addOne).filter { x =>
             assertEC()
             x == 11
           } flatMap { x =>
             Promise.successful(x + 1).future.map(addOne).map(addOne)
           } onComplete {
            case Failure(t) =>
              done(throw new AssertionError("error in test: " + t.getMessage, t))
            case Success(x) =>
              assertEC()
              done(x == 14)
          }
          assertNoEC()
        }
      }
    }

    // the count is not defined (other than >=1)
    // due to the batching optimizations.
    assert(count >= 1)
  }

  testOnSuccessCustomEC()
  testKeptPromiseCustomEC()
  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(): 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 _ => done(theLocal.get == "secret") }
  }

  def testMap(): Unit = once {
    done =>
    theLocal.set("secret2")
    val fut = Future { 42 }
    fut map { x => done(theLocal.get == "secret2") }
  }

  testOnComplete()
  testMap()
}

object Test
extends App
with FutureCallbacks
with FutureCombinators
with FutureProjections
with Promises
with BlockContexts
with Exceptions
with GlobalExecutionContext
with CustomExecutionContext
with ExecutionContextPrepare
{
  System.exit(0)
}