Commit
This commit is contained in:
22
Carthage/Checkouts/TrueTime.swift/Tests/ArbitraryExtensions.swift
vendored
Normal file
22
Carthage/Checkouts/TrueTime.swift/Tests/ArbitraryExtensions.swift
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// ArbitraryExtensions.swift
|
||||
// TrueTime
|
||||
//
|
||||
// Created by Michael Sanders on 7/19/16.
|
||||
// Copyright © 2016 Instacart. All rights reserved.
|
||||
//
|
||||
|
||||
@testable import TrueTime
|
||||
import SwiftCheck
|
||||
|
||||
extension timeval: Arbitrary {
|
||||
public static var arbitrary: Gen<timeval> {
|
||||
return Gen<(Int, Int32)>.zip(Int.arbitrary, Int32.arbitrary).map(timeval.init)
|
||||
}
|
||||
}
|
||||
|
||||
extension timeval {
|
||||
static var arbitraryPositive: Gen<timeval> {
|
||||
return arbitrary.suchThat { $0.tv_sec > 0 && $0.tv_usec > 0 }
|
||||
}
|
||||
}
|
24
Carthage/Checkouts/TrueTime.swift/Tests/Info.plist
vendored
Normal file
24
Carthage/Checkouts/TrueTime.swift/Tests/Info.plist
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>BNDL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
23
Carthage/Checkouts/TrueTime.swift/Tests/NTPExtensionsSpec.swift
vendored
Normal file
23
Carthage/Checkouts/TrueTime.swift/Tests/NTPExtensionsSpec.swift
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// NTPExtensionsSpec.swift
|
||||
// TrueTime
|
||||
//
|
||||
// Created by Michael Sanders on 7/18/16.
|
||||
// Copyright © 2016 Instacart. All rights reserved.
|
||||
//
|
||||
|
||||
@testable import TrueTime
|
||||
import Nimble
|
||||
import Quick
|
||||
import SwiftCheck
|
||||
|
||||
final class NTPExtensionsSpec: QuickSpec {
|
||||
override func spec() {
|
||||
it("ntp_time64_t") {
|
||||
property("Matches timeval precision") <- forAll(timeval.arbitraryPositive) { time in
|
||||
let ntp = ntp_time64_t(timeSince1970: time)
|
||||
return ntp.milliseconds == time.milliseconds
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
69
Carthage/Checkouts/TrueTime.swift/Tests/NTPIntegrationSpec.swift
vendored
Normal file
69
Carthage/Checkouts/TrueTime.swift/Tests/NTPIntegrationSpec.swift
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
//
|
||||
// NTPIntegrationSpec.swift
|
||||
// TrueTime
|
||||
//
|
||||
// Created by Michael Sanders on 8/1/16.
|
||||
// Copyright © 2016 Instacart. All rights reserved.
|
||||
//
|
||||
|
||||
@testable import TrueTime
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user