-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datasource-sql): fix worker crash on authentication testing (#736)
- Loading branch information
1 parent
c81cf61
commit e81f101
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/datasource-sql/src/connection/connection-tester.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Sequelize } from 'sequelize'; | ||
|
||
import { DatabaseConnectError } from './errors'; | ||
|
||
export default async function testConnectionWithTimeOut( | ||
sequelize: Sequelize, | ||
databaseUri: string, | ||
timeout = 10000, | ||
) { | ||
// Test connection. If this doesn't resolve after 10s, throw a timeout error | ||
const timeoutPromise = new Promise(resolve => { | ||
setTimeout(resolve, timeout); | ||
}).then(() => | ||
Promise.reject(new DatabaseConnectError('Connection to database timed out', databaseUri)), | ||
); | ||
|
||
await Promise.race([sequelize.authenticate(), timeoutPromise]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/datasource-sql/test/connection/connection-tester.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Sequelize } from 'sequelize'; | ||
|
||
import testConnectionWithTimeOut from '../../src/connection/connection-tester'; | ||
import { DatabaseConnectError } from '../../src/connection/errors'; | ||
|
||
describe('testConnectionWithTimeOut', () => { | ||
it('should time out', async () => { | ||
jest.useFakeTimers(); | ||
|
||
const sequelize = { | ||
authenticate: () => | ||
new Promise(resolve => { | ||
setTimeout(resolve, 4000); | ||
}), | ||
}; | ||
|
||
const promise = testConnectionWithTimeOut(sequelize as unknown as Sequelize, 'a db URI', 3000); | ||
|
||
jest.advanceTimersByTime(3000); | ||
|
||
await expect(promise).rejects.toThrow(DatabaseConnectError); | ||
}); | ||
|
||
it('should pass before 3s', async () => { | ||
jest.useFakeTimers(); | ||
|
||
const sequelize = { | ||
authenticate: () => | ||
new Promise(resolve => { | ||
setTimeout(resolve, 2000); | ||
}), | ||
}; | ||
|
||
const promise = testConnectionWithTimeOut(sequelize as unknown as Sequelize, 'a db URI', 3000); | ||
|
||
jest.advanceTimersByTime(2000); | ||
|
||
await expect(promise).resolves.not.toThrow(); | ||
}); | ||
}); |