-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathNTPIntegrationSpec.swift
More file actions
70 lines (62 loc) · 2.3 KB
/
NTPIntegrationSpec.swift
File metadata and controls
70 lines (62 loc) · 2.3 KB
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
//
// NTPIntegrationSpec.swift
// TrueTime
//
// Created by Michael Sanders on 8/1/16.
// Copyright © 2016 Instacart. All rights reserved.
//
@testable import TrueTime
import Foundation
import Nimble
import Quick
final class NTPIntegrationSpec: QuickSpec {
override func spec() {
describe("fetchIfNeeded") {
it("should ignore outliers") {
self.testReferenceTimeOutliers()
}
}
}
}
private extension NTPIntegrationSpec {
func testReferenceTimeOutliers() {
let clients = (0..<100).map { _ in TrueTimeClient() }
waitUntil(timeout: 60) { done in
var results: [ReferenceTimeResult?] = Array(repeating: nil, count: clients.count)
let start = NSDate()
let finish = {
let end = NSDate()
let results = results.compactMap { $0 }
let times = results.compactMap { try? $0.get() }
let errors: [Error] = results.compactMap {
guard case let .failure(failure) = $0 else { return nil }
return failure
}
expect(times).notTo(beEmpty(), description: "Expected times, got: \(errors)")
print("Got \(times.count) times for \(results.count) results")
let sortedTimes = times.sorted {
$0.time.timeIntervalSince1970 < $1.time.timeIntervalSince1970
}
if !sortedTimes.isEmpty {
let medianTime = sortedTimes[sortedTimes.count / 2]
let maxDelta = end.timeIntervalSince1970 - start.timeIntervalSince1970
for time in times {
let delta = abs(time.time.timeIntervalSince1970 -
medianTime.time.timeIntervalSince1970)
expect(delta) <= maxDelta
}
}
done()
}
for (idx, client) in clients.enumerated() {
client.start(pool: ["time.apple.com"])
client.fetchIfNeeded { result in
results[idx] = result
if !results.contains(where: { $0 == nil }) {
finish()
}
}
}
}
}
}