Skip to content

Commit d15ea9a

Browse files
author
Nacho Bonafonte
committed
Fix non iOS tests, add also support for watchOS
Make InstrumentationUtils.usesUndocumentedAsyncAwaitMethods a static variable so it is only calculated once
1 parent 8bdbcc7 commit d15ea9a

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

Sources/Exporters/DatadogExporter/Utils/Device.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#if os(macOS)
77
import Foundation
88
import SystemConfiguration
9-
#elseif os(iOS) || targetEnvironment(macCatalyst)
9+
#elseif os(iOS) || targetEnvironment(macCatalyst) || os(tvOS)
1010
import UIKit
1111
#elseif os(watchOS)
1212
import WatchKit
@@ -23,7 +23,8 @@ internal class Device {
2323
init(
2424
model: String,
2525
osName: String,
26-
osVersion: String) {
26+
osVersion: String)
27+
{
2728
self.model = model
2829
self.osName = osName
2930
self.osVersion = osVersion
@@ -36,6 +37,7 @@ internal class Device {
3637
osName: uiDevice.systemName,
3738
osVersion: uiDevice.systemVersion)
3839
}
40+
3941
#elseif os(macOS)
4042
convenience init(processInfo: ProcessInfo) {
4143
self.init(
@@ -64,8 +66,7 @@ internal class Device {
6466
return Device(
6567
model: device.model,
6668
osName: device.systemName,
67-
osVersion: device.systemVersion
68-
)
69+
osVersion: device.systemVersion)
6970
#endif
7071
}
7172
}

Sources/Instrumentation/URLSession/InstrumentationUtils.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*/
55

66
import Foundation
7-
#if !os(macOS)
7+
#if os(iOS) || os(tvOS)
88
import UIKit
9+
#elseif os(watchOS)
10+
import WatchKit
911
#endif
1012

1113
enum InstrumentationUtils {
@@ -52,12 +54,19 @@ enum InstrumentationUtils {
5254
}
5355
}
5456

55-
static func usesUndocumentedAsyncAwaitMethods() -> Bool {
57+
static var usesUndocumentedAsyncAwaitMethods: Bool = {
5658
#if os(macOS)
5759
let os = ProcessInfo.processInfo.operatingSystemVersion
5860
if os.majorVersion >= 13 {
5961
return true
6062
}
63+
#elseif os(watchOS)
64+
let version = WKInterfaceDevice.current().systemVersion
65+
if let versionNumber = Double(version),
66+
versionNumber >= 9.0
67+
{
68+
return true
69+
}
6170
#else
6271
let version = UIDevice.current.systemVersion
6372
if let versionNumber = Double(version),
@@ -67,5 +76,5 @@ enum InstrumentationUtils {
6776
}
6877
#endif
6978
return false
70-
}
79+
}()
7180
}

Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class URLSessionInstrumentation {
105105
injectTaskDidCompleteWithErrorIntoDelegateClass(cls: cls)
106106
injectRespondsToSelectorIntoDelegateClass(cls: cls)
107107
// For future use
108-
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods() {
108+
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods {
109109
injectTaskDidFinishCollectingMetricsIntoDelegateClass(cls: cls)
110110
}
111111

@@ -577,12 +577,12 @@ public class URLSessionInstrumentation {
577577
requestMap[taskId]?.setRequest(request)
578578
}
579579

580-
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods() {
580+
if InstrumentationUtils.usesUndocumentedAsyncAwaitMethods {
581581
let instrumentedRequest = URLSessionLogger.processAndLogRequest(request, sessionTaskId: taskId, instrumentation: self, shouldInjectHeaders: true)
582582
task.setValue(instrumentedRequest, forKey: "currentRequest")
583583
self.setIdKey(value: taskId, for: task)
584584

585-
if #available(iOS 16.0, *) {
585+
if #available(OSX 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
586586
let basePriority = Task.basePriority
587587
//If not inside a Task basePriority is nil
588588
if basePriority != nil && task.delegate == nil {
@@ -618,6 +618,5 @@ public class URLSessionInstrumentation {
618618

619619
class FakeDelegate: NSObject, URLSessionTaskDelegate {
620620
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
621-
let a = 0
622621
}
623622
}

Tests/InstrumentationTests/URLSessionTests/URLSessionInstrumentationTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class URLSessionInstrumentationTests: XCTestCase {
466466
XCTAssertNotNil(URLSessionInstrumentationTests.requestCopy?.allHTTPHeaderFields?[W3CTraceContextPropagator.traceparent])
467467
}
468468

469-
@available(macOS 12, iOS 15.0, tvOS 15.0, *)
469+
@available(macOS 12, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
470470
public func testDownloadTaskWithUrlBlockAsync() async throws {
471471
let url = URL(string: "http://localhost:33333/success")!
472472

@@ -476,7 +476,7 @@ class URLSessionInstrumentationTests: XCTestCase {
476476
XCTAssertNotNil(URLSessionInstrumentationTests.requestCopy?.allHTTPHeaderFields?[W3CTraceContextPropagator.traceparent])
477477
}
478478

479-
@available(macOS 12, iOS 15.0, tvOS 15.0, *)
479+
@available(macOS 12, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
480480
public func testDownloadTaskWithRequestBlockAsync() async throws {
481481
let url = URL(string: "http://localhost:33333/success")!
482482
let request = URLRequest(url: url)
@@ -520,7 +520,7 @@ class URLSessionInstrumentationTests: XCTestCase {
520520
XCTAssertNotNil(URLSessionInstrumentationTests.requestCopy?.allHTTPHeaderFields?[W3CTraceContextPropagator.traceparent])
521521
}
522522

523-
@available(macOS 12, iOS 15.0, tvOS 15.0, *)
523+
@available(macOS 12, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
524524
public func testDownloadTaskWithUrlDelegateAsync() async throws {
525525
let url = URL(string: "http://localhost:33333/success")!
526526

@@ -530,7 +530,7 @@ class URLSessionInstrumentationTests: XCTestCase {
530530
XCTAssertNotNil(URLSessionInstrumentationTests.requestCopy?.allHTTPHeaderFields?[W3CTraceContextPropagator.traceparent])
531531
}
532532

533-
@available(macOS 12, iOS 15.0, tvOS 15.0, *)
533+
@available(macOS 12, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
534534
public func testDownloadTaskWithRequestDelegateAsync() async throws {
535535
let url = URL(string: "http://localhost:33333/success")!
536536
let request = URLRequest(url: url)

0 commit comments

Comments
 (0)