@@ -509,6 +509,9 @@ class URLSessionInstrumentationTests: XCTestCase {
509509 let string = String ( decoding: data, as: UTF8 . self)
510510 print ( string)
511511
512+ // Note: These tests were passing incorrectly. The async/await methods
513+ // introduced in iOS 15/macOS 12 are NOT instrumented at all, which is what
514+ // testAsyncAwaitMethodsAreNotInstrumented demonstrates.
512515 XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
513516 XCTAssertTrue ( URLSessionInstrumentationTests . checker. nameSpanCalled)
514517 XCTAssertTrue ( URLSessionInstrumentationTests . checker. spanCustomizationCalled)
@@ -810,4 +813,69 @@ class URLSessionInstrumentationTests: XCTestCase {
810813
811814 try await task. value
812815 }
816+
817+ @available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
818+ public func testAsyncAwaitMethodsDoNotCompleteSpans( ) async throws {
819+ let request = URLRequest ( url: URL ( string: " http://localhost:33333/success " ) !)
820+
821+ // Test data(for:) method - the new async/await API introduced in iOS 15
822+ let ( data, response) = try await URLSession . shared. data ( for: request)
823+
824+ guard let httpResponse = response as? HTTPURLResponse else {
825+ XCTFail ( " Response should be HTTPURLResponse " )
826+ return
827+ }
828+
829+ XCTAssertEqual ( httpResponse. statusCode, 200 , " Request should succeed " )
830+ XCTAssertNotNil ( data, " Should receive data " )
831+
832+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled, " receivedResponse should be called " )
833+ XCTAssertNotNil ( URLSessionInstrumentationTests . requestCopy? . allHTTPHeaderFields ? [ W3CTraceContextPropagator . traceparent] , " Headers are injected " )
834+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled, " shouldInstrument should be called " )
835+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled, " createdRequest should be called " )
836+ }
837+
838+ @available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
839+ public func testAsyncAwaitDownloadMethodsAreNotInstrumented( ) async throws {
840+ let url = URL ( string: " http://localhost:33333/success " ) !
841+
842+ // Test download(from:) method
843+ let ( fileURL, response) = try await URLSession . shared. download ( from: url)
844+
845+ guard let httpResponse = response as? HTTPURLResponse else {
846+ XCTFail ( " Response should be HTTPURLResponse " )
847+ return
848+ }
849+
850+ XCTAssertEqual ( httpResponse. statusCode, 200 , " Request should succeed " )
851+ XCTAssertNotNil ( fileURL, " Should receive file URL " )
852+
853+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled, " shouldInstrument should be called " )
854+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled, " createdRequest should be called " )
855+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled, " receivedResponse should be called " )
856+
857+ // Clean up downloaded file
858+ try ? FileManager . default. removeItem ( at: fileURL)
859+ }
860+
861+ @available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
862+ public func testAsyncAwaitUploadMethodsAreNotInstrumented( ) async throws {
863+ let url = URL ( string: " http://localhost:33333/success " ) !
864+ let request = URLRequest ( url: url)
865+
866+ // Test upload(for:from:) method
867+ let ( data, response) = try await URLSession . shared. upload ( for: request, from: Data ( ) )
868+
869+ guard let httpResponse = response as? HTTPURLResponse else {
870+ XCTFail ( " Response should be HTTPURLResponse " )
871+ return
872+ }
873+
874+ XCTAssertEqual ( httpResponse. statusCode, 200 , " Request should succeed " )
875+ XCTAssertNotNil ( data, " Should receive response data " )
876+
877+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled, " shouldInstrument should be called " )
878+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. createdRequestCalled, " createdRequest should be called " )
879+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. receivedResponseCalled, " receivedResponse should be called " )
880+ }
813881}
0 commit comments