-
-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathtest-stream-errors.test.mts
More file actions
103 lines (95 loc) · 3.61 KB
/
test-stream-errors.test.mts
File metadata and controls
103 lines (95 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This file was modified by Oracle on January 21, 2021.
// The connection with the mock server needs to happen in the same host where
// the tests are running in order to avoid connecting a potential MySQL server
// instance running in the host identified by the MYSQL_HOST environment
// variable.
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
import type { Connection } from '../../../index.js';
import { describe, it, skip, strict } from 'poku';
import { createConnection, createServer } from '../../common.test.mjs';
type TestError = Error & { code?: string; fatal?: boolean };
if (typeof Deno !== 'undefined') skip('Deno: process is not terminated');
await describe('Stream Errors', async () => {
await it('should handle stream errors correctly', async () => {
let clientConnection: Connection | undefined;
const err: Error & { code?: string } = new Error(
'This socket has been ended by the other party'
);
err.code = 'EPIPE';
let receivedError1: TestError | undefined;
let receivedError2: TestError | undefined;
let receivedError3: TestError | undefined;
const query = 'SELECT 1';
await new Promise<void>((resolve) => {
const server = createServer(
() => {
clientConnection = createConnection({
// The mock server is running on the same host machine.
// We need to explicitly define the host to avoid connecting to a potential
// different host provided via MYSQL_HOST that identifies a real MySQL
// server instance.
host: 'localhost',
// @ts-expect-error: internal access
port: server._port,
ssl: false,
});
clientConnection?.query(query, (_err) => {
if (_err && _err.code === 'HANDSHAKE_NO_SSL_SUPPORT') {
clientConnection?.end();
}
receivedError1 = _err ?? undefined;
});
clientConnection?.query(
'second query, should not be executed',
() => {
receivedError2 = err;
clientConnection?.query(
'trying to enqueue command to a connection which is already in error state',
(_err1) => {
receivedError3 = _err1 ?? undefined;
resolve();
}
);
}
);
},
(conn) => {
conn.on('query', () => {
// @ts-expect-error: TODO: implement typings
conn.writeColumns([
{
catalog: 'def',
schema: '',
table: '',
orgTable: '',
name: '1',
orgName: '',
characterSet: 63,
columnLength: 1,
columnType: 8,
flags: 129,
decimals: 0,
},
]);
// emulate stream error here
// @ts-expect-error: TODO: implement typings
clientConnection?.stream.emit('error', err);
// @ts-expect-error: TODO: implement typings
clientConnection?.stream.end();
// @ts-expect-error: TODO: implement typings
server.close();
});
}
);
});
strict.equal(receivedError1?.fatal, true);
strict.equal(receivedError1?.code, err.code);
strict.equal(receivedError2?.fatal, true);
strict.equal(receivedError2?.code, err.code);
strict.equal(receivedError3?.fatal, true);
strict.equal(
receivedError3?.message,
"Can't add new command when connection is in closed state"
);
});
});