|
| 1 | +import { createClient } from "@clickhouse/client"; |
| 2 | +import { ClientRequest, createServer } from "node:http"; |
| 3 | +import { AddressInfo } from "node:net"; |
| 4 | +import { Readable } from "node:stream"; |
| 5 | +import { describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +describe("ClickHouse streamed inserts", () => { |
| 8 | + it.each(["JSONCompactEachRow", "JSONEachRow"] as const)( |
| 9 | + "sends %s as UTF-8 buffers without stalling or changing rows", |
| 10 | + async format => { |
| 11 | + // Large enough to cross Node's HTTP high-water mark repeatedly. Non-ASCII |
| 12 | + // strings trigger nodejs/node#66781; ASCII-only payloads miss the regression. |
| 13 | + const rows = Array.from({ length: 4000 }, (_, i) => { |
| 14 | + const message = `Accrued payroll — café 中文 😀 ${i} ` + "ع".repeat(100); |
| 15 | + return format === "JSONCompactEachRow" ? [i, message] : { i, message }; |
| 16 | + }); |
| 17 | + const expected = Buffer.from(rows.map(row => JSON.stringify(row) + "\n").join("")); |
| 18 | + const chunks: Buffer[] = []; |
| 19 | + const server = createServer((req, res) => { |
| 20 | + req.on("data", chunk => chunks.push(chunk)); |
| 21 | + req.on("end", () => { |
| 22 | + res.setHeader("X-ClickHouse-Query-Id", "buffer-regression"); |
| 23 | + res.end(); |
| 24 | + }); |
| 25 | + }); |
| 26 | + await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve)); |
| 27 | + const client = createClient({ |
| 28 | + url: `http://127.0.0.1:${(server.address() as AddressInfo).port}`, |
| 29 | + request_timeout: 2000, |
| 30 | + }); |
| 31 | + // Observe the real SDK-to-HTTP boundary, not the received socket data: |
| 32 | + // Node always delivers received bytes as Buffers, even for string writes. |
| 33 | + const writes = vi.spyOn(ClientRequest.prototype, "write"); |
| 34 | + try { |
| 35 | + const result = await client.insert({ |
| 36 | + table: "events_log", |
| 37 | + format, |
| 38 | + values: Readable.from(rows), |
| 39 | + }); |
| 40 | + expect(result.executed).toBe(true); |
| 41 | + expect(Buffer.concat(chunks).equals(expected)).toBe(true); |
| 42 | + expect(writes.mock.calls.length).toBeGreaterThan(0); |
| 43 | + expect(writes.mock.calls.every(([chunk]) => Buffer.isBuffer(chunk))).toBe(true); |
| 44 | + } finally { |
| 45 | + writes.mockRestore(); |
| 46 | + await client.close(); |
| 47 | + server.closeAllConnections(); |
| 48 | + await new Promise<void>((resolve, reject) => server.close(err => (err ? reject(err) : resolve()))); |
| 49 | + } |
| 50 | + }, |
| 51 | + 10000 |
| 52 | + ); |
| 53 | +}); |
0 commit comments