Skip to content

Commit

Permalink
Updates URLSessionInstrumentation to check task.state before setting …
Browse files Browse the repository at this point in the history
…FakeDelegaet (#539)
  • Loading branch information
atreat authored May 7, 2024
1 parent 62772bd commit 60eac93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,15 @@ public class URLSessionInstrumentation {

if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
guard Task.basePriority != nil else {
// If not inside a Task basePriority is nil
return
}
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: taskId, instrumentation: self, shouldInjectHeaders: true)
task.setValue(instrumentedRequest, forKey: "currentRequest")
self.setIdKey(value: taskId, for: task)

// If not inside a Task basePriority is nil
if task.delegate == nil {

if task.delegate == nil && task.state != .running {
task.delegate = FakeDelegate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ class URLSessionInstrumentationTests: XCTestCase {
XCTAssertNotNil(URLSessionInstrumentationTests.requestCopy?.allHTTPHeaderFields?[W3CTraceContextPropagator.traceparent])
}

public func testDataTaskWithUrlBlock_doesNotCrashWhenResumed_whenDataTaskIsRunning() {
let url = URL(string: "http://localhost:33333/success")!
let dataTask = URLSession.shared.dataTask(with: url) { _, _, _ in
URLSessionInstrumentationTests.semaphore.signal()
}

dataTask.resume()
Task.detached {
dataTask.resume()
}

URLSessionInstrumentationTests.semaphore.wait()
XCTAssertTrue(URLSessionInstrumentationTests.checker.createdRequestCalled)
XCTAssertNotNil(URLSessionInstrumentationTests.requestCopy?.allHTTPHeaderFields?[W3CTraceContextPropagator.traceparent])
}

public func testDownloadTaskWithUrlBlock() {
let url = URL(string: "http://localhost:33333/success")!

Expand Down

0 comments on commit 60eac93

Please sign in to comment.