|
4 | 4 | import assert from "node:assert"; |
5 | 5 | import { createServer, type AddressInfo } from "node:net"; |
6 | 6 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { retryUntil, retryUntilAsync } from "../src/lib/core/retry.js"; |
| 8 | + |
7 | 9 | import { |
8 | 10 | buildLoopbackProbeEnv, |
9 | 11 | sleepMs, |
@@ -47,6 +49,167 @@ describe("wait utility", () => { |
47 | 49 | assert.ok(duration < 50, `duration ${duration}ms > 50ms`); |
48 | 50 | }); |
49 | 51 |
|
| 52 | + it("retryUntil returns the first accepted result without sleeping (#9218)", () => { |
| 53 | + const sleep = vi.fn(); |
| 54 | + const operation = vi.fn(() => "ready"); |
| 55 | + |
| 56 | + const result = retryUntil(operation, { |
| 57 | + accept: (value) => value === "ready", |
| 58 | + retryDelaysMs: [10, 20], |
| 59 | + sleep, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(result).toBe("ready"); |
| 63 | + expect(operation).toHaveBeenCalledOnce(); |
| 64 | + expect(operation).toHaveBeenCalledWith(1); |
| 65 | + expect(sleep).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("retryUntil applies exact delays before a later accepted result (#9218)", () => { |
| 69 | + const sleep = vi.fn(); |
| 70 | + |
| 71 | + const result = retryUntil((attempt) => (attempt === 3 ? "ready" : "starting"), { |
| 72 | + accept: (value) => value === "ready", |
| 73 | + retryDelaysMs: [10, 20, 30], |
| 74 | + sleep, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(result).toBe("ready"); |
| 78 | + expect(sleep.mock.calls).toEqual([[10], [20]]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("retryUntil returns the final unaccepted result after exhaustion (#9218)", () => { |
| 82 | + const sleep = vi.fn(); |
| 83 | + |
| 84 | + const result = retryUntil((attempt) => `failure-${attempt}`, { |
| 85 | + accept: () => false, |
| 86 | + retryDelaysMs: [10, 20], |
| 87 | + sleep, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(result).toBe("failure-3"); |
| 91 | + expect(sleep.mock.calls).toEqual([[10], [20]]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("retryUntil runs once with an empty retry schedule (#9218)", () => { |
| 95 | + const operation = vi.fn(() => "failure"); |
| 96 | + |
| 97 | + const result = retryUntil(operation, { |
| 98 | + accept: () => false, |
| 99 | + retryDelaysMs: [], |
| 100 | + |
| 101 | + sleep: vi.fn(), |
| 102 | + }); |
| 103 | + |
| 104 | + expect(result).toBe("failure"); |
| 105 | + expect(operation).toHaveBeenCalledOnce(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("retryUntil propagates an operation error without sleeping (#9218)", () => { |
| 109 | + const sleep = vi.fn(); |
| 110 | + const error = new Error("operation failed"); |
| 111 | + |
| 112 | + expect(() => |
| 113 | + retryUntil( |
| 114 | + () => { |
| 115 | + throw error; |
| 116 | + }, |
| 117 | + { accept: () => false, retryDelaysMs: [10], sleep }, |
| 118 | + ), |
| 119 | + ).toThrow(error); |
| 120 | + expect(sleep).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("retryUntil propagates a sleep error without another attempt (#9218)", () => { |
| 124 | + const operation = vi.fn(() => "starting"); |
| 125 | + const error = new Error("sleep failed"); |
| 126 | + |
| 127 | + expect(() => |
| 128 | + retryUntil(operation, { |
| 129 | + accept: () => false, |
| 130 | + retryDelaysMs: [10], |
| 131 | + sleep: () => { |
| 132 | + throw error; |
| 133 | + }, |
| 134 | + }), |
| 135 | + ).toThrow(error); |
| 136 | + expect(operation).toHaveBeenCalledOnce(); |
| 137 | + }); |
| 138 | + |
| 139 | + it("retryUntilAsync applies exact delays before a later accepted result (#9218)", async () => { |
| 140 | + const sleep = vi.fn(async () => {}); |
| 141 | + |
| 142 | + const result = await retryUntilAsync( |
| 143 | + async (attempt) => (attempt === 3 ? "ready" : "starting"), |
| 144 | + { |
| 145 | + accept: (value) => value === "ready", |
| 146 | + retryDelaysMs: [10, 20, 30], |
| 147 | + sleep, |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + expect(result).toBe("ready"); |
| 152 | + expect(sleep.mock.calls).toEqual([[10], [20]]); |
| 153 | + }); |
| 154 | + |
| 155 | + it("retryUntilAsync runs once with an empty retry schedule (#9218)", async () => { |
| 156 | + const operation = vi.fn(async () => "failure"); |
| 157 | + |
| 158 | + const result = await retryUntilAsync(operation, { |
| 159 | + accept: () => false, |
| 160 | + retryDelaysMs: [], |
| 161 | + |
| 162 | + sleep: vi.fn(async () => {}), |
| 163 | + }); |
| 164 | + |
| 165 | + expect(result).toBe("failure"); |
| 166 | + expect(operation).toHaveBeenCalledOnce(); |
| 167 | + }); |
| 168 | + |
| 169 | + it("retryUntilAsync returns the final unaccepted result after exhaustion (#9218)", async () => { |
| 170 | + const sleep = vi.fn(async () => {}); |
| 171 | + |
| 172 | + const result = await retryUntilAsync(async (attempt) => `failure-${attempt}`, { |
| 173 | + accept: () => false, |
| 174 | + retryDelaysMs: [10, 20], |
| 175 | + sleep, |
| 176 | + }); |
| 177 | + |
| 178 | + expect(result).toBe("failure-3"); |
| 179 | + expect(sleep.mock.calls).toEqual([[10], [20]]); |
| 180 | + }); |
| 181 | + |
| 182 | + it("retryUntilAsync propagates an operation error without sleeping (#9218)", async () => { |
| 183 | + const sleep = vi.fn(async () => {}); |
| 184 | + const error = new Error("operation failed"); |
| 185 | + |
| 186 | + await expect( |
| 187 | + retryUntilAsync( |
| 188 | + async () => { |
| 189 | + throw error; |
| 190 | + }, |
| 191 | + { accept: () => false, retryDelaysMs: [10], sleep }, |
| 192 | + ), |
| 193 | + ).rejects.toBe(error); |
| 194 | + expect(sleep).not.toHaveBeenCalled(); |
| 195 | + }); |
| 196 | + |
| 197 | + it("retryUntilAsync propagates a sleep error without another attempt (#9218)", async () => { |
| 198 | + const operation = vi.fn(async () => "starting"); |
| 199 | + const error = new Error("sleep failed"); |
| 200 | + |
| 201 | + await expect( |
| 202 | + retryUntilAsync(operation, { |
| 203 | + accept: () => false, |
| 204 | + retryDelaysMs: [10], |
| 205 | + sleep: async () => { |
| 206 | + throw error; |
| 207 | + }, |
| 208 | + }), |
| 209 | + ).rejects.toBe(error); |
| 210 | + expect(operation).toHaveBeenCalledOnce(); |
| 211 | + }); |
| 212 | + |
50 | 213 | it("waitUntil returns immediately when the condition is already true", () => { |
51 | 214 | const sleeps: number[] = []; |
52 | 215 | let attempts = 0; |
|
0 commit comments