|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { assert, test, log } = require('poku'); |
| 4 | +const common = require('../../common.test.cjs'); |
| 5 | +const { Readable, Duplex } = require('stream'); |
| 6 | +const Net = require('node:net'); |
| 7 | +const driver = require('../../../index.js'); |
| 8 | +const { setTimeout } = require('node:timers/promises'); |
| 9 | + |
| 10 | +class BigInput extends Readable { |
| 11 | + count = 0; |
| 12 | + MAX_EXPECTED_ROWS = 100_000; |
| 13 | + onStart = null; |
| 14 | + |
| 15 | + _read() { |
| 16 | + if (this.onStart) { |
| 17 | + this.onStart(); |
| 18 | + this.onStart = null; |
| 19 | + } |
| 20 | + |
| 21 | + if (this.count < this.MAX_EXPECTED_ROWS) { |
| 22 | + this.count++; |
| 23 | + const row = `${this.count}-${Math.random()}\n`; |
| 24 | + this.push(row); |
| 25 | + } else { |
| 26 | + this.push(null); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +test('load data infile backpressure on local stream', async () => { |
| 32 | + const config = common.config; |
| 33 | + const netStream = Net.connect(config.port, config.host); |
| 34 | + netStream.setNoDelay(true); |
| 35 | + await new Promise((resolve, reject) => |
| 36 | + netStream.once('connect', resolve).once('error', reject) |
| 37 | + ); |
| 38 | + |
| 39 | + class NetworkInterceptor extends Duplex { |
| 40 | + simulateWriteBackpressure = false; |
| 41 | + |
| 42 | + constructor() { |
| 43 | + super({ writableHighWaterMark: 65536 }); |
| 44 | + netStream.on('data', (data) => { |
| 45 | + const continueReading = this.push(data); |
| 46 | + if (!continueReading) { |
| 47 | + netStream.pause(); |
| 48 | + } |
| 49 | + }); |
| 50 | + netStream.on('error', (err) => this.destroy(err)); |
| 51 | + } |
| 52 | + |
| 53 | + _read() { |
| 54 | + netStream.resume(); |
| 55 | + } |
| 56 | + |
| 57 | + _write(chunk, encoding, callback) { |
| 58 | + netStream.write(chunk, encoding, (err) => { |
| 59 | + if (err) { |
| 60 | + callback(err); |
| 61 | + } else if (!this.simulateWriteBackpressure) { |
| 62 | + callback(); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const interceptor = new NetworkInterceptor(); |
| 69 | + const connection = driver.createConnection({ |
| 70 | + ...config, |
| 71 | + multipleStatements: true, |
| 72 | + stream: interceptor, |
| 73 | + }); |
| 74 | + |
| 75 | + try { |
| 76 | + const bigInput = new BigInput(); |
| 77 | + bigInput.onStart = () => (interceptor.simulateWriteBackpressure = true); |
| 78 | + |
| 79 | + connection.query( |
| 80 | + { |
| 81 | + sql: ` |
| 82 | + set global local_infile = 1; |
| 83 | + create temporary table test_load_data_backpressure (id varchar(100)); |
| 84 | + load data local infile "_" replace into table test_load_data_backpressure; |
| 85 | + `, |
| 86 | + infileStreamFactory: () => bigInput, |
| 87 | + }, |
| 88 | + (err, result) => { |
| 89 | + if (err) throw err; |
| 90 | + log('Load complete', result); |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + await setTimeout(100); // allow time for backpressure to take effect |
| 95 | + |
| 96 | + assert.ok( |
| 97 | + bigInput.count < bigInput.MAX_EXPECTED_ROWS, |
| 98 | + `expected backpressure to stop infile stream at less than ${bigInput.MAX_EXPECTED_ROWS} rows (read ${bigInput.count} rows)` |
| 99 | + ); |
| 100 | + } finally { |
| 101 | + connection.close(); |
| 102 | + netStream.destroy(); |
| 103 | + } |
| 104 | +}); |
0 commit comments