Skip to content

Commit 7f4090f

Browse files
author
Pushkar Kulkarni
committed
Fix for test_finishTaskAndInvalidate failure
1 parent 670323e commit 7f4090f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Foundation/NSURLProtocol.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,24 @@ internal class _ProtocolClient : NSObject, URLProtocolClient {
156156
guard let session = task.session as? URLSession else { fatalError() }
157157
switch session.behaviour(for: task) {
158158
case .taskDelegate(let delegate):
159-
guard let s = session as? URLSession else { fatalError() }
160-
s.delegateQueue.addOperation {
161-
delegate.urlSession(s, task: task, didCompleteWithError: nil)
159+
session.delegateQueue.addOperation {
160+
delegate.urlSession(session, task: task, didCompleteWithError: nil)
162161
task.state = .completed
162+
session.taskRegistry.remove(task)
163163
}
164164
case .noDelegate:
165165
task.state = .completed
166+
session.taskRegistry.remove(task)
166167
case .dataCompletionHandler(let completion):
167168
let data = Data()
168169
guard let client = `protocol`.client else { fatalError() }
169170
client.urlProtocol(`protocol`, didLoad: data)
170171
return
171172
case .downloadCompletionHandler(let completion):
172-
guard let s = session as? URLSession else { fatalError() }
173-
s.delegateQueue.addOperation {
173+
session.delegateQueue.addOperation {
174174
completion(task.currentRequest?.url, task.response, nil)
175175
task.state = .completed
176+
session.taskRegistry.remove(task)
176177
}
177178
}
178179
}
@@ -194,6 +195,7 @@ internal class _ProtocolClient : NSObject, URLProtocolClient {
194195
s.delegateQueue.addOperation {
195196
completion(data, task.response, nil)
196197
task.state = .completed
198+
s.taskRegistry.remove(task)
197199
}
198200
default: return
199201
}
@@ -204,24 +206,25 @@ internal class _ProtocolClient : NSObject, URLProtocolClient {
204206
guard let session = task.session as? URLSession else { fatalError() }
205207
switch session.behaviour(for: task) {
206208
case .taskDelegate(let delegate):
207-
guard let s = session as? URLSession else { fatalError() }
208-
s.delegateQueue.addOperation {
209-
delegate.urlSession(s, task: task, didCompleteWithError: error as Error)
209+
session.delegateQueue.addOperation {
210+
delegate.urlSession(session, task: task, didCompleteWithError: error as Error)
210211
task.state = .completed
212+
session.taskRegistry.remove(task)
211213
}
212214
case .noDelegate:
213215
task.state = .completed
216+
session.taskRegistry.remove(task)
214217
case .dataCompletionHandler(let completion):
215-
guard let s = session as? URLSession else { fatalError() }
216-
s.delegateQueue.addOperation {
218+
session.delegateQueue.addOperation {
217219
completion(nil, nil, error)
218220
task.state = .completed
221+
session.taskRegistry.remove(task)
219222
}
220223
case .downloadCompletionHandler(let completion):
221-
guard let s = session as? URLSession else { fatalError() }
222-
s.delegateQueue.addOperation {
224+
session.delegateQueue.addOperation {
223225
completion(nil, nil, error)
224226
task.state = .completed
227+
session.taskRegistry.remove(task)
225228
}
226229
}
227230
}

Foundation/NSURLSession/http/HTTPURLProtocol.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ internal class _HTTPURLProtocol: URLProtocol {
5555
self.internalState = .transferFailed
5656
guard let error = self.task?.error else { fatalError() }
5757
completeTask(withError: error)
58-
return
5958
}
6059
}
6160

0 commit comments

Comments
 (0)