|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | 2 | import { K8sPodCountSignalSource } from "./k8sPodCountSignalSource.js"; |
3 | | -import { createPodCountFetcher } from "../clients/kubernetes.js"; |
4 | | -import type { K8sApi } from "../clients/kubernetes.js"; |
| 3 | +import { podCountFromList, withTimeout } from "../clients/kubernetes.js"; |
5 | 4 |
|
6 | | -function apiReturning(list: unknown): K8sApi { |
7 | | - return { core: { listNamespacedPod: async () => list } } as unknown as K8sApi; |
8 | | -} |
9 | | - |
10 | | -describe("createPodCountFetcher", () => { |
11 | | - it("returns items.length when the list is not truncated", async () => { |
12 | | - const fetch = createPodCountFetcher( |
13 | | - apiReturning({ items: [{}], metadata: {} }), |
14 | | - "v4-runs", |
15 | | - 1000 |
16 | | - ); |
17 | | - expect(await fetch()).toBe(1); |
| 5 | +describe("podCountFromList", () => { |
| 6 | + it("returns items.length when the list is not truncated", () => { |
| 7 | + expect(podCountFromList({ items: [{}], metadata: {} })).toBe(1); |
18 | 8 | }); |
19 | 9 |
|
20 | | - it("returns zero for an empty namespace", async () => { |
21 | | - const fetch = createPodCountFetcher(apiReturning({ items: [], metadata: {} }), "v4-runs", 1000); |
22 | | - expect(await fetch()).toBe(0); |
| 10 | + it("returns zero for an empty namespace", () => { |
| 11 | + expect(podCountFromList({ items: [], metadata: {} })).toBe(0); |
23 | 12 | }); |
24 | 13 |
|
25 | | - it("adds remainingItemCount when the list is truncated", async () => { |
| 14 | + it("adds remainingItemCount when the list is truncated", () => { |
26 | 15 | const list = { items: [{}], metadata: { _continue: "tok", remainingItemCount: 24492 } }; |
27 | | - const fetch = createPodCountFetcher(apiReturning(list), "v4-runs", 1000); |
28 | | - expect(await fetch()).toBe(24493); |
| 16 | + expect(podCountFromList(list)).toBe(24493); |
29 | 17 | }); |
30 | 18 |
|
31 | | - it("throws when truncated but remainingItemCount is absent", async () => { |
| 19 | + it("throws when truncated but remainingItemCount is absent", () => { |
32 | 20 | const list = { items: [{}], metadata: { _continue: "tok" } }; |
33 | | - const fetch = createPodCountFetcher(apiReturning(list), "v4-runs", 1000); |
34 | | - await expect(fetch()).rejects.toThrow(/remainingItemCount/); |
| 21 | + expect(() => podCountFromList(list)).toThrow(/remainingItemCount/); |
35 | 22 | }); |
36 | 23 |
|
37 | | - it("throws when truncated but remainingItemCount is negative", async () => { |
| 24 | + it("throws when truncated but remainingItemCount is negative", () => { |
38 | 25 | const list = { items: [{}], metadata: { _continue: "tok", remainingItemCount: -1 } }; |
39 | | - const fetch = createPodCountFetcher(apiReturning(list), "v4-runs", 1000); |
40 | | - await expect(fetch()).rejects.toThrow(/remainingItemCount/); |
| 26 | + expect(() => podCountFromList(list)).toThrow(/remainingItemCount/); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("withTimeout", () => { |
| 31 | + it("rejects once the deadline passes", async () => { |
| 32 | + await expect(withTimeout(new Promise(() => {}), 10, "pod count list")).rejects.toThrow( |
| 33 | + /timed out/ |
| 34 | + ); |
41 | 35 | }); |
42 | 36 |
|
43 | | - it("rejects when the list hangs past the timeout", async () => { |
44 | | - const api = { core: { listNamespacedPod: () => new Promise(() => {}) } } as unknown as K8sApi; |
45 | | - await expect(createPodCountFetcher(api, "v4-runs", 10)()).rejects.toThrow(/timed out/); |
| 37 | + it("passes a value through when it settles first", async () => { |
| 38 | + await expect(withTimeout(Promise.resolve(7), 1000, "pod count list")).resolves.toBe(7); |
46 | 39 | }); |
47 | 40 | }); |
48 | 41 |
|
|
0 commit comments