@@ -53,6 +53,8 @@ class TestURLSession : LoopbackServerTest {
53
53
( " test_checkErrorTypeAfterInvalidateAndCancel " , test_checkErrorTypeAfterInvalidateAndCancel) ,
54
54
( " test_taskCountAfterInvalidateAndCancel " , test_taskCountAfterInvalidateAndCancel) ,
55
55
( " test_sessionDelegateAfterInvalidateAndCancel " , test_sessionDelegateAfterInvalidateAndCancel) ,
56
+ ( " test_getAllTasks " , test_getAllTasks) ,
57
+ ( " test_getTasksWithCompletion " , test_getTasksWithCompletion) ,
56
58
]
57
59
}
58
60
@@ -819,32 +821,31 @@ class TestURLSession : LoopbackServerTest {
819
821
session. invalidateAndCancel ( )
820
822
waitForExpectations ( timeout: 5 )
821
823
}
822
-
824
+
823
825
func test_taskCountAfterInvalidateAndCancel( ) {
824
826
let expect = expectation ( description: " Check task count after invalidateAndCancel " )
825
-
827
+
826
828
let session = URLSession ( configuration: . default)
827
829
let task1 = session. dataTask ( with: URL ( string: " https://www.apple.com " ) !)
828
830
let task2 = session. dataTask ( with: URL ( string: " https://developer.apple.com " ) !)
829
831
let task3 = session. dataTask ( with: URL ( string: " https://developer.apple.com/swift " ) !)
830
-
832
+
831
833
task1. resume ( )
832
834
task2. resume ( )
833
835
session. invalidateAndCancel ( )
834
836
Thread . sleep ( forTimeInterval: 1 )
835
-
837
+
836
838
session. getAllTasks { tasksBeforeResume in
837
839
XCTAssertEqual ( tasksBeforeResume. count, 0 )
838
-
840
+
839
841
// Resume a task after invalidating a session shouldn't change the task's status
840
842
task3. resume ( )
841
-
843
+
842
844
session. getAllTasks { tasksAfterResume in
843
845
XCTAssertEqual ( tasksAfterResume. count, 0 )
844
846
expect. fulfill ( )
845
847
}
846
848
}
847
-
848
849
waitForExpectations ( timeout: 5 )
849
850
}
850
851
@@ -856,6 +857,91 @@ class TestURLSession : LoopbackServerTest {
856
857
XCTAssertNil ( session. delegate)
857
858
}
858
859
860
+ func test_getAllTasks( ) {
861
+ let expect = expectation ( description: " Tasks URLSession.getAllTasks " )
862
+
863
+ let session = URLSession ( configuration: . default)
864
+ let dataTask1 = session. dataTask ( with: URL ( string: " https://www.apple.com " ) !)
865
+ let dataTask2 = session. dataTask ( with: URL ( string: " https://developer.apple.com " ) !)
866
+ let dataTask3 = session. dataTask ( with: URL ( string: " https://developer.apple.com/swift " ) !)
867
+
868
+ session. getAllTasks { ( tasksBeforeResume) in
869
+ XCTAssertEqual ( tasksBeforeResume. count, 0 )
870
+
871
+ dataTask1. cancel ( )
872
+
873
+ dataTask2. resume ( )
874
+ dataTask2. suspend ( )
875
+ // dataTask3 is suspended even before it was resumed, so the next call to `getAllTasks` should not include this tasks
876
+ dataTask3. suspend ( )
877
+ session. getAllTasks { ( tasksAfterCancel) in
878
+ // tasksAfterCancel should only contain dataTask2
879
+ XCTAssertEqual ( tasksAfterCancel. count, 1 )
880
+
881
+ // A task will in be in suspended state when it was created.
882
+ // Given that, dataTask3 was suspended once again earlier above, so it should receive `resume()` twice in order to be executed
883
+ // Calling `getAllTasks` next time should not include dataTask3
884
+ dataTask3. resume ( )
885
+
886
+ session. getAllTasks { ( tasksAfterFirstResume) in
887
+ // tasksAfterFirstResume should only contain dataTask2
888
+ XCTAssertEqual ( tasksAfterFirstResume. count, 1 )
889
+
890
+ // Now dataTask3 received `resume()` twice, this time `getAllTasks` should include
891
+ dataTask3. resume ( )
892
+ session. getAllTasks { ( tasksAfterSecondResume) in
893
+ // tasksAfterSecondResume should contain dataTask2 and dataTask2 this time
894
+ XCTAssertEqual ( tasksAfterSecondResume. count, 2 )
895
+ expect. fulfill ( )
896
+ }
897
+ }
898
+ }
899
+ }
900
+
901
+ waitForExpectations ( timeout: 20 )
902
+ }
903
+
904
+ func test_getTasksWithCompletion( ) {
905
+ let expect = expectation ( description: " Test URLSession.getTasksWithCompletion " )
906
+
907
+ let session = URLSession ( configuration: . default)
908
+ let dataTask1 = session. dataTask ( with: URL ( string: " https://www.apple.com " ) !)
909
+ let dataTask2 = session. dataTask ( with: URL ( string: " https://developer.apple.com " ) !)
910
+ let dataTask3 = session. dataTask ( with: URL ( string: " https://developer.apple.com/swift " ) !)
911
+
912
+ let uploadTask1 = session. uploadTask ( with: URLRequest ( url: URL ( string: " https://developer.apple.com " ) !) , from: Data ( ) )
913
+ let uploadTask2 = session. uploadTask ( with: URLRequest ( url: URL ( string: " https://developer.apple.com/swift " ) !) , from: Data ( ) )
914
+
915
+ let downloadTask1 = session. downloadTask ( with: URL ( string: " https://developer.apple.com/assets/elements/icons/brandmark/apple-developer-brandmark.svg " ) !)
916
+
917
+ session. getTasksWithCompletionHandler { ( dataTasksBeforeCancel, uploadTasksBeforeCancel, downloadTasksBeforeCancel) in
918
+ XCTAssertEqual ( dataTasksBeforeCancel. count, 0 )
919
+ XCTAssertEqual ( uploadTasksBeforeCancel. count, 0 )
920
+ XCTAssertEqual ( downloadTasksBeforeCancel. count, 0 )
921
+
922
+ dataTask1. cancel ( )
923
+ dataTask2. resume ( )
924
+ // dataTask3 is resumed and suspended, so this task should be a part of `getTasksWithCompletionHandler` response
925
+ dataTask3. resume ( )
926
+ dataTask3. suspend ( )
927
+
928
+ // uploadTask1 suspended even before it was resumed, so this task shouldn't be a part of `getTasksWithCompletionHandler` response
929
+ uploadTask1. suspend ( )
930
+ uploadTask2. resume ( )
931
+
932
+ downloadTask1. cancel ( )
933
+
934
+ session. getTasksWithCompletionHandler { ( dataTasksAfterCancel, uploadTasksAfterCancel, downloadTasksAfterCancel) in
935
+ XCTAssertEqual ( dataTasksAfterCancel. count, 2 )
936
+ XCTAssertEqual ( uploadTasksAfterCancel. count, 1 )
937
+ XCTAssertEqual ( downloadTasksAfterCancel. count, 0 )
938
+ expect. fulfill ( )
939
+ }
940
+ }
941
+
942
+ waitForExpectations ( timeout: 20 )
943
+ }
944
+
859
945
}
860
946
861
947
class SharedDelegate : NSObject {
0 commit comments