DomainDigTests/LookupCacheTests.swift
145 lines · 5511 bytes
12 symbols in this file
LookupCacheTestsmakeRuntimetestTheFirstCallIsLiveAndTheSecondIsCachedtestTheSameDomainCachesEachSectionSeparatelytestDomainAndSubjectKeysDoNotCollidetestAKeyHoldingAnotherTypeFallsThroughToALiveFetchtestClearingTheCacheMakesTheNextCallLivetestClearingTheCacheCancelsWorkInFlightCounterincrementCancellationProberecord
1import XCTest
2@testable import DomainDig
3
4/// `LookupRuntime.execute` used to cache an enum of fourteen payload kinds and
5/// re-derive the caller's type from it with a closure, trapping on
6/// `fatalError` when the case did not match. It is generic over its value now,
7/// so the mismatch cannot be expressed. These pin what the cache does instead,
8/// which nothing covered when the trap was the only thing guarding it.
9///
10/// Every public method on the runtime reaches a live service, so `execute` is
11/// the seam. The operations here are closures that count their own calls.
12final class LookupCacheTests: XCTestCase {
13
14 private func makeRuntime() -> LookupRuntime { LookupRuntime() }
15
16 // MARK: Caching
17
18 func testTheFirstCallIsLiveAndTheSecondIsCached() async {
19 let runtime = makeRuntime()
20 let calls = Counter()
21
22 let first = await runtime.execute(key: .domain("example.com", .dns)) {
23 await calls.increment()
24 return 1
25 }
26 let second = await runtime.execute(key: .domain("example.com", .dns)) {
27 await calls.increment()
28 return 2
29 }
30
31 XCTAssertEqual(first.source, .live)
32 XCTAssertEqual(first.value, 1)
33 XCTAssertEqual(second.source, .cached)
34 XCTAssertEqual(second.value, 1, "the cached call returned a fresh value")
35 let callCount = await calls.count
36 XCTAssertEqual(callCount, 1, "the operation ran twice")
37 }
38
39 /// The key carries the section kind, so two sections of one domain are
40 /// separate entries. Collapsing them is what would have made a payload of
41 /// the wrong type reachable under the old design.
42 func testTheSameDomainCachesEachSectionSeparately() async {
43 let runtime = makeRuntime()
44
45 let dns = await runtime.execute(key: .domain("example.com", .dns)) { "dns" }
46 let ssl = await runtime.execute(key: .domain("example.com", .ssl)) { "ssl" }
47
48 XCTAssertEqual(dns.value, "dns")
49 XCTAssertEqual(ssl.value, "ssl")
50 XCTAssertEqual(ssl.source, .live, "the ssl key read the dns entry")
51 }
52
53 func testDomainAndSubjectKeysDoNotCollide() async {
54 let runtime = makeRuntime()
55
56 let domain = await runtime.execute(key: .domain("x", .ptr)) { "domain" }
57 let subject = await runtime.execute(key: .subject("x", .ptr)) { "subject" }
58
59 XCTAssertEqual(domain.value, "domain")
60 XCTAssertEqual(subject.value, "subject")
61 XCTAssertEqual(subject.source, .live)
62 }
63
64 /// A cached value of another type is not returned as this one. Under the
65 /// old design the equivalent check was the `extract` closure, and failing
66 /// it after a live fetch is what reached the `fatalError`. Here it must
67 /// simply fall through to a live fetch.
68 func testAKeyHoldingAnotherTypeFallsThroughToALiveFetch() async {
69 let runtime = makeRuntime()
70
71 let stored = await runtime.execute(key: .domain("example.com", .dns)) { 42 }
72 let reread = await runtime.execute(key: .domain("example.com", .dns)) { "a string" }
73
74 XCTAssertEqual(stored.value, 42)
75 XCTAssertEqual(reread.value, "a string")
76 XCTAssertEqual(reread.source, .live)
77 }
78
79 // MARK: Clearing
80
81 func testClearingTheCacheMakesTheNextCallLive() async {
82 let runtime = makeRuntime()
83 let calls = Counter()
84
85 _ = await runtime.execute(key: .domain("example.com", .dns)) {
86 await calls.increment()
87 return 1
88 }
89 await runtime.clearCache()
90 let afterClear = await runtime.execute(key: .domain("example.com", .dns)) {
91 await calls.increment()
92 return 2
93 }
94
95 XCTAssertEqual(afterClear.source, .live)
96 XCTAssertEqual(afterClear.value, 2)
97 let callCount = await calls.count
98 XCTAssertEqual(callCount, 2)
99 }
100
101 /// `clearCache()` cancels what is in flight, and both callers are
102 /// user-initiated — the Clear Lookup Cache action and the full data wipe.
103 /// The erased task `inFlight` holds is a wrapper around the real one, so
104 /// cancelling the wrapper alone would leave the work running.
105 func testClearingTheCacheCancelsWorkInFlight() async {
106 let runtime = makeRuntime()
107 let observed = CancellationProbe()
108
109 let pending = Task {
110 await runtime.execute(key: .domain("example.com", .subdomains)) { () -> Int in
111 for _ in 0..<200 {
112 if Task.isCancelled {
113 await observed.record()
114 return -1
115 }
116 try? await Task.sleep(nanoseconds: 5_000_000)
117 }
118 return 0
119 }
120 }
121
122 // Let the operation reach its loop before clearing.
123 try? await Task.sleep(nanoseconds: 50_000_000)
124 await runtime.clearCache()
125 _ = await pending.value
126
127 let sawCancellation = await observed.wasCancelled
128 XCTAssertTrue(
129 sawCancellation,
130 "the operation never saw cancellation, so clearCache() did not reach it"
131 )
132 }
133}
134
135/// `actor` rather than a captured `var`: the operations run off the test's
136/// context and Swift 6 will not let them mutate shared state otherwise.
137private actor Counter {
138 private(set) var count = 0
139 func increment() { count += 1 }
140}
141
142private actor CancellationProbe {
143 private(set) var wasCancelled = false
144 func record() { wasCancelled = true }
145}