This test doesn't pass, and i'm wondering why. It looks like the url session isn't releasing its delegate after invalidateAndCancel() is called, contrarely to what the documentation says. I've tried adding some dispatch.async in case this would be due to some event loop side effects to no success.
import XCTest
final class URLSessionTests: XCTestCase {
@objc
class DelegateObject: NSObject, URLSessionDelegate {
var onDidBecomeInvalid: (() -> Void)?
lazy var urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
onDidBecomeInvalid?()
}
}
func testURLSessionRelease() throws {
weak var del: DelegateObject?
let didBecomeInvalidExp = expectation(description: "did become invalid")
func instanciateDelegateObjectAndInvalidateSession() {
let t = DelegateObject()
t.onDidBecomeInvalid = {
didBecomeInvalidExp.fulfill()
}
del = t
t.urlSession.invalidateAndCancel()
}
instanciateDelegateObjectAndInvalidateSession()
wait(for: [didBecomeInvalidExp], timeout: 3)
XCTAssertNil(del)
}
}