SPB Git

Repository Public License

CacheTests.swift 7.3 KB 145 lines swift

1// ORIX Secure2// File: Packages/ORIXMediaCleanup/Tests/ORIXMediaCleanupTests/CacheTests.swift3// Description: Persistent fingerprint cache: hits and misses, invalidation on modification date, corrupt files, version bumps.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXMediaCleanup1011@Suite("Fingerprint cache")12struct CacheTests {13    static func temporaryDirectory() -> URL {14        FileManager.default.temporaryDirectory.appendingPathComponent("orix-media-cache-tests-\(UUID().uuidString)", isDirectory: true)15    }1617    static func sample(_ value: UInt64) -> ImageFingerprint {18        Fixture.fingerprint(d: value, p: ~value, luma: UInt8(value % 200), signature: (0..<48).map { UInt8(truncatingIfNeeded: value &+ UInt64($0)) })19    }2021    @Test("Miss, store, hit; persisted across loads")22    func hitAndMiss() throws {23        let directory = Self.temporaryDirectory()24        defer { try? FileManager.default.removeItem(at: directory) }25        let modified = Date(timeIntervalSinceReferenceDate: 12345.678)26        var cache = FingerprintCache(directory: directory)27        #expect(cache.count == 0)28        #expect(cache.statistics.discarded == nil)29        #expect(cache.fingerprint(for: "A/L0/001", modificationDate: modified) == nil)30        cache.store(Self.sample(1), for: "A/L0/001", modificationDate: modified)31        cache.store(Self.sample(2), for: "ünïcödé-id", modificationDate: nil)32        #expect(cache.fingerprint(for: "A/L0/001", modificationDate: modified) == Self.sample(1))33        #expect(cache.statistics.misses == 1)34        #expect(cache.statistics.hits == 1)35        try cache.save()3637        var reloaded = FingerprintCache(directory: directory)38        #expect(reloaded.count == 2)39        #expect(reloaded.statistics.loadedEntries == 2)40        #expect(reloaded.fingerprint(for: "A/L0/001", modificationDate: modified) == Self.sample(1))41        #expect(reloaded.fingerprint(for: "ünïcödé-id", modificationDate: nil) == Self.sample(2))42        #expect(reloaded.fingerprint(for: "unknown", modificationDate: nil) == nil)43        #expect(reloaded.statistics.hits == 2)44        #expect(reloaded.statistics.misses == 1)45        #expect(reloaded.statistics.lookups == 3)46        #expect(abs(reloaded.statistics.hitRate - 2.0 / 3.0) < 1e-9)47    }4849    @Test("A changed modification date invalidates the entry")50    func invalidation() throws {51        let directory = Self.temporaryDirectory()52        defer { try? FileManager.default.removeItem(at: directory) }53        let before = Date(timeIntervalSinceReferenceDate: 1000)54        let after = Date(timeIntervalSinceReferenceDate: 2000)55        var cache = FingerprintCache(directory: directory)56        cache.store(Self.sample(9), for: "asset", modificationDate: before)57        try cache.save()58        var reloaded = FingerprintCache(directory: directory)59        #expect(reloaded.fingerprint(for: "asset", modificationDate: after) == nil)60        #expect(reloaded.fingerprint(for: "asset", modificationDate: nil) == nil)61        #expect(reloaded.statistics.invalidated == 2)62        #expect(reloaded.statistics.hits == 0)63        reloaded.store(Self.sample(10), for: "asset", modificationDate: after)64        #expect(reloaded.fingerprint(for: "asset", modificationDate: after) == Self.sample(10))65        #expect(reloaded.fingerprint(for: "asset", modificationDate: before) == nil)66    }6768    @Test("Corrupt files are discarded, never trusted", arguments: ["garbage", "truncated", "bit flip", "empty", "bad magic"])69    func corrupt(_ damage: String) throws {70        let directory = Self.temporaryDirectory()71        defer { try? FileManager.default.removeItem(at: directory) }72        var cache = FingerprintCache(directory: directory)73        for index in 0..<50 { cache.store(Self.sample(UInt64(index)), for: "asset-\(index)", modificationDate: Date(timeIntervalSinceReferenceDate: Double(index))) }74        try cache.save()75        var bytes = [UInt8](try Data(contentsOf: cache.fileURL))76        switch damage {77        case "garbage": bytes = Array("this is not a cache file at all, just text".utf8)78        case "truncated": bytes = Array(bytes.prefix(bytes.count / 2))79        case "bit flip": bytes[bytes.count / 2] ^= 0x1080        case "empty": bytes = []81        default: bytes[0] = 0x0082        }83        try Data(bytes).write(to: cache.fileURL)84        var damaged = FingerprintCache(directory: directory)85        #expect(damaged.count == 0)86        #expect(damaged.statistics.discarded == .corrupt)87        #expect(damaged.fingerprint(for: "asset-1", modificationDate: Date(timeIntervalSinceReferenceDate: 1)) == nil)88        // A fresh save replaces the damaged file.89        damaged.store(Self.sample(3), for: "new", modificationDate: nil)90        try damaged.save()91        let healed = FingerprintCache(directory: directory)92        #expect(healed.count == 1)93        #expect(healed.statistics.discarded == nil)94    }9596    @Test("A version bump discards the old file")97    func versionBump() throws {98        let directory = Self.temporaryDirectory()99        defer { try? FileManager.default.removeItem(at: directory) }100        var old = FingerprintCache(directory: directory, version: FingerprintCache.currentVersion)101        old.store(Self.sample(4), for: "asset", modificationDate: nil)102        try old.save()103        let newer = FingerprintCache(directory: directory, version: FingerprintCache.currentVersion + 1)104        #expect(newer.count == 0)105        #expect(newer.statistics.discarded == .versionMismatch)106        let same = FingerprintCache(directory: directory)107        #expect(same.count == 1)108    }109110    @Test("Prune, remove, malformed fingerprints, missing directory")111    func housekeeping() throws {112        let directory = Self.temporaryDirectory().appendingPathComponent("nested/deeper", isDirectory: true)113        defer { try? FileManager.default.removeItem(at: directory) }114        var cache = FingerprintCache(directory: directory)115        for index in 0..<10 { cache.store(Self.sample(UInt64(index)), for: "asset-\(index)", modificationDate: nil) }116        cache.store(ImageFingerprint(dHash: 1, pHash: 2, meanLuma: 3, colorSignature: [1, 2, 3]), for: "malformed", modificationDate: nil)117        #expect(cache.count == 10)118        cache.prune(keeping: ["asset-1", "asset-2", "gone"])119        #expect(cache.count == 2)120        try cache.save()121        var reloaded = FingerprintCache(directory: directory)122        #expect(reloaded.count == 2)123        reloaded.removeAll()124        try reloaded.save()125        #expect(FingerprintCache(directory: directory).count == 0)126    }127128    @Test("Round trip of 20 000 entries")129    func large() throws {130        let directory = Self.temporaryDirectory()131        defer { try? FileManager.default.removeItem(at: directory) }132        var cache = FingerprintCache(directory: directory)133        var rng = SeededGenerator(seed: 8)134        for index in 0..<20_000 { cache.store(Self.sample(rng.next()), for: "asset-\(index)", modificationDate: Date(timeIntervalSinceReferenceDate: Double(index))) }135        let clock = ContinuousClock()136        let start = clock.now137        try cache.save()138        let reloaded = FingerprintCache(directory: directory)139        let elapsed = clock.now - start140        let size = try Data(contentsOf: cache.fileURL).count141        print("CACHE 20 000 entries: \(size) bytes, save + load \(elapsed)")142        #expect(reloaded.count == 20_000)143    }144}145
Files main
Menu

Theme