forked from ashleymills/Reachability.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReachabilityTests.swift
75 lines (57 loc) · 2.33 KB
/
ReachabilityTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// ReachabilityTests.swift
// ReachabilityTests
//
// Created by Ashley Mills on 23/11/2015.
// Copyright © 2015 Ashley Mills. All rights reserved.
//
import XCTest
@testable import Reachability
class ReachabilityTests: XCTestCase {
func testValidHost() {
let validHostName = "google.com"
guard let reachability = try? Reachability(hostname: validHostName) else {
return XCTFail("Unable to create reachability")
}
let expected = expectation(description: "Check valid host")
reachability.whenReachable = { reachability in
print("Pass: \(validHostName) is reachable - \(reachability)")
// Only fulfill the expectation on host reachable
expected.fulfill()
}
reachability.whenUnreachable = { reachability in
print("\(validHostName) is initially unreachable - \(reachability)")
// Expectation isn't fulfilled here, so wait will time out if this is the only closure called
}
do {
try reachability.startNotifier()
} catch {
return XCTFail("Unable to start notifier")
}
waitForExpectations(timeout: 5, handler: nil)
reachability.stopNotifier()
}
func testInvalidHost() {
// Testing with an invalid host will initially show as reachable, but then the callback
// gets fired a second time reporting the host as unreachable
let invalidHostName = "invalidhost"
guard let reachability = try? Reachability(hostname: invalidHostName) else {
return XCTFail("Unable to create reachability")
}
let expected = expectation(description: "Check invalid host")
reachability.whenReachable = { reachability in
print("\(invalidHostName) is initially reachable - \(reachability)")
}
reachability.whenUnreachable = { reachability in
print("Pass: \(invalidHostName) is unreachable - \(reachability))")
expected.fulfill()
}
do {
try reachability.startNotifier()
} catch {
return XCTFail("Unable to start notifier")
}
waitForExpectations(timeout: 5, handler: nil)
reachability.stopNotifier()
}
}