~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
33 lines · 925 B Raw History
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 } }
}