Skip to content

Commit 35faf1f

Browse files
committed
[Test] Fix chained-task-progress test, revealing new bug.
1 parent 9f5c994 commit 35faf1f

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

SwiftTaskTests/AlamofireTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class AlamofireTests: _TestCase
6060
return
6161
}
6262

63+
if response?.statusCode >= 300 {
64+
reject(NSError())
65+
}
66+
6367
fulfill("OK")
6468

6569
}

SwiftTaskTests/SwiftTaskTests.swift

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class SwiftTaskTests: _TestCase
495495
return
496496
}.success { value -> Void in
497497
XCTAssertEqual(value, "OK")
498-
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
498+
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
499499
expect.fulfill()
500500
}
501501

@@ -522,10 +522,10 @@ class SwiftTaskTests: _TestCase
522522
progressCount++
523523
println(progressValues)
524524
return
525-
}.success { value -> Void in
526-
XCTAssertEqual(value, "OK")
527-
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
528-
expect.fulfill()
525+
}.success { value -> Void in
526+
XCTAssertEqual(value, "OK")
527+
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
528+
expect.fulfill()
529529
}
530530

531531
self.wait()
@@ -553,7 +553,7 @@ class SwiftTaskTests: _TestCase
553553
return
554554
}.success { value -> Void in
555555
XCTAssertEqual(value, "OK")
556-
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
556+
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
557557
expect.fulfill()
558558
}
559559

@@ -768,10 +768,12 @@ class SwiftTaskTests: _TestCase
768768
var expect = self.expectationWithDescription(__FUNCTION__)
769769

770770
let task = self._interruptableTask()
771+
weak var innerTask: _InterruptableTask?
771772

772773
// chain async-task with then
773774
let task2 = task.then { [weak self] _ -> _InterruptableTask in
774-
return self!._interruptableTask()
775+
innerTask = self!._interruptableTask()
776+
return innerTask!
775777
}
776778

777779
task2.success { value -> Void in
@@ -785,8 +787,10 @@ class SwiftTaskTests: _TestCase
785787
// NOTE: parentTask should also be paused (if not, `task` will never be fulfilled/rejected)
786788
task2.pause()
787789

790+
XCTAssertNil(innerTask, "`innerTask` has not been created yet.")
791+
788792
XCTAssertEqual(task2.state, TaskState.Paused)
789-
XCTAssertTrue(task2.progress? == 0.5)
793+
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
790794

791795
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
792796
XCTAssertTrue(task.progress? == 0.5)
@@ -795,7 +799,7 @@ class SwiftTaskTests: _TestCase
795799
Async.main(after: 0.3) {
796800

797801
XCTAssertEqual(task2.state, TaskState.Paused)
798-
XCTAssertTrue(task2.progress? == 0.5)
802+
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
799803

800804
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
801805
XCTAssertTrue(task.progress? == 0.5)
@@ -818,10 +822,12 @@ class SwiftTaskTests: _TestCase
818822
var expect = self.expectationWithDescription(__FUNCTION__)
819823

820824
let task = self._interruptableTask()
825+
weak var innerTask: _InterruptableTask?
821826

822827
// chain async-task with success
823828
let task2 = task.success { [weak self] _ -> _InterruptableTask in
824-
return self!._interruptableTask()
829+
innerTask = self!._interruptableTask()
830+
return innerTask!
825831
}
826832

827833
task2.success { value -> Void in
@@ -836,16 +842,18 @@ class SwiftTaskTests: _TestCase
836842
task2.pause()
837843

838844
XCTAssertEqual(task2.state, TaskState.Paused)
839-
XCTAssertTrue(task2.progress? == 0.5)
845+
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
840846

841847
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
842848
XCTAssertTrue(task.progress? == 0.5)
843849

844850
// resume after 0.3sec (t=0.6)
845851
Async.main(after: 0.3) {
846852

853+
XCTAssertNil(innerTask, "`innerTask` has not been created yet.")
854+
847855
XCTAssertEqual(task2.state, TaskState.Paused)
848-
XCTAssertTrue(task2.progress? == 0.5)
856+
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
849857

850858
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
851859
XCTAssertTrue(task.progress? == 0.5)
@@ -868,10 +876,12 @@ class SwiftTaskTests: _TestCase
868876
var expect = self.expectationWithDescription(__FUNCTION__)
869877

870878
let task = self._interruptableTask()
879+
weak var innerTask: _InterruptableTask?
871880

872881
// chain async-task with failure
873882
let task2 = task.failure { [weak self] _ -> _InterruptableTask in
874-
return self!._interruptableTask()
883+
innerTask = self!._interruptableTask()
884+
return innerTask!
875885
}
876886

877887
task2.success { value -> Void in
@@ -886,7 +896,7 @@ class SwiftTaskTests: _TestCase
886896
task2.pause()
887897

888898
XCTAssertEqual(task2.state, TaskState.Paused)
889-
XCTAssertTrue(task2.progress? == 0.5)
899+
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
890900

891901
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
892902
XCTAssertTrue(task.progress? == 0.5)
@@ -895,7 +905,7 @@ class SwiftTaskTests: _TestCase
895905
Async.main(after: 0.3) {
896906

897907
XCTAssertEqual(task2.state, TaskState.Paused)
898-
XCTAssertTrue(task2.progress? == 0.5)
908+
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
899909

900910
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
901911
XCTAssertTrue(task.progress? == 0.5)

0 commit comments

Comments
 (0)