Skip to content

Commit 7464aca

Browse files
committed
[Test] Improve testPauseResume_innerTask & testTry_cancel to reproduce chained-task-pause/resume bug & try() bug.
1 parent 090a410 commit 7464aca

File tree

1 file changed

+52
-133
lines changed

1 file changed

+52
-133
lines changed

SwiftTaskTests/SwiftTaskTests.swift

Lines changed: 52 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class SwiftTaskTests: _TestCase
137137
self.wait()
138138
}
139139

140-
func testFulfill_successTaskFulfill()
140+
func testFulfill_success_innerTask_fulfill()
141141
{
142142
var expect = self.expectationWithDescription(__FUNCTION__)
143143

@@ -169,7 +169,7 @@ class SwiftTaskTests: _TestCase
169169
self.wait()
170170
}
171171

172-
func testFulfill_successTaskReject()
172+
func testFulfill_success_innerTask_reject()
173173
{
174174
var expect = self.expectationWithDescription(__FUNCTION__)
175175

@@ -317,7 +317,7 @@ class SwiftTaskTests: _TestCase
317317
self.wait()
318318
}
319319

320-
func testReject_failureTaskFulfill()
320+
func testReject_failure_innerTask_fulfill()
321321
{
322322
var expect = self.expectationWithDescription(__FUNCTION__)
323323

@@ -350,7 +350,7 @@ class SwiftTaskTests: _TestCase
350350
self.wait()
351351
}
352352

353-
func testReject_failureTaskReject()
353+
func testReject_failure_innerTask_reject()
354354
{
355355
var expect = self.expectationWithDescription(__FUNCTION__)
356356

@@ -473,7 +473,7 @@ class SwiftTaskTests: _TestCase
473473
self.wait()
474474
}
475475

476-
func testProgress_async_then()
476+
func testProgress_innerTask_then()
477477
{
478478
// NOTE: this is async test
479479
if !self.isAsync { return }
@@ -502,7 +502,7 @@ class SwiftTaskTests: _TestCase
502502
self.wait()
503503
}
504504

505-
func testProgress_async_success()
505+
func testProgress_innerTask_success()
506506
{
507507
// NOTE: this is async test
508508
if !self.isAsync { return }
@@ -531,7 +531,7 @@ class SwiftTaskTests: _TestCase
531531
self.wait()
532532
}
533533

534-
func testProgress_async_failure()
534+
func testProgress_innerTask_failure()
535535
{
536536
// NOTE: this is async test
537537
if !self.isAsync { return }
@@ -606,7 +606,7 @@ class SwiftTaskTests: _TestCase
606606
self.wait()
607607
}
608608

609-
func testCancel_thenTask()
609+
func testCancel_then_innerTask()
610610
{
611611
var expect = self.expectationWithDescription(__FUNCTION__)
612612

@@ -673,30 +673,31 @@ class SwiftTaskTests: _TestCase
673673

674674
}
675675

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)
677677
Async.main(after: 0.3) {
678678

679679
task.pause()
680680

681681
XCTAssertEqual(task.state, TaskState.Paused)
682-
XCTAssertTrue(task.progress? == 2)
682+
XCTAssertTrue(task.progress? == 2, "`task` should be progressed halfway.")
683683

684-
// resume after 0.3sec (t=0.6)
684+
// resume at t=0.6
685685
Async.main(after: 0.3) {
686686

687687
XCTAssertEqual(task.state, TaskState.Paused)
688-
XCTAssertTrue(task.progress? == 2)
688+
XCTAssertTrue(task.progress? == 2, "`task` should pause progressing.")
689689

690690
task.resume()
691-
XCTAssertEqual(task.state, TaskState.Running)
691+
692+
XCTAssertEqual(task.state, TaskState.Running, "`task` should start running again.")
692693

693694
}
694695
}
695696

696697
self.wait()
697698
}
698699

699-
func testPauseResume_async_then()
700+
func testPauseResume_innerTask()
700701
{
701702
// NOTE: this is async test
702703
if !self.isAsync { return }
@@ -706,7 +707,7 @@ class SwiftTaskTests: _TestCase
706707
let task = _interruptableTask(progressCount: 5)
707708
weak var innerTask: _InterruptableTask?
708709

709-
// chain async-task with then
710+
// chain async-task with `then`
710711
let task2 = task.then { [weak self] _ -> _InterruptableTask in
711712
innerTask = _interruptableTask(progressCount: 5)
712713
return innerTask!
@@ -717,138 +718,43 @@ class SwiftTaskTests: _TestCase
717718
expect.fulfill()
718719
}
719720

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)
721722
Async.main(after: 0.3) {
722723

723-
// NOTE: parentTask should also be paused (if not, `task` will never be fulfilled/rejected)
724+
// NOTE: task2 will be paused,
724725
task2.pause()
725726

726-
XCTAssertNil(innerTask, "`innerTask` has not been created yet.")
727-
728727
XCTAssertEqual(task2.state, TaskState.Paused)
729728
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
730729

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.")
733731

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.")
779735

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
787737
Async.main(after: 0.3) {
788738

789-
XCTAssertNil(innerTask, "`innerTask` has not been created yet.")
790-
791739
XCTAssertEqual(task2.state, TaskState.Paused)
792740
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
793741

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`.")
796744

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.")
800747

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()
845749

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.")
848751

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+
}
852758

853759
}
854760
}
@@ -1054,9 +960,9 @@ class SwiftTaskTests: _TestCase
1054960
var maxTryCount = 3
1055961
var actualTryCount = 0
1056962

1057-
let retryableTask = Task<Float, String, ErrorString> { progress, fulfill, reject, configure in
963+
let task = Task<Float, String, ErrorString> { progress, fulfill, reject, configure in
1058964

1059-
Async.main(after: 0.1) {
965+
Async.main(after: 0.3) {
1060966

1061967
actualTryCount++
1062968

@@ -1070,7 +976,9 @@ class SwiftTaskTests: _TestCase
1070976

1071977
return
1072978

1073-
}.try(maxTryCount)
979+
}
980+
981+
let retryableTask = task.try(maxTryCount)
1074982

1075983
retryableTask.success { value -> Void in
1076984

@@ -1079,8 +987,19 @@ class SwiftTaskTests: _TestCase
1079987
}.failure { errorInfo -> Void in
1080988

1081989
XCTAssertTrue(errorInfo.isCancelled)
1082-
expect.fulfill()
990+
// expect.fulfill()
991+
992+
}
993+
994+
task.success { value -> Void in
1083995

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+
10841003
}
10851004

10861005
// cancel `retryableTask` at some point before all tries completes

0 commit comments

Comments
 (0)