@@ -137,7 +137,7 @@ class SwiftTaskTests: _TestCase
137
137
self . wait ( )
138
138
}
139
139
140
- func testFulfill_successTaskFulfill ( )
140
+ func testFulfill_success_innerTask_fulfill ( )
141
141
{
142
142
var expect = self . expectationWithDescription ( __FUNCTION__)
143
143
@@ -169,7 +169,7 @@ class SwiftTaskTests: _TestCase
169
169
self . wait ( )
170
170
}
171
171
172
- func testFulfill_successTaskReject ( )
172
+ func testFulfill_success_innerTask_reject ( )
173
173
{
174
174
var expect = self . expectationWithDescription ( __FUNCTION__)
175
175
@@ -317,7 +317,7 @@ class SwiftTaskTests: _TestCase
317
317
self . wait ( )
318
318
}
319
319
320
- func testReject_failureTaskFulfill ( )
320
+ func testReject_failure_innerTask_fulfill ( )
321
321
{
322
322
var expect = self . expectationWithDescription ( __FUNCTION__)
323
323
@@ -350,7 +350,7 @@ class SwiftTaskTests: _TestCase
350
350
self . wait ( )
351
351
}
352
352
353
- func testReject_failureTaskReject ( )
353
+ func testReject_failure_innerTask_reject ( )
354
354
{
355
355
var expect = self . expectationWithDescription ( __FUNCTION__)
356
356
@@ -473,7 +473,7 @@ class SwiftTaskTests: _TestCase
473
473
self . wait ( )
474
474
}
475
475
476
- func testProgress_async_then ( )
476
+ func testProgress_innerTask_then ( )
477
477
{
478
478
// NOTE: this is async test
479
479
if !self . isAsync { return }
@@ -502,7 +502,7 @@ class SwiftTaskTests: _TestCase
502
502
self . wait ( )
503
503
}
504
504
505
- func testProgress_async_success ( )
505
+ func testProgress_innerTask_success ( )
506
506
{
507
507
// NOTE: this is async test
508
508
if !self . isAsync { return }
@@ -531,7 +531,7 @@ class SwiftTaskTests: _TestCase
531
531
self . wait ( )
532
532
}
533
533
534
- func testProgress_async_failure ( )
534
+ func testProgress_innerTask_failure ( )
535
535
{
536
536
// NOTE: this is async test
537
537
if !self . isAsync { return }
@@ -606,7 +606,7 @@ class SwiftTaskTests: _TestCase
606
606
self . wait ( )
607
607
}
608
608
609
- func testCancel_thenTask ( )
609
+ func testCancel_then_innerTask ( )
610
610
{
611
611
var expect = self . expectationWithDescription ( __FUNCTION__)
612
612
@@ -673,30 +673,31 @@ class SwiftTaskTests: _TestCase
673
673
674
674
}
675
675
676
- // pause at time between _interruptableTask's 1st & 2nd delay (t=0.3 )
676
+ // pause at t=0.3 ( between _interruptableTask's 1st & 2nd delay before pause-check )
677
677
Async . main ( after: 0.3 ) {
678
678
679
679
task. pause ( )
680
680
681
681
XCTAssertEqual ( task. state, TaskState . Paused)
682
- XCTAssertTrue ( task. progress? == 2 )
682
+ XCTAssertTrue ( task. progress? == 2 , " `task` should be progressed halfway. " )
683
683
684
- // resume after 0.3sec ( t=0.6)
684
+ // resume at t=0.6
685
685
Async . main ( after: 0.3 ) {
686
686
687
687
XCTAssertEqual ( task. state, TaskState . Paused)
688
- XCTAssertTrue ( task. progress? == 2 )
688
+ XCTAssertTrue ( task. progress? == 2 , " `task` should pause progressing. " )
689
689
690
690
task. resume ( )
691
- XCTAssertEqual ( task. state, TaskState . Running)
691
+
692
+ XCTAssertEqual ( task. state, TaskState . Running, " `task` should start running again. " )
692
693
693
694
}
694
695
}
695
696
696
697
self . wait ( )
697
698
}
698
699
699
- func testPauseResume_async_then ( )
700
+ func testPauseResume_innerTask ( )
700
701
{
701
702
// NOTE: this is async test
702
703
if !self . isAsync { return }
@@ -706,7 +707,7 @@ class SwiftTaskTests: _TestCase
706
707
let task = _interruptableTask ( progressCount: 5 )
707
708
weak var innerTask : _InterruptableTask ?
708
709
709
- // chain async-task with then
710
+ // chain async-task with ` then`
710
711
let task2 = task. then { [ weak self] _ -> _InterruptableTask in
711
712
innerTask = _interruptableTask ( progressCount: 5 )
712
713
return innerTask!
@@ -717,138 +718,43 @@ class SwiftTaskTests: _TestCase
717
718
expect. fulfill ( )
718
719
}
719
720
720
- // pause at time between _interruptableTask's 1st & 2nd delay (t=0.3 )
721
+ // pause at t=0.3 ( between _interruptableTask's 1st & 2nd delay before pause-check )
721
722
Async . main ( after: 0.3 ) {
722
723
723
- // NOTE: parentTask should also be paused (if not, `task` will never be fulfilled/rejected)
724
+ // NOTE: task2 will be paused,
724
725
task2. pause ( )
725
726
726
- XCTAssertNil ( innerTask, " `innerTask` has not been created yet. " )
727
-
728
727
XCTAssertEqual ( task2. state, TaskState . Paused)
729
728
XCTAssertNil ( task2. progress, " `task2.progress` should be nil because `innerTask.progress()` has not been invoked yet. " )
730
729
731
- XCTAssertEqual ( task. state, TaskState . Paused, " Parent task should also be paused. " )
732
- XCTAssertTrue ( task. progress? == 2 )
730
+ XCTAssertNil ( innerTask, " `innerTask` should NOT be created yet. " )
733
731
734
- // resume after 0.3sec (t=0.6)
735
- Async . main ( after: 0.3 ) {
736
-
737
- XCTAssertEqual ( task2. state, TaskState . Paused)
738
- XCTAssertNil ( task2. progress, " `task2.progress` should still be nil. " )
739
-
740
- XCTAssertEqual ( task. state, TaskState . Paused, " Parent task should also be paused. " )
741
- XCTAssertTrue ( task. progress? == 2 )
742
-
743
- task2. resume ( )
744
- XCTAssertEqual ( task2. state, TaskState . Running)
745
- XCTAssertEqual ( task. state, TaskState . Running, " Parent task should also be resumed. " )
746
-
747
- }
748
- }
749
-
750
- self . wait ( )
751
- }
752
-
753
- func testPauseResume_async_success( )
754
- {
755
- // NOTE: this is async test
756
- if !self . isAsync { return }
757
-
758
- var expect = self . expectationWithDescription ( __FUNCTION__)
759
-
760
- let task = _interruptableTask ( progressCount: 5 )
761
- weak var innerTask : _InterruptableTask ?
762
-
763
- // chain async-task with success
764
- let task2 = task. success { [ weak self] _ -> _InterruptableTask in
765
- innerTask = _interruptableTask ( progressCount: 5 )
766
- return innerTask!
767
- }
768
-
769
- task2. success { value -> Void in
770
- XCTAssertEqual ( value, " OK " )
771
- expect. fulfill ( )
772
- }
773
-
774
- // pause at time between _interruptableTask's 1st & 2nd delay (t=0.3)
775
- Async . main ( after: 0.3 ) {
776
-
777
- // NOTE: parentTask should also be paused (if not, `task` will never be fulfilled/rejected)
778
- task2. pause ( )
732
+ XCTAssertEqual ( task. state, TaskState . Running, " `task` should NOT be paused. " )
733
+ XCTAssertTrue ( task. progress? == 2 , " `task` should be halfway progressed. " )
734
+ XCTAssertNil ( task. value, " `task` should NOT be fulfilled yet. " )
779
735
780
- XCTAssertEqual ( task2. state, TaskState . Paused)
781
- XCTAssertNil ( task2. progress, " `task2.progress` should be nil because `innerTask.progress()` has not been invoked yet. " )
782
-
783
- XCTAssertEqual ( task. state, TaskState . Paused, " Parent task should also be paused. " )
784
- XCTAssertTrue ( task. progress? == 2 )
785
-
786
- // resume after 0.3sec (t=0.6)
736
+ // resume at t=0.6
787
737
Async . main ( after: 0.3 ) {
788
738
789
- XCTAssertNil ( innerTask, " `innerTask` has not been created yet. " )
790
-
791
739
XCTAssertEqual ( task2. state, TaskState . Paused)
792
740
XCTAssertNil ( task2. progress, " `task2.progress` should still be nil. " )
793
741
794
- XCTAssertEqual ( task . state , TaskState . Paused , " Parent task should also be paused ." )
795
- XCTAssertTrue ( task . progress ? == 2 )
742
+ XCTAssertNotNil ( innerTask , " `innerTask` should be created at this point ." )
743
+ XCTAssertEqual ( innerTask! . state , task2 . state , " `innerTask!.state` should be same as `task2.state`. " )
796
744
797
- task2. resume ( )
798
- XCTAssertEqual ( task2. state, TaskState . Running)
799
- XCTAssertEqual ( task. state, TaskState . Running, " Parent task should also be resumed. " )
745
+ XCTAssertEqual ( task. state, TaskState . Fulfilled, " `task` should NOT be paused, and it should be fulfilled at this point. " )
746
+ XCTAssertEqual ( task. value!, " OK " , " `task` should be fulfilled. " )
800
747
801
- }
802
- }
803
-
804
- self . wait ( )
805
- }
806
-
807
- func testPauseResume_async_failure( )
808
- {
809
- // NOTE: this is async test
810
- if !self . isAsync { return }
811
-
812
- var expect = self . expectationWithDescription ( __FUNCTION__)
813
-
814
- let task = _interruptableTask ( progressCount: 5 )
815
- weak var innerTask : _InterruptableTask ?
816
-
817
- // chain async-task with failure
818
- let task2 = task. failure { [ weak self] _ -> _InterruptableTask in
819
- innerTask = _interruptableTask ( progressCount: 5 )
820
- return innerTask!
821
- }
822
-
823
- task2. success { value -> Void in
824
- XCTAssertEqual ( value, " OK " )
825
- expect. fulfill ( )
826
- }
827
-
828
- // pause at time between _interruptableTask's 1st & 2nd delay (t=0.3)
829
- Async . main ( after: 0.3 ) {
830
-
831
- // NOTE: parentTask should also be paused (if not, `task` will never be fulfilled/rejected)
832
- task2. pause ( )
833
-
834
- XCTAssertEqual ( task2. state, TaskState . Paused)
835
- XCTAssertNil ( task2. progress, " `task2.progress` should be nil because `innerTask.progress()` has not been invoked yet. " )
836
-
837
- XCTAssertEqual ( task. state, TaskState . Paused, " Parent task should also be paused. " )
838
- XCTAssertTrue ( task. progress? == 2 )
839
-
840
- // resume after 0.3sec (t=0.6)
841
- Async . main ( after: 0.3 ) {
842
-
843
- XCTAssertEqual ( task2. state, TaskState . Paused)
844
- XCTAssertNil ( task2. progress, " `task2.progress` should still be nil. " )
748
+ task2. resume ( )
845
749
846
- XCTAssertEqual ( task. state, TaskState . Paused, " Parent task should also be paused. " )
847
- XCTAssertTrue ( task. progress? == 2 )
750
+ XCTAssertEqual ( task2. state, TaskState . Running, " `task2` should be resumed. " )
848
751
849
- task2. resume ( )
850
- XCTAssertEqual ( task2. state, TaskState . Running)
851
- XCTAssertEqual ( task. state, TaskState . Running, " Parent task should also be resumed. " )
752
+ // check tasks's states at t=0.7
753
+ Async . main ( after: 0.1 ) {
754
+ XCTAssertEqual ( task2. state, TaskState . Running)
755
+ XCTAssertEqual ( innerTask!. state, task2. state, " `innerTask!.state` should be same as `task2.state`. " )
756
+ XCTAssertEqual ( task. state, TaskState . Fulfilled)
757
+ }
852
758
853
759
}
854
760
}
@@ -1054,9 +960,9 @@ class SwiftTaskTests: _TestCase
1054
960
var maxTryCount = 3
1055
961
var actualTryCount = 0
1056
962
1057
- let retryableTask = Task < Float , String , ErrorString > { progress, fulfill, reject, configure in
963
+ let task = Task < Float , String , ErrorString > { progress, fulfill, reject, configure in
1058
964
1059
- Async . main ( after: 0.1 ) {
965
+ Async . main ( after: 0.3 ) {
1060
966
1061
967
actualTryCount++
1062
968
@@ -1070,7 +976,9 @@ class SwiftTaskTests: _TestCase
1070
976
1071
977
return
1072
978
1073
- } . try ( maxTryCount)
979
+ }
980
+
981
+ let retryableTask = task. try ( maxTryCount)
1074
982
1075
983
retryableTask. success { value -> Void in
1076
984
@@ -1079,8 +987,19 @@ class SwiftTaskTests: _TestCase
1079
987
} . failure { errorInfo -> Void in
1080
988
1081
989
XCTAssertTrue ( errorInfo. isCancelled)
1082
- expect. fulfill ( )
990
+ // expect.fulfill()
991
+
992
+ }
993
+
994
+ task. success { value -> Void in
1083
995
996
+ XCTFail ( " Should never reach here because `retryableTask` is cancelled so original-`task` should also be cancelled. " )
997
+
998
+ } . failure { errorInfo -> Void in
999
+
1000
+ XCTAssertTrue ( errorInfo. isCancelled)
1001
+ expect. fulfill ( )
1002
+
1084
1003
}
1085
1004
1086
1005
// cancel `retryableTask` at some point before all tries completes
0 commit comments