|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const process = require('node:process'); |
4 |
| -const { test, skip } = require('poku'); |
| 3 | +const { assert, describe, test } = require('poku'); |
5 | 4 | const common = require('../../common.test.cjs');
|
6 | 5 |
|
7 |
| -if (process.env.MYSQL_USE_TLS === '1') skip('Skipping for SSL=1'); |
8 |
| - |
9 |
| -test('Ensure stream ends in case of error', async () => { |
| 6 | +describe(async () => { |
10 | 7 | const connection = common.createConnection();
|
11 | 8 |
|
12 |
| - connection.query( |
13 |
| - [ |
14 |
| - 'CREATE TEMPORARY TABLE `items` (', |
15 |
| - '`id` int(11) NOT NULL AUTO_INCREMENT,', |
16 |
| - '`text` varchar(255) DEFAULT NULL,', |
17 |
| - 'PRIMARY KEY (`id`)', |
18 |
| - ') ENGINE=InnoDB DEFAULT CHARSET=utf8', |
19 |
| - ].join('\n'), |
20 |
| - (err) => { |
21 |
| - if (err) { |
22 |
| - throw err; |
| 9 | + await test('Ensure stream ends in case of error', async () => { |
| 10 | + connection.query( |
| 11 | + [ |
| 12 | + 'CREATE TEMPORARY TABLE `items` (', |
| 13 | + '`id` int(11) NOT NULL AUTO_INCREMENT,', |
| 14 | + '`text` varchar(255) DEFAULT NULL,', |
| 15 | + 'PRIMARY KEY (`id`)', |
| 16 | + ') ENGINE=InnoDB DEFAULT CHARSET=utf8', |
| 17 | + ].join('\n'), |
| 18 | + (err) => { |
| 19 | + if (err) { |
| 20 | + throw err; |
| 21 | + } |
23 | 22 | }
|
| 23 | + ); |
| 24 | + |
| 25 | + for (let i = 0; i < 100; i++) { |
| 26 | + connection.execute( |
| 27 | + 'INSERT INTO items(text) VALUES(?)', |
| 28 | + ['test'], |
| 29 | + (err) => { |
| 30 | + if (err) { |
| 31 | + throw err; |
| 32 | + } |
| 33 | + } |
| 34 | + ); |
24 | 35 | }
|
25 |
| - ); |
26 | 36 |
|
27 |
| - for (let i = 0; i < 100; i++) { |
28 |
| - connection.execute('INSERT INTO items(text) VALUES(?)', ['test'], (err) => { |
29 |
| - if (err) { |
30 |
| - throw err; |
31 |
| - } |
| 37 | + const rows = connection.query('SELECT * FROM items').stream(); |
| 38 | + |
| 39 | + // eslint-disable-next-line no-unused-vars |
| 40 | + for await (const _ of rows) break; // forces return () -> destroy() |
| 41 | + }); |
| 42 | + |
| 43 | + await test('end: Ensure stream emits error then close on server-side query error', async () => { |
| 44 | + let uncaughtExceptionError; |
| 45 | + |
| 46 | + const stream = connection |
| 47 | + .query('SELECT invalid_column FROM invalid_table') |
| 48 | + .stream(); |
| 49 | + |
| 50 | + stream.on('error', (error) => { |
| 51 | + uncaughtExceptionError = error; |
| 52 | + }); |
| 53 | + |
| 54 | + await new Promise((resolve) => stream.on('end', resolve)); |
| 55 | + |
| 56 | + assert( |
| 57 | + uncaughtExceptionError instanceof Error, |
| 58 | + 'Expected an uncaught exception error' |
| 59 | + ); |
| 60 | + |
| 61 | + assert.equal( |
| 62 | + uncaughtExceptionError.message, |
| 63 | + "Table 'test.invalid_table' doesn't exist" |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + await test('close: Ensure stream emits error then close on server-side query error', async () => { |
| 68 | + let uncaughtExceptionError; |
| 69 | + |
| 70 | + const stream = connection |
| 71 | + .query('SELECT invalid_column FROM invalid_table') |
| 72 | + .stream(); |
| 73 | + |
| 74 | + stream.on('error', (error) => { |
| 75 | + uncaughtExceptionError = error; |
32 | 76 | });
|
33 |
| - } |
34 | 77 |
|
35 |
| - const rows = connection.query('SELECT * FROM items').stream(); |
| 78 | + await new Promise((resolve) => stream.on('close', resolve)); |
36 | 79 |
|
37 |
| - // eslint-disable-next-line no-unused-vars |
38 |
| - for await (const _ of rows) break; |
| 80 | + assert( |
| 81 | + uncaughtExceptionError instanceof Error, |
| 82 | + 'Expected an uncaught exception error' |
| 83 | + ); |
39 | 84 |
|
40 |
| - setTimeout(() => { |
41 |
| - throw new Error('Connection remains open after stream error'); |
42 |
| - }, 1000).unref(); |
| 85 | + assert.equal( |
| 86 | + uncaughtExceptionError.message, |
| 87 | + "Table 'test.invalid_table' doesn't exist" |
| 88 | + ); |
| 89 | + }); |
43 | 90 |
|
44 | 91 | connection.end();
|
45 | 92 | });
|
0 commit comments