Skip to content

Commit

Permalink
RUMM-494 Fix unit tests and integration tests on iOS 11 and iOS 12
Browse files Browse the repository at this point in the history
  • Loading branch information
ncreated committed Jun 9, 2020
1 parent d726bf6 commit 9da414b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool>.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<Bool>.self
)
}

#if targetEnvironment(simulator)
// When running on iOS Simulator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions Tests/DatadogTests/Datadog/Mocks/CoreTelephonyMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

0 comments on commit 9da414b

Please sign in to comment.