Registry Mirror
Skip to content

Latest commit

 

History

History
93 lines (79 loc) · 4.26 KB

TestingYourStore.md

File metadata and controls

93 lines (79 loc) · 4.26 KB

Testing your store

Run the conformance kit against your setup, test your failure handling, and keep concurrency tests deterministic.

Run the conformance kit

Give the kit your migrations and, if your store doesn't use the default configuration, a function that builds it for a path. Every check runs in its own temporary directory with its own probe tables (sqliteswift_probe*), so it never touches real data. One parameterized test reports each check separately:

import SQLiteSwiftTesting
import Testing

@Test(arguments: SQLiteConformanceKit.Check.allCases)
func conformance(check: SQLiteConformanceKit.Check) async {
    let kit = SQLiteConformanceKit(migrations: MyStore.migrations) { path in
        var configuration = SQLiteConfiguration(path: path)
        configuration.readerCount = 2  // as your store opens it
        return configuration
    }
    let result = await kit.run(check)
    #expect(result.outcome == .passed, "\(result.notes)")
}
Check What it shows
migrations Migrations apply once and in order; a failing one rolls back alone.
schemaTooNew A newer database is refused and left untouched.
compareAndSetConflict Compare-and-set reports every result; of two concurrent updates exactly one wins.
commitFailure A COMMIT rejected while the transaction is open is a typed error, not an unknown outcome.
commitOutcome Reported outcomes match what is durable.
busyContention A second database on the file gets .busy after its timeout, then succeeds.
cancellationInterrupt Cancellation interrupts, rolls back and leaves the database usable.
operationLimits Deadlines and instruction budgets stop work with typed errors and roll writes back.
walCheckpoint Checkpoints copy every frame back; .truncate empties the WAL.
recoveryAfterAbortedWrite A crash image taken mid-write recovers to the last commit.
diskFull A full disk fails the write with .full and the database keeps working.
settingsVerification The settings read back match the configuration.
fileIdentity The file identity is written, accepted on reopen and enforced.

A check that doesn't apply to your setup is SQLiteConformanceKit/Outcome/skipped(_:) with the reason: the file-based checks on an in-memory database, and commitFailure without foreignKeys. New checks can arrive in minor releases; a test over SQLiteConformanceKit/Check/allCases picks them up.

Test your handling of an unknown commit outcome

An unknown commit outcome is rare in production and hard to provoke. Arm it with SQLiteSwift/SQLiteDatabase/injectFault(_:): the next write that commits throws commitOutcomeUnknown, and nothing of it is durable.

try await database.injectFault(.commitOutcomeUnknown)
let outcome = await database.writeReportingOutcome { tx throws(SQLiteError) in
    try tx.execute("INSERT INTO inputs (key) VALUES (?)", [.text(key)])
}
guard case .unknown = outcome else { Issue.record("expected an unknown outcome"); return }
let stored = try await database.resolveCommit { tx throws(SQLiteError) in
    try tx.queryOne("SELECT 1 FROM inputs WHERE key = ?", [.text(key)]) != nil
}
#expect(!stored)

Keep tests deterministic

  • SQLiteTemporaryDirectory gives each test its own directory and removes it on deinit, so tests run in parallel without sharing files.
  • SQLiteManualClock stands still until you move it. Set it as SQLiteConfiguration.clock to test expiry without waiting.
  • SQLiteTestLatch holds a transaction open at a known point. The closure calls SQLiteTestLatch/blockingWait(timeout:); the test awaits SQLiteTestLatch/waitUntilEntered(timeout:), does what it needs while the transaction is open, then calls SQLiteTestLatch/open(). Both waits are bounded, so a mistake fails the test instead of hanging it.
let latch = SQLiteTestLatch()
let holder = Task {
    try await database.write { _ throws(SQLiteError) in latch.blockingWait() }
}
#expect(await latch.waitUntilEntered())
// The write lock is held here.
latch.open()
try await holder.value
  • SQLiteCrashImage/capture(databasePath:to:dropSharedMemory:) copies a database's files as a crash or power loss would leave them, so you can open the copy and check what survived.