|
1 | 1 | import * as os from "os"; |
2 | 2 | import * as path from "path"; |
3 | | -import { getControlPath } from "./sshConnectionPool"; |
4 | | -import type { SSHRuntimeConfig } from "./SSHRuntime"; |
| 3 | +import { getControlPath, SSHConnectionPool, type SSHRuntimeConfig } from "./sshConnectionPool"; |
5 | 4 |
|
6 | 5 | describe("sshConnectionPool", () => { |
7 | 6 | describe("getControlPath", () => { |
@@ -134,3 +133,151 @@ describe("username isolation", () => { |
134 | 133 | expect(controlPath).toMatch(/mux-ssh-[a-f0-9]{12}$/); |
135 | 134 | }); |
136 | 135 | }); |
| 136 | + |
| 137 | +describe("SSHConnectionPool", () => { |
| 138 | + describe("health tracking", () => { |
| 139 | + test("getConnectionHealth returns undefined for unknown connection", () => { |
| 140 | + const pool = new SSHConnectionPool(); |
| 141 | + const config: SSHRuntimeConfig = { |
| 142 | + host: "unknown.example.com", |
| 143 | + srcBaseDir: "/work", |
| 144 | + }; |
| 145 | + |
| 146 | + expect(pool.getConnectionHealth(config)).toBeUndefined(); |
| 147 | + }); |
| 148 | + |
| 149 | + test("markHealthy sets connection to healthy state", () => { |
| 150 | + const pool = new SSHConnectionPool(); |
| 151 | + const config: SSHRuntimeConfig = { |
| 152 | + host: "test.example.com", |
| 153 | + srcBaseDir: "/work", |
| 154 | + }; |
| 155 | + |
| 156 | + pool.markHealthy(config); |
| 157 | + const health = pool.getConnectionHealth(config); |
| 158 | + |
| 159 | + expect(health).toBeDefined(); |
| 160 | + expect(health!.status).toBe("healthy"); |
| 161 | + expect(health!.consecutiveFailures).toBe(0); |
| 162 | + expect(health!.lastSuccess).toBeInstanceOf(Date); |
| 163 | + }); |
| 164 | + |
| 165 | + test("reportFailure puts connection into backoff", () => { |
| 166 | + const pool = new SSHConnectionPool(); |
| 167 | + const config: SSHRuntimeConfig = { |
| 168 | + host: "test.example.com", |
| 169 | + srcBaseDir: "/work", |
| 170 | + }; |
| 171 | + |
| 172 | + // Mark healthy first |
| 173 | + pool.markHealthy(config); |
| 174 | + expect(pool.getConnectionHealth(config)?.status).toBe("healthy"); |
| 175 | + |
| 176 | + // Report a failure |
| 177 | + pool.reportFailure(config, "Connection refused"); |
| 178 | + const health = pool.getConnectionHealth(config); |
| 179 | + |
| 180 | + expect(health?.status).toBe("unhealthy"); |
| 181 | + expect(health?.consecutiveFailures).toBe(1); |
| 182 | + expect(health?.lastError).toBe("Connection refused"); |
| 183 | + expect(health?.backoffUntil).toBeDefined(); |
| 184 | + }); |
| 185 | + |
| 186 | + test("resetBackoff clears backoff state after failed probe", async () => { |
| 187 | + const pool = new SSHConnectionPool(); |
| 188 | + const config: SSHRuntimeConfig = { |
| 189 | + host: "nonexistent.invalid.host.test", |
| 190 | + srcBaseDir: "/work", |
| 191 | + }; |
| 192 | + |
| 193 | + // Trigger a failure via acquireConnection (will fail to connect) |
| 194 | + await expect(pool.acquireConnection(config, 1000)).rejects.toThrow(); |
| 195 | + |
| 196 | + // Verify we're now in backoff |
| 197 | + const healthBefore = pool.getConnectionHealth(config); |
| 198 | + expect(healthBefore?.status).toBe("unhealthy"); |
| 199 | + expect(healthBefore?.backoffUntil).toBeDefined(); |
| 200 | + |
| 201 | + // Reset backoff |
| 202 | + pool.resetBackoff(config); |
| 203 | + const healthAfter = pool.getConnectionHealth(config); |
| 204 | + |
| 205 | + expect(healthAfter).toBeDefined(); |
| 206 | + expect(healthAfter!.status).toBe("unknown"); |
| 207 | + expect(healthAfter!.consecutiveFailures).toBe(0); |
| 208 | + expect(healthAfter!.backoffUntil).toBeUndefined(); |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + describe("acquireConnection", () => { |
| 213 | + test("returns immediately for known healthy connection", async () => { |
| 214 | + const pool = new SSHConnectionPool(); |
| 215 | + const config: SSHRuntimeConfig = { |
| 216 | + host: "test.example.com", |
| 217 | + srcBaseDir: "/work", |
| 218 | + }; |
| 219 | + |
| 220 | + // Mark as healthy first |
| 221 | + pool.markHealthy(config); |
| 222 | + |
| 223 | + // Should return immediately without probing |
| 224 | + const start = Date.now(); |
| 225 | + await pool.acquireConnection(config); |
| 226 | + const elapsed = Date.now() - start; |
| 227 | + |
| 228 | + // Should be nearly instant (< 50ms) |
| 229 | + expect(elapsed).toBeLessThan(50); |
| 230 | + }); |
| 231 | + |
| 232 | + test("throws immediately when in backoff", async () => { |
| 233 | + const pool = new SSHConnectionPool(); |
| 234 | + const config: SSHRuntimeConfig = { |
| 235 | + host: "nonexistent.invalid.host.test", |
| 236 | + srcBaseDir: "/work", |
| 237 | + }; |
| 238 | + |
| 239 | + // Trigger a failure to put connection in backoff |
| 240 | + await expect(pool.acquireConnection(config, 1000)).rejects.toThrow(); |
| 241 | + |
| 242 | + // Second call should throw immediately with backoff message |
| 243 | + await expect(pool.acquireConnection(config)).rejects.toThrow(/in backoff/); |
| 244 | + }); |
| 245 | + |
| 246 | + test("getControlPath returns deterministic path", () => { |
| 247 | + const pool = new SSHConnectionPool(); |
| 248 | + const config: SSHRuntimeConfig = { |
| 249 | + host: "test.example.com", |
| 250 | + srcBaseDir: "/work", |
| 251 | + }; |
| 252 | + |
| 253 | + const path1 = pool.getControlPath(config); |
| 254 | + const path2 = pool.getControlPath(config); |
| 255 | + |
| 256 | + expect(path1).toBe(path2); |
| 257 | + expect(path1).toBe(getControlPath(config)); |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
| 261 | + describe("singleflighting", () => { |
| 262 | + test("concurrent acquireConnection calls share same probe", async () => { |
| 263 | + const pool = new SSHConnectionPool(); |
| 264 | + const config: SSHRuntimeConfig = { |
| 265 | + host: "test.example.com", |
| 266 | + srcBaseDir: "/work", |
| 267 | + }; |
| 268 | + |
| 269 | + // Mark healthy to avoid actual probe |
| 270 | + pool.markHealthy(config); |
| 271 | + |
| 272 | + // Multiple concurrent calls should all succeed |
| 273 | + const results = await Promise.all([ |
| 274 | + pool.acquireConnection(config), |
| 275 | + pool.acquireConnection(config), |
| 276 | + pool.acquireConnection(config), |
| 277 | + ]); |
| 278 | + |
| 279 | + // All should resolve (no errors) |
| 280 | + expect(results).toHaveLength(3); |
| 281 | + }); |
| 282 | + }); |
| 283 | +}); |
0 commit comments