|
| 1 | +// |
| 2 | +// FlushTokenBucketTests.swift |
| 3 | +// |
| 4 | +// Tests for the demand-adaptive (token-bucket) flush governor and the |
| 5 | +// queue-depth early-flush trigger. |
| 6 | +// |
| 7 | +// Copyright (c) 2026 Klaviyo |
| 8 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 9 | +// |
| 10 | + |
| 11 | +@testable import KlaviyoCore |
| 12 | +@testable import KlaviyoSwift |
| 13 | +import Foundation |
| 14 | +import XCTest |
| 15 | + |
| 16 | +final class FlushTokenBucketTests: XCTestCase { |
| 17 | + @MainActor |
| 18 | + override func setUp() async throws { |
| 19 | + environment = KlaviyoEnvironment.test() |
| 20 | + klaviyoSwiftEnvironment = KlaviyoSwiftEnvironment.test() |
| 21 | + } |
| 22 | + |
| 23 | + private func sampleRequest(name: String = "test") -> KlaviyoRequest { |
| 24 | + KlaviyoRequest(endpoint: .createEvent("foo", CreateEventPayload(data: .init(name: name)))) |
| 25 | + } |
| 26 | + |
| 27 | + // MARK: - consumeFlushToken(currentTime:) |
| 28 | + |
| 29 | + func test_consumeFlushToken_fullBucketAllowsFlushAndDecrementsByOne() { |
| 30 | + var state = KlaviyoState(queue: []) |
| 31 | + state.flushInterval = StateManagementConstants.wifiFlushInterval |
| 32 | + |
| 33 | + let flushTime = Date(timeIntervalSince1970: 1000) |
| 34 | + XCTAssertTrue(state.consumeFlushToken(currentTime: flushTime)) |
| 35 | + XCTAssertEqual( |
| 36 | + state.availableFlushTokens, |
| 37 | + StateManagementConstants.flushTokenBucketCapacity - 1, |
| 38 | + accuracy: 0.0001 |
| 39 | + ) |
| 40 | + XCTAssertEqual(state.lastFlushTokenRefill, flushTime) |
| 41 | + } |
| 42 | + |
| 43 | + func test_consumeFlushToken_emptyBucketIsDeniedUntilEnoughTimeElapses() { |
| 44 | + var state = KlaviyoState(queue: []) |
| 45 | + state.flushInterval = StateManagementConstants.wifiFlushInterval // 10s -> 0.1 token/sec |
| 46 | + state.availableFlushTokens = 0 |
| 47 | + let start = Date(timeIntervalSince1970: 1000) |
| 48 | + state.lastFlushTokenRefill = start |
| 49 | + |
| 50 | + // After 5s only half a token has accrued -> still denied. |
| 51 | + XCTAssertFalse(state.consumeFlushToken(currentTime: start.addingTimeInterval(5))) |
| 52 | + XCTAssertEqual(state.availableFlushTokens, 0.5, accuracy: 0.0001) |
| 53 | + |
| 54 | + // 10 more seconds accrues a full token -> allowed, then decremented. |
| 55 | + XCTAssertTrue(state.consumeFlushToken(currentTime: start.addingTimeInterval(15))) |
| 56 | + XCTAssertEqual(state.availableFlushTokens, 0.5, accuracy: 0.0001) |
| 57 | + } |
| 58 | + |
| 59 | + func test_consumeFlushToken_refillIsCappedAtCapacity() { |
| 60 | + var state = KlaviyoState(queue: []) |
| 61 | + state.flushInterval = StateManagementConstants.wifiFlushInterval |
| 62 | + state.availableFlushTokens = 4 |
| 63 | + let start = Date(timeIntervalSince1970: 1000) |
| 64 | + state.lastFlushTokenRefill = start |
| 65 | + |
| 66 | + // 100s would add 10 tokens, but the bucket is capped at capacity before consuming. |
| 67 | + XCTAssertTrue(state.consumeFlushToken(currentTime: start.addingTimeInterval(100))) |
| 68 | + XCTAssertEqual( |
| 69 | + state.availableFlushTokens, |
| 70 | + StateManagementConstants.flushTokenBucketCapacity - 1, |
| 71 | + accuracy: 0.0001 |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + func test_consumeFlushToken_doesNotRefillWhenOffline() { |
| 76 | + var state = KlaviyoState(queue: []) |
| 77 | + state.flushInterval = .infinity // offline: timer paused, bucket frozen |
| 78 | + state.availableFlushTokens = 0 |
| 79 | + let start = Date(timeIntervalSince1970: 1000) |
| 80 | + state.lastFlushTokenRefill = start |
| 81 | + |
| 82 | + XCTAssertFalse(state.consumeFlushToken(currentTime: start.addingTimeInterval(10_000))) |
| 83 | + XCTAssertEqual(state.availableFlushTokens, 0, accuracy: 0.0001) |
| 84 | + XCTAssertEqual(state.lastFlushTokenRefill, start.addingTimeInterval(10_000)) |
| 85 | + } |
| 86 | + |
| 87 | + // MARK: - shouldFlushForQueueDepth |
| 88 | + |
| 89 | + func test_shouldFlushForQueueDepth_belowThresholdIsFalse() { |
| 90 | + let queue = Array(repeating: sampleRequest(), count: StateManagementConstants.flushDepth - 1) |
| 91 | + var state = KlaviyoState(queue: queue) |
| 92 | + state.flushInterval = StateManagementConstants.wifiFlushInterval |
| 93 | + XCTAssertFalse(state.shouldFlushForQueueDepth) |
| 94 | + } |
| 95 | + |
| 96 | + func test_shouldFlushForQueueDepth_atThresholdIsTrue() { |
| 97 | + let queue = Array(repeating: sampleRequest(), count: StateManagementConstants.flushDepth) |
| 98 | + var state = KlaviyoState(queue: queue) |
| 99 | + state.flushInterval = StateManagementConstants.wifiFlushInterval |
| 100 | + XCTAssertTrue(state.shouldFlushForQueueDepth) |
| 101 | + } |
| 102 | + |
| 103 | + func test_shouldFlushForQueueDepth_isFalseWhenOffline() { |
| 104 | + let queue = Array(repeating: sampleRequest(), count: StateManagementConstants.flushDepth) |
| 105 | + var state = KlaviyoState(queue: queue) |
| 106 | + state.flushInterval = .infinity |
| 107 | + XCTAssertFalse(state.shouldFlushForQueueDepth) |
| 108 | + } |
| 109 | + |
| 110 | + // MARK: - flushQueue token gate (reducer) |
| 111 | + |
| 112 | + @MainActor |
| 113 | + func test_flushQueue_withDepletedBucketDoesNotFlush() async { |
| 114 | + var initialState = INITIALIZED_TEST_STATE() |
| 115 | + initialState.flushing = false |
| 116 | + initialState.availableFlushTokens = 0 |
| 117 | + // Refilled "now", so no time has elapsed at flush time -> no token accrues. |
| 118 | + initialState.lastFlushTokenRefill = environment.date() |
| 119 | + initialState.queue = [initialState.buildProfileRequest( |
| 120 | + apiKey: initialState.apiKey!, |
| 121 | + anonymousId: initialState.anonymousId! |
| 122 | + )] |
| 123 | + let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) |
| 124 | + |
| 125 | + // Bucket is empty and nothing refills, so the flush is deferred: |
| 126 | + // no state change and, crucially, no `.sendRequest` is emitted. |
| 127 | + _ = await store.send(.flushQueue) |
| 128 | + } |
| 129 | + |
| 130 | + // MARK: - queue-depth early-flush trigger (reducer) |
| 131 | + |
| 132 | + @MainActor |
| 133 | + func test_enqueueAggregateEventReachingQueueDepthTriggersFlush() async { |
| 134 | + var initialState = INITIALIZED_TEST_STATE() |
| 135 | + initialState.flushing = false |
| 136 | + // Empty the bucket so the triggered flush is gated. This keeps the test focused on the |
| 137 | + // depth trigger itself rather than the downstream send cascade. |
| 138 | + initialState.availableFlushTokens = 0 |
| 139 | + initialState.queue = Array( |
| 140 | + repeating: sampleRequest(name: "filler"), |
| 141 | + count: StateManagementConstants.flushDepth - 1 |
| 142 | + ) |
| 143 | + let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) |
| 144 | + store.exhaustivity = .off |
| 145 | + |
| 146 | + // Enqueuing one more request reaches `flushDepth`, which schedules an early flush. |
| 147 | + await store.send(.enqueueAggregateEvent(Data("agg".utf8))) |
| 148 | + await store.receive(.flushQueue) |
| 149 | + } |
| 150 | +} |
0 commit comments