45 lines
1.9 KiB
Swift
45 lines
1.9 KiB
Swift
|
|
import XCTest
|
||
|
|
@testable import NetworkCityCore
|
||
|
|
|
||
|
|
final class TrafficDifferTests: XCTestCase {
|
||
|
|
|
||
|
|
private func conn(_ host: String, in bIn: UInt64, out bOut: UInt64) -> Connection {
|
||
|
|
Connection(
|
||
|
|
proto: .tcp4,
|
||
|
|
local: Endpoint(host: "192.168.1.2", port: 5000),
|
||
|
|
remote: Endpoint(host: host, port: 443),
|
||
|
|
bytesIn: bIn, bytesOut: bOut,
|
||
|
|
processName: "test", pid: 1
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
func testFirstSnapshotYieldsNoDeltas() {
|
||
|
|
let differ = TrafficDiffer()
|
||
|
|
let t0 = Date(timeIntervalSince1970: 1000)
|
||
|
|
let deltas = differ.ingest(ConnectionSnapshot(timestamp: t0, connections: [conn("1.1.1.1", in: 100, out: 50)]))
|
||
|
|
XCTAssertTrue(deltas.isEmpty) // need two readings to know a rate
|
||
|
|
}
|
||
|
|
|
||
|
|
func testComputesDeltaAndRate() {
|
||
|
|
let differ = TrafficDiffer()
|
||
|
|
let t0 = Date(timeIntervalSince1970: 1000)
|
||
|
|
let t1 = Date(timeIntervalSince1970: 1002) // +2s
|
||
|
|
_ = differ.ingest(ConnectionSnapshot(timestamp: t0, connections: [conn("1.1.1.1", in: 100, out: 50)]))
|
||
|
|
let deltas = differ.ingest(ConnectionSnapshot(timestamp: t1, connections: [conn("1.1.1.1", in: 1124, out: 50)]))
|
||
|
|
|
||
|
|
XCTAssertEqual(deltas.count, 1)
|
||
|
|
XCTAssertEqual(deltas[0].bytesInDelta, 1024)
|
||
|
|
XCTAssertEqual(deltas[0].bytesOutDelta, 0)
|
||
|
|
XCTAssertEqual(deltas[0].inBytesPerSec, 512, accuracy: 0.001) // 1024 / 2s
|
||
|
|
}
|
||
|
|
|
||
|
|
func testCounterResetTreatedAsZero() {
|
||
|
|
let differ = TrafficDiffer()
|
||
|
|
let t0 = Date(timeIntervalSince1970: 1000)
|
||
|
|
let t1 = Date(timeIntervalSince1970: 1002)
|
||
|
|
_ = differ.ingest(ConnectionSnapshot(timestamp: t0, connections: [conn("1.1.1.1", in: 9999, out: 0)]))
|
||
|
|
let deltas = differ.ingest(ConnectionSnapshot(timestamp: t1, connections: [conn("1.1.1.1", in: 10, out: 0)]))
|
||
|
|
XCTAssertTrue(deltas.isEmpty) // reused tuple, counter dropped -> no phantom delta
|
||
|
|
}
|
||
|
|
}
|