File size: 6,274 Bytes
086ee97 | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import {describe, expect, test} from "bun:test"
import {
SCHEMA_POLL_INTERVAL_MS,
SCHEMA_WAIT_TIMEOUT_MS,
waitForSchema,
type RequiredSchema,
type SchemaReader,
} from "./database-schema-wait"
const REQUIRED: RequiredSchema = {
collections: ["workspace_audit_counters"],
indexes: [{collection: "workspace_audit_events", key: {seq: 1}}],
}
/** A database whose state at each look (1st, 2nd, ...) is given by `stateAt`, and a clock that only sleeping moves. */
function fakeDatabase(
stateAt: (look: number) => {collections: string[]; indexes: Array<{key: Record<string, unknown>; unique?: boolean}>},
) {
let looks = 0
let clock = 0
const sleeps: number[] = []
const reader: SchemaReader = {
async collectionNames() {
looks += 1
return stateAt(looks).collections
},
async indexes() {
return stateAt(looks).indexes
},
}
const options = {
label: "ci-fresh",
sleep: async (ms: number) => {
sleeps.push(ms)
clock += ms
},
now: () => clock,
log: () => {},
}
return {reader, options, sleeps, looks: () => looks}
}
const BUILT = {
collections: ["workspace_audit_counters", "workspace_audit_events"],
indexes: [{key: {_id: 1}}, {key: {seq: 1}}],
}
const BUILDING = {collections: ["workspace_audit_counters", "workspace_audit_events"], indexes: [{key: {_id: 1}}]}
describe("waitForSchema", () => {
test("uses a two second interval and a two minute deadline", () => {
expect(SCHEMA_POLL_INTERVAL_MS).toBe(2_000)
expect(SCHEMA_WAIT_TIMEOUT_MS).toBe(120_000)
})
test("passes at once, without sleeping, when everything is already built", async () => {
const db = fakeDatabase(() => BUILT)
expect(await waitForSchema(db.reader, REQUIRED, db.options)).toEqual({waitedMs: 0})
expect(db.looks()).toBe(1)
expect(db.sleeps).toEqual([])
})
test("passes when an index that was missing is listed on the third look", async () => {
const db = fakeDatabase((look) => (look < 3 ? BUILDING : BUILT))
expect(await waitForSchema(db.reader, REQUIRED, db.options)).toEqual({waitedMs: 4_000})
expect(db.looks()).toBe(3)
expect(db.sleeps).toEqual([2_000, 2_000])
})
test("waits for a missing collection as well", async () => {
const db = fakeDatabase((look) =>
look < 2 ? {collections: ["workspace_audit_events"], indexes: BUILT.indexes} : BUILT,
)
await waitForSchema(db.reader, REQUIRED, db.options)
expect(db.looks()).toBe(2)
})
test("fails on a missing index only at the deadline, and says how long it waited", async () => {
const db = fakeDatabase(() => BUILDING)
const error = await waitForSchema(db.reader, REQUIRED, db.options).then(
() => null,
(thrown: Error) => thrown,
)
expect(error).toBeInstanceOf(Error)
expect(error!.message).toContain('workspace_audit_events has no index on {"seq":1}')
expect(error!.message).toContain("after waiting 120 s")
expect(error!.message).toContain("ci-fresh")
expect(error!.message).toContain("failed to create workspace collection indexes")
// A look at the start, then every two seconds up to and including the deadline.
expect(db.looks()).toBe(61)
expect(db.sleeps.reduce((sum, ms) => sum + ms, 0)).toBe(120_000)
expect(db.sleeps.every((ms) => ms === 2_000)).toBe(true)
})
test("reports every problem still missing at the deadline", async () => {
const db = fakeDatabase(() => ({collections: [], indexes: []}))
const error = await waitForSchema(db.reader, REQUIRED, {...db.options, timeoutMs: 6_000}).catch(
(thrown: Error) => thrown,
)
expect((error as Error).message).toContain("collection workspace_audit_counters does not exist")
expect((error as Error).message).toContain('workspace_audit_events has no index on {"seq":1}')
expect((error as Error).message).toContain("after waiting 6 s")
expect(db.looks()).toBe(4)
})
test("does not sleep past the deadline when the interval does not divide it", async () => {
const db = fakeDatabase(() => BUILDING)
await waitForSchema(db.reader, REQUIRED, {...db.options, intervalMs: 4_000, timeoutMs: 10_000}).catch(() => {})
expect(db.sleeps).toEqual([4_000, 4_000, 2_000])
expect(db.looks()).toBe(4)
})
test("fails on a unique index at once instead of waiting out the deadline", async () => {
const db = fakeDatabase(() => ({...BUILT, indexes: [{key: {seq: 1}, unique: true}]}))
const error = await waitForSchema(db.reader, REQUIRED, db.options).catch((thrown: Error) => thrown)
expect((error as Error).message).toContain('workspace_audit_events\'s index on {"seq":1} is unique')
expect((error as Error).message).toContain("stopped after 0 s")
expect(db.looks()).toBe(1)
expect(db.sleeps).toEqual([])
})
test("fails on a unique index found while waiting for another, without waiting further", async () => {
const db = fakeDatabase((look) => (look < 3 ? BUILDING : {...BUILT, indexes: [{key: {seq: 1}, unique: true}]}))
const error = await waitForSchema(db.reader, REQUIRED, db.options).catch((thrown: Error) => thrown)
expect((error as Error).message).toContain("is unique")
expect((error as Error).message).toContain("stopped after 4 s")
expect(db.looks()).toBe(3)
})
test("matches an index on exactly the required key", async () => {
const db = fakeDatabase(() => ({...BUILT, indexes: [{key: {seq: 1, workspaceId: 1}}, {key: {seq: -1}}]}))
const error = await waitForSchema(db.reader, REQUIRED, {...db.options, timeoutMs: 2_000}).catch(
(thrown: Error) => thrown,
)
expect((error as Error).message).toContain('has no index on {"seq":1}')
})
test("logs once that it is waiting, and once that it is done", async () => {
const messages: string[] = []
const db = fakeDatabase((look) => (look < 3 ? BUILDING : BUILT))
await waitForSchema(db.reader, REQUIRED, {...db.options, log: (message) => messages.push(message)})
expect(messages).toHaveLength(2)
expect(messages[0]).toContain("not fully built yet")
expect(messages[1]).toContain("after 4 s")
})
})
|