|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { Registry } from "prom-client"; |
| 3 | +import { RunQueueConsumer } from "./queueConsumer.js"; |
| 4 | +import { ConsumerPoolMetrics } from "./consumerPoolMetrics.js"; |
| 5 | +import type { SupervisorHttpClient } from "./http.js"; |
| 6 | +import type { WorkerApiDequeueResponseBody } from "./schemas.js"; |
| 7 | + |
| 8 | +// Mock only the logger (same approach as consumerPool.test.ts) |
| 9 | +vi.mock("../../utils/structuredLogger.js"); |
| 10 | + |
| 11 | +function makeClient(dequeueImpl: () => Promise<unknown>): SupervisorHttpClient { |
| 12 | + return { dequeue: vi.fn(dequeueImpl) } as unknown as SupervisorHttpClient; |
| 13 | +} |
| 14 | + |
| 15 | +describe("RunQueueConsumer dequeue latency metric", () => { |
| 16 | + let register: Registry; |
| 17 | + let metrics: ConsumerPoolMetrics; |
| 18 | + let consumer: RunQueueConsumer | undefined; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + // Fake timers so the trailing scheduleNextDequeue() never fires during the test. |
| 23 | + vi.useFakeTimers(); |
| 24 | + register = new Registry(); |
| 25 | + metrics = new ConsumerPoolMetrics({ register }); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + consumer?.stop(); |
| 30 | + vi.clearAllTimers(); |
| 31 | + vi.useRealTimers(); |
| 32 | + }); |
| 33 | + |
| 34 | + /** |
| 35 | + * Runs exactly one dequeue iteration and awaits it. We set `isEnabled` |
| 36 | + * directly and invoke the private `dequeue()` rather than `start()`, so no |
| 37 | + * timer-driven loop runs - the metric is recorded before scheduleNextDequeue(). |
| 38 | + */ |
| 39 | + async function runOneDequeue(opts: { |
| 40 | + dequeueImpl: () => Promise<unknown>; |
| 41 | + withMetrics?: boolean; |
| 42 | + }) { |
| 43 | + consumer = new RunQueueConsumer({ |
| 44 | + client: makeClient(opts.dequeueImpl), |
| 45 | + intervalMs: 600_000, |
| 46 | + idleIntervalMs: 600_000, |
| 47 | + onDequeue: async () => {}, |
| 48 | + ...(opts.withMetrics === false ? {} : { metrics }), |
| 49 | + }); |
| 50 | + |
| 51 | + (consumer as unknown as { isEnabled: boolean }).isEnabled = true; |
| 52 | + await (consumer as unknown as { dequeue(): Promise<void> }).dequeue(); |
| 53 | + } |
| 54 | + |
| 55 | + it('records outcome="empty" for a successful empty dequeue', async () => { |
| 56 | + await runOneDequeue({ dequeueImpl: async () => ({ success: true, data: [] }) }); |
| 57 | + |
| 58 | + expect(await register.metrics()).toContain( |
| 59 | + 'queue_consumer_pool_dequeue_duration_seconds_count{outcome="empty"} 1' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('records outcome="success" once per round-trip, regardless of message count', async () => { |
| 64 | + const messages = [{ run: {} }, { run: {} }] as unknown as WorkerApiDequeueResponseBody; |
| 65 | + await runOneDequeue({ dequeueImpl: async () => ({ success: true, data: messages }) }); |
| 66 | + |
| 67 | + const text = await register.metrics(); |
| 68 | + // One observation for the whole batch, not one per message. |
| 69 | + expect(text).toContain('queue_consumer_pool_dequeue_duration_seconds_count{outcome="success"} 1'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('records outcome="error" when the response is unsuccessful', async () => { |
| 73 | + await runOneDequeue({ |
| 74 | + dequeueImpl: async () => ({ success: false, error: new Error("boom") }), |
| 75 | + }); |
| 76 | + |
| 77 | + expect(await register.metrics()).toContain( |
| 78 | + 'queue_consumer_pool_dequeue_duration_seconds_count{outcome="error"} 1' |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + // Defensive path: wrapZodFetch traps all errors today, so the real client |
| 83 | + // never throws - this guards against a future client that does. |
| 84 | + it('records outcome="error" when the dequeue call throws', async () => { |
| 85 | + await runOneDequeue({ |
| 86 | + dequeueImpl: async () => { |
| 87 | + throw new Error("network down"); |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(await register.metrics()).toContain( |
| 92 | + 'queue_consumer_pool_dequeue_duration_seconds_count{outcome="error"} 1' |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it("is a no-op (does not throw) when no metrics instance is provided", async () => { |
| 97 | + await expect( |
| 98 | + runOneDequeue({ dequeueImpl: async () => ({ success: true, data: [] }), withMetrics: false }) |
| 99 | + ).resolves.not.toThrow(); |
| 100 | + |
| 101 | + // Histogram has no observations - the labelled count line should be absent. |
| 102 | + expect(await register.metrics()).not.toContain("queue_consumer_pool_dequeue_duration_seconds_count"); |
| 103 | + }); |
| 104 | +}); |
0 commit comments