-
-
Notifications
You must be signed in to change notification settings - Fork 652
Add state property to BaseConnection to match original mysql package #3885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add state property to BaseConnection to match original mysql package #3885
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3885 +/- ##
==========================================
+ Coverage 89.78% 90.51% +0.73%
==========================================
Files 86 86
Lines 13607 13614 +7
Branches 1607 1635 +28
==========================================
+ Hits 12217 12323 +106
+ Misses 1390 1291 -99
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A tip would be to use the sleep method instead of setting the setTimeout multiple times with a Promise, for example:
import { sleep } from 'poku';
// ...
await sleep(20);
// ...| const raw = connection.connection; | ||
| // use changeUser to induce auth error (assuming user 'nope' doesn't exist) | ||
| let gotErr = null; | ||
| assert.equal(raw.state, "connected"); | ||
| raw.on('error', (e) => { gotErr = e; }); | ||
| try { | ||
| await connection.changeUser({ user: 'nope', password: 'bad' }); | ||
| } catch (err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const raw = connection.connection; | |
| // use changeUser to induce auth error (assuming user 'nope' doesn't exist) | |
| let gotErr = null; | |
| assert.equal(raw.state, "connected"); | |
| raw.on('error', (e) => { gotErr = e; }); | |
| try { | |
| await connection.changeUser({ user: 'nope', password: 'bad' }); | |
| } catch (err) { | |
| const raw = connection.connection; | |
| let gotErr = null; | |
| assert.equal(raw.state, "connected"); | |
| raw.on('error', (e) => { gotErr = e; }); | |
| try { | |
| await connection.changeUser({ user: 'NON_EXISTENT_USER', password: 'BAD_PASSWORD' }); | |
| } catch (err) { |
| assert.equal(badConnection.connection.state, 'protocol_error'); | ||
| } finally { | ||
| try { | ||
| await bad.end(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| await bad.end(); | |
| await badConnection.end(); |
| }); | ||
|
|
||
| await it('state is protocol_error if creds are wrong.', async () => { | ||
| const badConnection = common.createConnection({ password: "WR0NG", user: 'wrong' }).promise(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const badConnection = common.createConnection({ password: "WR0NG", user: 'wrong' }).promise(); | |
| const badConnection = common.createConnection({ password: "WR0NG_PASSWORD", user: 'WRONG_USER' }).promise(); |
|
I just realized I forgot to add the "authorized" state, so I will be adding that and also fixing the unit tests as per your recommendation :) |
This PR intends to address #3764 by introducing a
stateproperty on the connection object, aligning behavior with the original mysql package.The new property reflects the current connection status; it only includes the following three states: disconnected, connected, or protocol_error.
The state flow includes:
Default: 'disconnected'
Updated to 'connected' upon successful handshake.
Updated to 'disconnected' after .end() or .close() is called (note that failures can eventually call this method).
Updated to 'protocol_error' on fatal or unexpected protocol errors.
Tests include:
Connects successfully and sets state to connected.
Closes cleanly and sets state to disconnected.
Incorrect credentials result in state protocol_error.
Let me know what you think, and tell me if any changes should be made -- along if we need more tests as well :)