Skip to content

Instantly share code, notes, and snippets.

@bgerstle
Last active February 23, 2016 20:11
Show Gist options
  • Save bgerstle/98b17e9fb39ad45eb105 to your computer and use it in GitHub Desktop.
Save bgerstle/98b17e9fb39ad45eb105 to your computer and use it in GitHub Desktop.
Test for reproducing a deadlock between "cancel" and "connectionDidFinishLoading"
func testSDWebImageDownloaderOperationThreadSafety() {
NSURLProtocol.registerClass(WMFHTTPHangingProtocol)
defer {
NSURLProtocol.unregisterClass(WMFHTTPHangingProtocol)
}
[0...1000].forEach { _ in
let downloadOperation = SDWebImageDownloaderOperation(
request: NSURLRequest(URL: NSURL(string: "https://test.org/foo")!),
options: [],
progress: nil,
completed: nil,
cancelled: nil
)
let testThread = NSThread(target: downloadOperation, selector: Selector("start"), object: nil)
testThread.start()
expect(downloadOperation.executing).toEventually(beTrue())
expect(downloadOperation.finished).toEventually(beFalse())
expect(downloadOperation.valueForKey("thread") as? NSThread).toEventually(beIdenticalTo(testThread))
let operations: [() -> Void] = [
downloadOperation.cancel, {
downloadOperation.performSelector(
Selector("connectionDidFinishLoading:"),
onThread: testThread,
withObject: nil,
waitUntilDone: false)
}]
dispatch_apply(2, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
operations[$0]()
}
expect(downloadOperation.executing).toEventually(beFalse())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment