1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Foundation
import XCTest
@testable import OldFella
final class NotificationHydrationGate: @unchecked Sendable {
private let lock = NSLock()
private let release = DispatchSemaphore(value: 0)
private let started: XCTestExpectation
private var active = 0
private var maximum = 0
private var total = 0
init(started: XCTestExpectation) { self.started = started }
func wait() {
let mustWait = lock.withLock {
active += 1
total += 1
maximum = max(maximum, active)
return total <= 3
}
if mustWait {
started.fulfill()
_ = release.wait(timeout: .now() + 10)
}
lock.withLock { active -= 1 }
}
func resume() { for _ in 0..<3 { release.signal() } }
var requestCount: Int { lock.withLock { total } }
var maximumConcurrentRequests: Int { lock.withLock { maximum } }
}