|
| 1 | +import XCTest |
| 2 | +@testable import AetherEngine |
| 3 | + |
| 4 | +final class SourceThrottleTests: XCTestCase { |
| 5 | + |
| 6 | + func testCostNsDisabledOrEmpty() { |
| 7 | + XCTAssertEqual(SourceThrottle.costNs(bytes: 125_000, kbps: 0), 0) |
| 8 | + XCTAssertEqual(SourceThrottle.costNs(bytes: 0, kbps: 1000), 0) |
| 9 | + } |
| 10 | + |
| 11 | + func testCostNs1000kbpsDelivers125kBPerSecond() { |
| 12 | + // 1000 kbit/s = 125000 byte/s, so 125000 bytes should cost exactly 1 second. |
| 13 | + XCTAssertEqual(SourceThrottle.costNs(bytes: 125_000, kbps: 1000), 1_000_000_000) |
| 14 | + // Half the bytes -> half the time. |
| 15 | + XCTAssertEqual(SourceThrottle.costNs(bytes: 62_500, kbps: 1000), 500_000_000) |
| 16 | + } |
| 17 | + |
| 18 | + func testCostNsScalesInverselyWithRate() { |
| 19 | + // Same payload at 2x the rate costs half the time. |
| 20 | + let slow = SourceThrottle.costNs(bytes: 1_000_000, kbps: 1000) |
| 21 | + let fast = SourceThrottle.costNs(bytes: 1_000_000, kbps: 2000) |
| 22 | + XCTAssertEqual(fast, slow / 2) |
| 23 | + } |
| 24 | + |
| 25 | + func testAdvanceSteadyStateSleepsOneChunkCost() { |
| 26 | + // Caught up (vclock == now): a 125 kB chunk at 1000 kbps must sleep ~1s. |
| 27 | + var vclock: UInt64 = 1_000_000_000 |
| 28 | + let sleep = SourceThrottle.advance( |
| 29 | + vclockNs: &vclock, nowNs: 1_000_000_000, deliveredBytes: 125_000, kbps: 1000) |
| 30 | + XCTAssertEqual(sleep, 1_000_000_000) |
| 31 | + XCTAssertEqual(vclock, 2_000_000_000) // virtual clock advanced by the chunk cost |
| 32 | + } |
| 33 | + |
| 34 | + func testAdvanceBacklogAccumulates() { |
| 35 | + // Two back-to-back chunks at the same `now` (producer reading fast): the second must wait |
| 36 | + // behind the first's virtual deadline, not deliver immediately. |
| 37 | + var vclock: UInt64 = 0 |
| 38 | + let now: UInt64 = 0 |
| 39 | + let s1 = SourceThrottle.advance(vclockNs: &vclock, nowNs: now, deliveredBytes: 125_000, kbps: 1000) |
| 40 | + let s2 = SourceThrottle.advance(vclockNs: &vclock, nowNs: now, deliveredBytes: 125_000, kbps: 1000) |
| 41 | + XCTAssertEqual(s1, 1_000_000_000) |
| 42 | + XCTAssertEqual(s2, 2_000_000_000) // queued behind chunk 1 |
| 43 | + XCTAssertEqual(vclock, 2_000_000_000) |
| 44 | + } |
| 45 | + |
| 46 | + func testAdvanceIdleGapDoesNotBankCredit() { |
| 47 | + // After delivering one chunk (vclock at 1s), a long real gap (now jumps to 10s) must not let |
| 48 | + // the next chunk deliver "for free" or bank negative sleep; base resets to now. |
| 49 | + var vclock: UInt64 = 1_000_000_000 |
| 50 | + let sleep = SourceThrottle.advance( |
| 51 | + vclockNs: &vclock, nowNs: 10_000_000_000, deliveredBytes: 125_000, kbps: 1000) |
| 52 | + XCTAssertEqual(sleep, 1_000_000_000) // just one chunk cost, no burst credit |
| 53 | + XCTAssertEqual(vclock, 11_000_000_000) // rebased to now + cost |
| 54 | + } |
| 55 | + |
| 56 | + func testAdvanceDisabledReturnsZero() { |
| 57 | + var vclock: UInt64 = 0 |
| 58 | + XCTAssertEqual( |
| 59 | + SourceThrottle.advance(vclockNs: &vclock, nowNs: 0, deliveredBytes: 125_000, kbps: 0), 0) |
| 60 | + XCTAssertEqual(vclock, 0) |
| 61 | + } |
| 62 | +} |
0 commit comments