|
1 | 1 | import { WebSocketConnectionPool } from "@src/client/wsConnectorPool"; |
| 2 | +import { RetryConfig } from "@src/client/wsConnector"; |
2 | 3 | import { WSConfig } from "@src/common/config"; |
3 | 4 | import { parse, WS_TMQ_ENDPOINT } from "@src/common/dsn"; |
4 | 5 | import { WsSql } from "@src/sql/wsSql"; |
5 | 6 | import { testPassword, testUsername } from "@test-helpers/utils"; |
| 7 | +import { w3cwebsocket } from "websocket"; |
6 | 8 |
|
7 | 9 | function resetPoolSingleton() { |
8 | 10 | const PoolClass = WebSocketConnectionPool as any; |
@@ -52,6 +54,66 @@ describe("WebSocketConnectionPool key generation", () => { |
52 | 54 | expect(keyA).not.toBe(keyB); |
53 | 55 | }); |
54 | 56 |
|
| 57 | + test("does not collide auth scope when credentials contain colon", () => { |
| 58 | + const pool = WebSocketConnectionPool.instance(); |
| 59 | + |
| 60 | + const dsnA = parse("ws://host1:6041/mydb"); |
| 61 | + dsnA.username = "a:b"; |
| 62 | + dsnA.password = "c"; |
| 63 | + |
| 64 | + const dsnB = parse("ws://host1:6041/mydb"); |
| 65 | + dsnB.username = "a"; |
| 66 | + dsnB.password = "b:c"; |
| 67 | + |
| 68 | + const keyA = (pool as any).getPoolKey(dsnA); |
| 69 | + const keyB = (pool as any).getPoolKey(dsnB); |
| 70 | + expect(keyA).not.toBe(keyB); |
| 71 | + }); |
| 72 | + |
| 73 | + test("does not split pool key by reconnect policy", () => { |
| 74 | + const pool = WebSocketConnectionPool.instance(); |
| 75 | + const lowRetryDsn = parse( |
| 76 | + "ws://root:taosdata@host1:6041/mydb?retries=1&retry_backoff_ms=10&retry_backoff_max_ms=20" |
| 77 | + ); |
| 78 | + const highRetryDsn = parse( |
| 79 | + "ws://root:taosdata@host1:6041/mydb?retries=60&retry_backoff_ms=100&retry_backoff_max_ms=500" |
| 80 | + ); |
| 81 | + |
| 82 | + const lowRetryKey = (pool as any).getPoolKey(lowRetryDsn); |
| 83 | + const highRetryKey = (pool as any).getPoolKey(highRetryDsn); |
| 84 | + |
| 85 | + expect(lowRetryKey).toBe(highRetryKey); |
| 86 | + }); |
| 87 | + |
| 88 | + test("updates connector retry policy when reusing pooled connector", async () => { |
| 89 | + const pool = WebSocketConnectionPool.instance(); |
| 90 | + const lowRetryDsn = parse( |
| 91 | + "ws://root:taosdata@host1:6041/mydb?retries=1&retry_backoff_ms=10&retry_backoff_max_ms=20" |
| 92 | + ); |
| 93 | + const highRetryDsn = parse( |
| 94 | + "ws://root:taosdata@host1:6041/mydb?retries=60&retry_backoff_ms=100&retry_backoff_max_ms=500" |
| 95 | + ); |
| 96 | + const poolKey = (pool as any).getPoolKey(lowRetryDsn); |
| 97 | + |
| 98 | + let retries = 1; |
| 99 | + const connector = { |
| 100 | + readyState: jest.fn(() => w3cwebsocket.OPEN), |
| 101 | + close: jest.fn(), |
| 102 | + refreshRetryConfig: jest.fn((dsn) => { |
| 103 | + retries = RetryConfig.fromDsn(dsn).retries; |
| 104 | + }), |
| 105 | + getReconnectRetries: jest.fn(() => retries), |
| 106 | + getPoolKey: jest.fn(() => poolKey), |
| 107 | + }; |
| 108 | + |
| 109 | + (pool as any).pool.set(poolKey, [connector]); |
| 110 | + const reused = await pool.getConnection(highRetryDsn, 3000); |
| 111 | + |
| 112 | + expect(reused).toBe(connector); |
| 113 | + expect(connector.refreshRetryConfig).toHaveBeenCalledWith(highRetryDsn); |
| 114 | + expect((reused as any).getReconnectRetries()).toBe(60); |
| 115 | + }); |
| 116 | + |
55 | 117 | test("includes endpoint-derived websocket path in the pool key scope", () => { |
56 | 118 | const pool = WebSocketConnectionPool.instance(); |
57 | 119 | const sqlDsn = parse("ws://root:taosdata@host1:6041/mydb"); |
|
0 commit comments