File size: 3,732 Bytes
109cc95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"

import { useSnapshot } from "./comparison-fixture"

// Comparison and view queries share one DuckDB connection, so every reader
// must wait its turn on the one queue. This drives them concurrently through
// a connection that counts overlapping readers.

const ROUTE_ID = "openai%2Fgpt-5.5"
const EVAL_IDS = ["llm-stats%2Fwmdp", "benchpress%2Fwmdp", "wmdp"]

const tracking = vi.hoisted(() => ({ inFlight: 0, maxInFlight: 0, failSql: null as string | null }))

let restore: () => void

beforeAll(async () => {
  restore = useSnapshot()
  vi.resetModules()
  const duckdb = await import("@/lib/duckdb")
  const realGetConnection = duckdb.getConnection
  vi.doMock("@/lib/duckdb", async () => ({
    ...duckdb,
    getConnection: async () => {
      const connection = await realGetConnection()
      return new Proxy(connection, {
        get(target, prop, receiver) {
          if (prop === "runAndRead" || prop === "runAndReadAll") {
            return async (sql: string, ...rest: unknown[]) => {
              tracking.inFlight += 1
              tracking.maxInFlight = Math.max(tracking.maxInFlight, tracking.inFlight)
              try {
                await new Promise((resolve) => setTimeout(resolve, 2))
                if (tracking.failSql && String(sql).includes(tracking.failSql)) {
                  tracking.failSql = null
                  throw new Error("injected query failure")
                }
                return await (target as never as Record<string, (...a: unknown[]) => unknown>)[prop as string](
                  sql,
                  ...rest,
                )
              } finally {
                tracking.inFlight -= 1
              }
            }
          }
          const value = Reflect.get(target, prop, receiver)
          return typeof value === "function" ? value.bind(target) : value
        },
      })
    },
  }))
})

afterAll(() => {
  vi.doUnmock("@/lib/duckdb")
  vi.resetModules()
  restore?.()
})

async function runAll() {
  const table = await import("@/lib/comparison-table")
  const backend = await import("@/lib/data-backend")
  return Promise.all([
    table.cleanerInput(),
    table.sliceForModel(ROUTE_ID),
    table.sliceForEvals(EVAL_IDS),
    backend.getModelCardsLite(),
    backend.getEvalListLiteData(),
  ])
}

describe("shared connection queue", () => {
  it("runs comparison and view queries one at a time with stable results", async () => {
    const table = await import("@/lib/comparison-table")
    const backend = await import("@/lib/data-backend")
    const baseline = [
      await table.cleanerInput(),
      await table.sliceForModel(ROUTE_ID),
      await table.sliceForEvals(EVAL_IDS),
      await backend.getModelCardsLite(),
      await backend.getEvalListLiteData(),
    ]
    expect(baseline[1]).not.toBeNull()

    tracking.maxInFlight = 0
    const rounds = await Promise.all([runAll(), runAll(), runAll()])
    for (const round of rounds) expect(round).toStrictEqual(baseline)
    expect(tracking.maxInFlight).toBe(1)
  })

  it("keeps serving after a failed query", async () => {
    const table = await import("@/lib/comparison-table")
    const duckdb = await import("@/lib/duckdb")
    const expected = await table.sliceForEvals(EVAL_IDS)

    tracking.failSql = "FROM comparison_scores"
    const failing = table.sliceForModel(ROUTE_ID)
    const rejected = duckdb.serialized(() => Promise.reject(new Error("rejected task")))
    const next = table.sliceForEvals(EVAL_IDS)

    await expect(failing).rejects.toThrow("injected query failure")
    await expect(rejected).rejects.toThrow("rejected task")
    expect(await next).toStrictEqual(expected)
  })
})