From 9da414b18af6675f5ba32c933b33f7d0488e94a1 Mon Sep 17 00:00:00 2001 From: Maciek Grzybowski Date: Tue, 9 Jun 2020 18:01:49 +0200 Subject: [PATCH] RUMM-494 Fix unit tests and integration tests on iOS 11 and iOS 12 --- .../Integration/LoggingIntegrationTests.swift | 31 ++++---- .../NetworkConnectionInfoProviderTests.swift | 71 ++++++++++--------- .../Datadog/Mocks/CoreTelephonyMocks.swift | 7 ++ 3 files changed, 63 insertions(+), 46 deletions(-) diff --git a/Tests/DatadogIntegrationTests/Integration/LoggingIntegrationTests.swift b/Tests/DatadogIntegrationTests/Integration/LoggingIntegrationTests.swift index 4382aec676..0ce91e0350 100644 --- a/Tests/DatadogIntegrationTests/Integration/LoggingIntegrationTests.swift +++ b/Tests/DatadogIntegrationTests/Integration/LoggingIntegrationTests.swift @@ -97,19 +97,24 @@ class LoggingIntegrationTests: IntegrationTests { forKeyPath: LogMatcher.JSONKey.networkReachability, matches: { LogMatcher.allowedNetworkReachabilityValues.contains($0) } ) - matcher.assertValue( - forKeyPath: LogMatcher.JSONKey.networkAvailableInterfaces, - matches: { (values: [String]) -> Bool in - LogMatcher.allowedNetworkAvailableInterfacesValues.isSuperset(of: Set(values)) - } - ) - matcher.assertValue(forKeyPath: LogMatcher.JSONKey.networkConnectionSupportsIPv4, isTypeOf: Bool.self) - matcher.assertValue(forKeyPath: LogMatcher.JSONKey.networkConnectionSupportsIPv6, isTypeOf: Bool.self) - matcher.assertValue(forKeyPath: LogMatcher.JSONKey.networkConnectionIsExpensive, isTypeOf: Bool.self) - matcher.assertValue( - forKeyPath: LogMatcher.JSONKey.networkConnectionIsConstrained, - isTypeOf: Optional.self - ) + + if #available(iOS 12.0, *) { + matcher.assertValue( + forKeyPath: LogMatcher.JSONKey.networkAvailableInterfaces, + matches: { (values: [String]) -> Bool in + LogMatcher.allowedNetworkAvailableInterfacesValues.isSuperset(of: Set(values)) + } + ) + + matcher.assertValue(forKeyPath: LogMatcher.JSONKey.networkConnectionSupportsIPv4, isTypeOf: Bool.self) + matcher.assertValue(forKeyPath: LogMatcher.JSONKey.networkConnectionSupportsIPv6, isTypeOf: Bool.self) + matcher.assertValue(forKeyPath: LogMatcher.JSONKey.networkConnectionIsExpensive, isTypeOf: Bool.self) + + matcher.assertValue( + forKeyPath: LogMatcher.JSONKey.networkConnectionIsConstrained, + isTypeOf: Optional.self + ) + } #if targetEnvironment(simulator) // When running on iOS Simulator diff --git a/Tests/DatadogTests/Datadog/Core/System/NetworkConnectionInfoProviderTests.swift b/Tests/DatadogTests/Datadog/Core/System/NetworkConnectionInfoProviderTests.swift index 2b85b87b3b..ec0ad3763c 100644 --- a/Tests/DatadogTests/Datadog/Core/System/NetworkConnectionInfoProviderTests.swift +++ b/Tests/DatadogTests/Datadog/Core/System/NetworkConnectionInfoProviderTests.swift @@ -25,42 +25,45 @@ class NetworkConnectionInfoProviderTests: XCTestCase { // MARK: - iOS 12+ - @available(iOS 12, *) func testNWPathNetworkConnectionInfoProviderGivesValue() { - let provider = NWPathNetworkConnectionInfoProvider() + if #available(iOS 12.0, *) { + let provider = NWPathNetworkConnectionInfoProvider() - pullNetworkConnectionInfo( - from: provider, - on: DispatchQueue(label: "com.datadoghq.pulling-NWPathNetworkConnectionInfoProvider", target: .global(qos: .utility)), - thenFulfill: expectation(description: "Receive `NetworkConnectionInfo` from `NWPathNetworkConnectionInfoProvider`") - ) + pullNetworkConnectionInfo( + from: provider, + on: DispatchQueue(label: "com.datadoghq.pulling-NWPathNetworkConnectionInfoProvider", target: .global(qos: .utility)), + thenFulfill: expectation(description: "Receive `NetworkConnectionInfo` from `NWPathNetworkConnectionInfoProvider`") + ) - waitForExpectations(timeout: 1, handler: nil) + waitForExpectations(timeout: 1, handler: nil) + } } - @available(iOS 12, *) func testNWPathNetworkConnectionInfoProviderCanBeSafelyAccessedFromConcurrentThreads() { - let provider = NWPathNetworkConnectionInfoProvider() + if #available(iOS 12.0, *) { + let provider = NWPathNetworkConnectionInfoProvider() - DispatchQueue.concurrentPerform(iterations: 1_000) { _ in - _ = provider.current + DispatchQueue.concurrentPerform(iterations: 1_000) { _ in + _ = provider.current + } } } - @available(iOS 12, *) func testNWPathMonitorHandling() { - weak var nwPathMonitorWeakReference: NWPathMonitor? + if #available(iOS 12.0, *) { + weak var nwPathMonitorWeakReference: NWPathMonitor? - autoreleasepool { - let nwPathMonitor = NWPathMonitor() - _ = NWPathNetworkConnectionInfoProvider(monitor: nwPathMonitor) - nwPathMonitorWeakReference = nwPathMonitor - XCTAssertNotNil(nwPathMonitor.queue, "`NWPathMonitor` is started with synchronization queue") - } + autoreleasepool { + let nwPathMonitor = NWPathMonitor() + _ = NWPathNetworkConnectionInfoProvider(monitor: nwPathMonitor) + nwPathMonitorWeakReference = nwPathMonitor + XCTAssertNotNil(nwPathMonitor.queue, "`NWPathMonitor` is started with synchronization queue") + } - Thread.sleep(forTimeInterval: 0.5) + Thread.sleep(forTimeInterval: 0.5) - XCTAssertNil(nwPathMonitorWeakReference, "`NWPathMonitor` is deallocated with `NWPathNetworkConnectionInfoProvider`") + XCTAssertNil(nwPathMonitorWeakReference, "`NWPathMonitor` is deallocated with `NWPathNetworkConnectionInfoProvider`") + } } // MARK: - iOS 11 @@ -90,21 +93,23 @@ class NetworkConnectionInfoConversionTests: XCTestCase { typealias Reachability = NetworkConnectionInfo.Reachability typealias Interface = NetworkConnectionInfo.Interface - @available(iOS 12, *) func testNWPathStatus() { - XCTAssertEqual(Reachability(from: .satisfied), .yes) - XCTAssertEqual(Reachability(from: .unsatisfied), .no) - XCTAssertEqual(Reachability(from: .requiresConnection), .maybe) + if #available(iOS 12.0, *) { + XCTAssertEqual(Reachability(from: .satisfied), .yes) + XCTAssertEqual(Reachability(from: .unsatisfied), .no) + XCTAssertEqual(Reachability(from: .requiresConnection), .maybe) + } } - @available(iOS 12, *) func testNWInterface() { - XCTAssertEqual(Array(fromInterfaceTypes: []), []) - XCTAssertEqual(Array(fromInterfaceTypes: [.wifi]), [.wifi]) - XCTAssertEqual(Array(fromInterfaceTypes: [.wiredEthernet]), [.wiredEthernet]) - XCTAssertEqual(Array(fromInterfaceTypes: [.wifi, .wifi]), [.wifi, .wifi]) - XCTAssertEqual(Array(fromInterfaceTypes: [.wifi, .cellular]), [.wifi, .cellular]) - XCTAssertEqual(Array(fromInterfaceTypes: [.loopback, .other]), [.loopback, .other]) + if #available(iOS 12.0, *) { + XCTAssertEqual(Array(fromInterfaceTypes: []), []) + XCTAssertEqual(Array(fromInterfaceTypes: [.wifi]), [.wifi]) + XCTAssertEqual(Array(fromInterfaceTypes: [.wiredEthernet]), [.wiredEthernet]) + XCTAssertEqual(Array(fromInterfaceTypes: [.wifi, .wifi]), [.wifi, .wifi]) + XCTAssertEqual(Array(fromInterfaceTypes: [.wifi, .cellular]), [.wifi, .cellular]) + XCTAssertEqual(Array(fromInterfaceTypes: [.loopback, .other]), [.loopback, .other]) + } } func testSCReachability() { diff --git a/Tests/DatadogTests/Datadog/Mocks/CoreTelephonyMocks.swift b/Tests/DatadogTests/Datadog/Mocks/CoreTelephonyMocks.swift index 2fc63c0bc4..4add09751d 100644 --- a/Tests/DatadogTests/Datadog/Mocks/CoreTelephonyMocks.swift +++ b/Tests/DatadogTests/Datadog/Mocks/CoreTelephonyMocks.swift @@ -39,6 +39,13 @@ class CTTelephonyNetworkInfoMock: CTTelephonyNetworkInfo { _serviceSubscriberCellularProviders = serviceSubscriberCellularProviders } + // MARK: - iOS 12+ + override var serviceCurrentRadioAccessTechnology: [String: String]? { _serviceCurrentRadioAccessTechnology } override var serviceSubscriberCellularProviders: [String: CTCarrier]? { _serviceSubscriberCellularProviders } + + // MARK: - Prior to iOS 12 + + override var currentRadioAccessTechnology: String? { _serviceCurrentRadioAccessTechnology?.first?.value } + override var subscriberCellularProvider: CTCarrier? { _serviceSubscriberCellularProviders?.first?.value } }