@@ -61,7 +61,7 @@ class ImageDownloaderTests: XCTestCase {
61
61
XCTAssertNil ( errorReturned)
62
62
}
63
63
64
- func testDownloadingSameImageWhileInProgressAddsCallbacksWithoutAddingAnotherRequest ( ) {
64
+ func testDownloadingImageWhileAlreadyInProgressAddsCallbacksWithoutAddingAnotherRequest ( ) {
65
65
guard let downloader = downloader else {
66
66
XCTFail ( )
67
67
return
@@ -70,6 +70,9 @@ class ImageDownloaderTests: XCTestCase {
70
70
var secondCallbackCalled = false
71
71
var operation : ImageDownload
72
72
73
+ // URL loading is delayed in order to simulate conditions under which multiple requests for the same asset would be made
74
+ ImageLoadingURLProtocolSpy . delayImageLoading ( )
75
+
73
76
downloader. downloadImage ( with: imageURL) { ( image, data, error) in
74
77
firstCallbackCalled = true
75
78
}
@@ -79,14 +82,17 @@ class ImageDownloaderTests: XCTestCase {
79
82
secondCallbackCalled = true
80
83
}
81
84
85
+ ImageLoadingURLProtocolSpy . resumeImageLoading ( )
86
+
82
87
XCTAssertTrue ( operation === downloader. activeOperation ( with: imageURL) !,
83
88
" Expected \( String ( describing: operation) ) to be identical to \( String ( describing: downloader. activeOperation ( with: imageURL) ) ) " )
84
89
85
90
var spinCount = 0
86
- runUntil ( condition: {
91
+
92
+ runUntil ( {
87
93
spinCount += 1
88
94
return operation. isFinished
89
- } , pollingInterval : 0.1 , until : XCTestCase . NavigationTests . timeout )
95
+ } )
90
96
91
97
print ( " Succeeded after evaluating condition \( spinCount) times. " )
92
98
@@ -107,10 +113,10 @@ class ImageDownloaderTests: XCTestCase {
107
113
}
108
114
var operation = downloader. activeOperation ( with: imageURL) !
109
115
110
- runUntil ( condition : {
116
+ runUntil ( {
111
117
spinCount += 1
112
118
return operation. isFinished
113
- } , pollingInterval : 0.1 , until : XCTestCase . NavigationTests . timeout )
119
+ } )
114
120
115
121
print ( " Succeeded after evaluating first condition \( spinCount) times. " )
116
122
XCTAssertTrue ( callbackCalled)
@@ -123,24 +129,28 @@ class ImageDownloaderTests: XCTestCase {
123
129
}
124
130
operation = downloader. activeOperation ( with: imageURL) !
125
131
126
- runUntil ( condition : {
132
+ runUntil ( {
127
133
spinCount += 1
128
134
return operation. isFinished
129
- } , pollingInterval : 0.1 , until : XCTestCase . NavigationTests . timeout )
135
+ } )
130
136
131
137
print ( " Succeeded after evaluating second condition \( spinCount) times. " )
132
138
XCTAssertTrue ( callbackCalled)
133
139
}
134
140
135
- private func runUntil( condition: ( ) -> Bool , pollingInterval: TimeInterval , until timeout: DispatchTime ) {
141
+ private func runUntil( _ condition: ( ) -> Bool , testCase: String = #function) {
142
+ runUntil ( condition: condition, testCase: testCase, pollingInterval: NavigationTests . pollingInterval, until: NavigationTests . timeout)
143
+ }
144
+
145
+ private func runUntil( condition: ( ) -> Bool , testCase: String , pollingInterval: TimeInterval , until timeout: DispatchTime ) {
136
146
guard ( timeout >= DispatchTime . now ( ) ) else {
137
- XCTFail ( " Timeout occurred on \( #function ) " )
147
+ XCTFail ( " Timeout occurred in \( testCase ) " )
138
148
return
139
149
}
140
150
141
151
if condition ( ) == false {
142
152
RunLoop . current. run ( until: Date ( timeIntervalSinceNow: pollingInterval) )
143
- runUntil ( condition: condition, pollingInterval: pollingInterval, until: timeout)
153
+ runUntil ( condition: condition, testCase : testCase , pollingInterval: pollingInterval, until: timeout)
144
154
}
145
155
}
146
156
}
0 commit comments