Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import Foundationimport ParcelTestSupportimport Testing
struct IdleWaitAllowance: Decodable { let path: String let count: Int let why: String}
enum IdleTimerGuard { static var waits: Regex<Substring> { /Timer\.scheduledTimer|DispatchSource\.makeTimerSource|Task\.sleep|clock\s*\.\s*sleep|asyncAfter|scheduler\s*\.\s*schedule\s*\(/ }
static func validate(_ source: String, allowance: IdleWaitAllowance?) -> Bool { let matches = source.matches(of: waits).count guard matches > 0 else { return allowance == nil } guard let allowance, allowance.count == matches, !allowance.why.isEmpty else { return false } if source.contains("DispatchSource.makeTimerSource") { guard !source.contains("flags: .strict"), source.contains("repeating: .never") else { return false } } return true }}
extension PerformanceTestSuite { @Suite("No unreviewed idle timers", .tags(.benchmark)) struct IdleTimerGuardTests { @Test func everyWaitHasAnExplicitBoundedActivityReason() throws { let repository = BenchmarkBaselines.repository let allowances = try JSONDecoder().decode( [IdleWaitAllowance].self, from: Data(contentsOf: repository.appendingPathComponent("Benchmarks/idle-waits.json"))) #expect(Set(allowances.map(\.path)).count == allowances.count) let byPath = Dictionary(uniqueKeysWithValues: allowances.map { ($0.path, $0) }) let sources = repository.appendingPathComponent("Sources") let enumerator = try #require( FileManager.default.enumerator( at: sources, includingPropertiesForKeys: nil)) var used: Set<String> = [] for case let url as URL in enumerator where url.pathExtension == "swift" { let path = String(url.path.dropFirst(sources.path.count + 1)) let source = try String(contentsOf: url, encoding: .utf8) #expect(IdleTimerGuard.validate(source, allowance: byPath[path]), "Unreviewed or changed wait: \(path)") if byPath[path] != nil { used.insert(path) } } #expect(used == Set(byPath.keys), "Remove stale idle-wait allowances") }
@Test func newRepeatingOrStrictTimersAreRejected() { #expect(!IdleTimerGuard.validate("Timer.scheduledTimer()", allowance: nil)) let allowance = IdleWaitAllowance(path: "fixture", count: 1, why: "bounded") #expect( !IdleTimerGuard.validate( "DispatchSource.makeTimerSource(flags: .strict); repeating: .never", allowance: allowance)) #expect( !IdleTimerGuard.validate( "DispatchSource.makeTimerSource(); repeating: .seconds(1)", allowance: allowance)) #expect( IdleTimerGuard.validate("DispatchSource.makeTimerSource(); repeating: .never", allowance: allowance)) #expect(!IdleTimerGuard.validate("Task.sleep(); Task.sleep()", allowance: allowance)) #expect(!IdleTimerGuard.validate("clock.sleep(until: deadline)", allowance: nil)) #expect(IdleTimerGuard.validate("clock.sleep(until: deadline)", allowance: allowance)) #expect(!IdleTimerGuard.validate("scheduler.schedule(after: 1) {}", allowance: nil)) #expect(!IdleTimerGuard.validate("self.scheduler . schedule (after: 1) {}", allowance: nil)) #expect(IdleTimerGuard.validate("scheduler.schedule(after: 0) {}", allowance: allowance)) #expect( !IdleTimerGuard.validate( "scheduler.schedule(after: 1) {}; Task.sleep()", allowance: allowance)) } }}