@@ -482,6 +482,45 @@ class ExpectationsTestCase: XCTestCase {
482
482
waitForExpectations ( timeout: 1 )
483
483
}
484
484
485
+ // CHECK: Test Case 'ExpectationsTestCase.test_expectationCreationOnSecondaryThread' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
486
+ // CHECK: Test Case 'ExpectationsTestCase.test_expectationCreationOnSecondaryThread' passed \(\d+\.\d+ seconds\)
487
+ func test_expectationCreationOnSecondaryThread( ) {
488
+ DispatchQueue . global ( ) . sync {
489
+ let foo = self . expectation ( description: " foo " )
490
+ foo. fulfill ( )
491
+ }
492
+ waitForExpectations ( timeout: 1 )
493
+
494
+ // Also test using an "explicit" wait API (which accepts an array of expectations) to wait on an
495
+ // expectation created on a non-main thread.
496
+ let foo : XCTestExpectation = DispatchQueue . global ( ) . sync {
497
+ let foo = self . expectation ( description: " foo " )
498
+ foo. fulfill ( )
499
+ return foo
500
+ }
501
+ wait ( for: [ foo] , timeout: 1 )
502
+ }
503
+
504
+ // CHECK: Test Case 'ExpectationsTestCase.test_expectationCreationWhileWaiting' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
505
+ // CHECK: Test Case 'ExpectationsTestCase.test_expectationCreationWhileWaiting' passed \(\d+\.\d+ seconds\)
506
+ func test_expectationCreationWhileWaiting( ) {
507
+ let foo = expectation ( description: " foo " )
508
+
509
+ DispatchQueue . main. async {
510
+ let bar = self . expectation ( description: " bar " )
511
+ bar. fulfill ( )
512
+
513
+ foo. fulfill ( )
514
+ }
515
+
516
+ // Waits on `foo` with a generous timeout, since we want to ensure we don't proceed until the dispatched block has completed.
517
+ waitForExpectations ( timeout: 100 )
518
+
519
+ // Waits on `bar` with a zero timeout, since by this point we expect the dispatched block to have completed and thus
520
+ // the second expectation should already be fulfilled.
521
+ waitForExpectations ( timeout: 0 )
522
+ }
523
+
485
524
// CHECK: Test Case 'ExpectationsTestCase.test_runLoopInsideDispatch' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
486
525
// CHECK: .*[/\\]Tests[/\\]Functional[/\\]Asynchronous[/\\]Expectations[/\\]main.swift:[[@LINE+8]]: error: ExpectationsTestCase.test_runLoopInsideDispatch : Asynchronous wait failed - Exceeded timeout of 0.5 seconds, with unfulfilled expectations: foo
487
526
// CHECK: Test Case 'ExpectationsTestCase.test_runLoopInsideDispatch' failed \(\d+\.\d+ seconds\)
@@ -545,16 +584,18 @@ class ExpectationsTestCase: XCTestCase {
545
584
546
585
// Regressions
547
586
( " test_fulfillmentOnSecondaryThread " , test_fulfillmentOnSecondaryThread) ,
587
+ ( " test_expectationCreationOnSecondaryThread " , test_expectationCreationOnSecondaryThread) ,
588
+ ( " test_expectationCreationWhileWaiting " , test_expectationCreationWhileWaiting) ,
548
589
( " test_runLoopInsideDispatch " , test_runLoopInsideDispatch) ,
549
590
]
550
591
} ( )
551
592
}
552
593
// CHECK: Test Suite 'ExpectationsTestCase' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
553
- // CHECK: \t Executed 32 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
594
+ // CHECK: \t Executed 34 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
554
595
555
596
XCTMain ( [ testCase ( ExpectationsTestCase . allTests) ] )
556
597
557
598
// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
558
- // CHECK: \t Executed 32 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
599
+ // CHECK: \t Executed 34 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
559
600
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
560
- // CHECK: \t Executed 32 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
601
+ // CHECK: \t Executed 34 tests, with 16 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
0 commit comments