Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 0eb93af6
authored
Dec 03, 2020
by
papb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: improve 'running queries' detection
(backport of #12885)
1 parent
e4144677
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
8 deletions
test/integration/support.js
test/integration/support.js
View file @
0eb93af
'use strict'
;
'use strict'
;
// Store local references to `setTimeout` and `clearTimeout` asap, so that we can use them within `p-timeout`,
// avoiding to be affected unintentionally by `sinon.useFakeTimers()` called by the tests themselves.
const
{
setTimeout
,
clearTimeout
}
=
global
;
const
pTimeout
=
require
(
'p-timeout'
);
const
Support
=
require
(
'../support'
);
const
Support
=
require
(
'../support'
);
const
runningQueries
=
new
Set
();
const
CLEANUP_TIMEOUT
=
Number
.
parseInt
(
process
.
env
.
SEQ_TEST_CLEANUP_TIMEOUT
,
10
)
||
10000
;
let
runningQueries
=
new
Set
();
before
(
function
()
{
before
(
function
()
{
this
.
sequelize
.
addHook
(
'beforeQuery'
,
(
options
,
query
)
=>
{
this
.
sequelize
.
addHook
(
'beforeQuery'
,
(
options
,
query
)
=>
{
...
@@ -17,14 +24,49 @@ beforeEach(async function() {
...
@@ -17,14 +24,49 @@ beforeEach(async function() {
await
Support
.
clearDatabase
(
this
.
sequelize
);
await
Support
.
clearDatabase
(
this
.
sequelize
);
});
});
afterEach
(
function
()
{
afterEach
(
async
function
()
{
if
(
runningQueries
.
size
===
0
)
{
// Note: recall that throwing an error from a `beforeEach` or `afterEach` hook in Mocha causes the entire test suite to abort.
return
;
let
runningQueriesProblem
;
if
(
runningQueries
.
size
>
0
)
{
runningQueriesProblem
=
`Expected 0 queries running after this test, but there are still
${
runningQueries
.
size
}
queries
running
in
the
database
(
or
,
at
least
,
the
\
`afterQuery\` Sequelize hook did not fire for them):\n\n
${
// prettier-ignore
[...
runningQueries
].
map
(
query
=>
`
${
query
.
uuid
}
:
${
query
.
sql
}
`
).
join
(
'\n'
)
}
`;
}
runningQueries = new Set();
try {
await pTimeout(
Support.clearDatabase(this.sequelize),
CLEANUP_TIMEOUT,
`
Could
not
clear
database
after
this
test
in
less
than
$
{
CLEANUP_TIMEOUT
}
ms. This test crashed the DB, and testing cannot continue. Aborting.`
,
{
customTimers
:
{
setTimeout
,
clearTimeout
}
}
);
}
catch
(
error
)
{
let
message
=
error
.
message
;
if
(
runningQueriesProblem
)
{
message
+=
`\n\n Also,
${
runningQueriesProblem
}
`
;
}
message
+=
`\n\n Full test name:\n
${
this
.
currentTest
.
fullTitle
()}
`
;
// Throw, aborting the entire Mocha execution
throw
new
Error
(
message
);
}
if
(
runningQueriesProblem
)
{
if
(
this
.
test
.
ctx
.
currentTest
.
state
===
'passed'
)
{
// `this.test.error` is an obscure Mocha API that allows failing a test from the `afterEach` hook
// This is better than throwing because throwing would cause the entire Mocha execution to abort
this
.
test
.
error
(
new
Error
(
`This test passed, but
${
runningQueriesProblem
}
`
));
}
else
{
console
.
log
(
`
${
runningQueriesProblem
}
`
);
}
}
}
let
msg
=
`Expected 0 running queries.
${
runningQueries
.
size
}
queries still running in
${
this
.
currentTest
.
fullTitle
()}
\n`
;
msg
+=
'Queries:\n\n'
;
msg
+=
[...
runningQueries
].
map
(
query
=>
`
${
query
.
uuid
}
:
${
query
.
sql
}
`
).
join
(
'\n'
);
throw
new
Error
(
msg
);
});
});
module
.
exports
=
Support
;
module
.
exports
=
Support
;
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment