Skip to content

Commit 5f9d590

Browse files
eseligerSimonSchick
authored andcommitted
fix(ci): flaky tests and dead code
1 parent 51c461b commit 5f9d590

File tree

11 files changed

+44
-124
lines changed

11 files changed

+44
-124
lines changed

lib/dialects/abstract/query.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const SqlString = require('../../sql-string');
55
const QueryTypes = require('../../query-types');
66
const Dot = require('dottie');
77
const deprecations = require('../../utils/deprecations');
8+
const uuid = require('uuid/v4');
89

910
class AbstractQuery {
1011

1112
constructor(connection, sequelize, options) {
13+
this.uuid = uuid();
1214
this.connection = connection;
1315
this.instance = options.instance;
1416
this.model = options.model;

lib/dialects/mariadb/query.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const AbstractQuery = require('../abstract/query');
4-
const uuidv4 = require('uuid/v4');
54
const sequelizeErrors = require('../../errors');
65
const _ = require('lodash');
76
const DataTypes = require('../../data-types');
@@ -17,7 +16,6 @@ const debug = logger.debugContext('sql:mariadb');
1716
class Query extends AbstractQuery {
1817
constructor(connection, sequelize, options) {
1918
super(connection, sequelize, Object.assign({ showWarnings: false }, options));
20-
this.uuid = uuidv4();
2119
}
2220

2321
static formatBindParameters(sql, values, dialect) {

lib/dialects/mysql/query.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const Utils = require('../../utils');
44
const AbstractQuery = require('../abstract/query');
5-
const uuidv4 = require('uuid/v4');
65
const sequelizeErrors = require('../../errors');
76
const _ = require('lodash');
87
const { logger } = require('../../utils/logger');
@@ -13,7 +12,6 @@ const debug = logger.debugContext('sql:mysql');
1312
class Query extends AbstractQuery {
1413
constructor(connection, sequelize, options) {
1514
super(connection, sequelize, Object.assign({ showWarnings: false }, options));
16-
this.uuid = uuidv4();
1715
}
1816

1917
static formatBindParameters(sql, values, dialect) {

lib/hooks.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ const hookTypes = {
4545
beforeSync: { params: 1 },
4646
afterSync: { params: 1 },
4747
beforeBulkSync: { params: 1 },
48-
afterBulkSync: { params: 1 }
48+
afterBulkSync: { params: 1 },
49+
beforeQuery: { params: 2 },
50+
afterQuery: { params: 2 }
4951
};
5052
exports.hooks = hookTypes;
5153

lib/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ class Model {
13321332
changes.push(() => this.QueryInterface.changeColumn(this.getTableName(options), columnName, currentAttribute));
13331333
}
13341334
});
1335-
return changes.reduce((p, fn) => p.then(fn), Promise.resolve());
1335+
return Promise.each(changes, f => f());
13361336
});
13371337
})
13381338
.then(() => this.QueryInterface.showIndex(this.getTableName(options), options))
@@ -1349,7 +1349,7 @@ class Model {
13491349
return 0;
13501350
});
13511351

1352-
return Promise.map(indexes, index => this.QueryInterface.addIndex(
1352+
return Promise.each(indexes, index => this.QueryInterface.addIndex(
13531353
this.getTableName(options),
13541354
Object.assign({
13551355
logging: options.logging,

lib/query-interface.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class QueryInterface {
312312
});
313313
}
314314
return this.getForeignKeysForTables(tableNames, options).then(foreignKeys => {
315-
const promises = [];
315+
const queries = [];
316316

317317
tableNames.forEach(tableName => {
318318
let normalizedTableName = tableName;
@@ -321,12 +321,12 @@ class QueryInterface {
321321
}
322322

323323
foreignKeys[normalizedTableName].forEach(foreignKey => {
324-
const sql = this.QueryGenerator.dropForeignKeyQuery(tableName, foreignKey);
325-
promises.push(this.sequelize.query(sql, options));
324+
queries.push(this.QueryGenerator.dropForeignKeyQuery(tableName, foreignKey));
326325
});
327326
});
328327

329-
return Promise.all(promises).then(() => dropAllTables(tableNames));
328+
return Promise.each(queries, q => this.sequelize.query(q, options))
329+
.then(() => dropAllTables(tableNames));
330330
});
331331
});
332332
}

lib/sequelize.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,6 @@ class Sequelize {
262262

263263
this.importCache = {};
264264

265-
this.test = {
266-
_trackRunningQueries: false,
267-
_runningQueries: 0,
268-
trackRunningQueries() {
269-
this._trackRunningQueries = true;
270-
},
271-
verifyNoRunningQueries() {
272-
if (this._runningQueries > 0) throw new Error(`Expected 0 running queries. ${this._runningQueries} queries still running`);
273-
}
274-
};
275-
276265
Sequelize.runHooks('afterInit', this);
277266
}
278267

@@ -551,17 +540,10 @@ class Sequelize {
551540

552541
const retryOptions = Object.assign({}, this.options.retry, options.retry || {});
553542

554-
return Promise.resolve(retry(retryParameters => Promise.try(() => {
555-
const isFirstTry = retryParameters.current === 1;
556-
557-
if (isFirstTry && this.test._trackRunningQueries) {
558-
this.test._runningQueries++;
559-
}
560-
543+
return Promise.resolve(retry(() => Promise.try(() => {
561544
if (options.transaction === undefined && Sequelize._cls) {
562545
options.transaction = Sequelize._cls.get('transaction');
563546
}
564-
565547
if (options.transaction && options.transaction.finished) {
566548
const error = new Error(`${options.transaction.finished} has been called on this transaction(${options.transaction.id}), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)`);
567549
error.sql = sql;
@@ -573,18 +555,15 @@ class Sequelize {
573555
: this.connectionManager.getConnection(options);
574556
}).then(connection => {
575557
const query = new this.dialect.Query(connection, this, options);
576-
577-
return query.run(sql, bindParameters)
558+
return this.runHooks('beforeQuery', options, query)
559+
.then(() => query.run(sql, bindParameters))
560+
.finally(() => this.runHooks('afterQuery', options, query))
578561
.finally(() => {
579562
if (!options.transaction) {
580563
return this.connectionManager.releaseConnection(connection);
581564
}
582565
});
583-
}), retryOptions)).finally(() => {
584-
if (this.test._trackRunningQueries) {
585-
this.test._runningQueries--;
586-
}
587-
});
566+
}), retryOptions));
588567
});
589568
}
590569

test/integration/model.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ describe(Support.getTestDialectTeaser('Model'), () => {
355355
});
356356

357357
it('should allow the user to specify indexes in options', function() {
358-
this.retries(3);
359358
const indices = [{
360359
name: 'a_b_uniq',
361360
unique: true,
@@ -815,7 +814,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
815814

816815
describe('save', () => {
817816
it('should mapping the correct fields when saving instance. see #10589', function() {
818-
const User = this.sequelize.define('User', {
817+
const User = this.sequelize.define('User', {
819818
id3: {
820819
field: 'id',
821820
type: Sequelize.INTEGER,
@@ -862,7 +861,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
862861
});
863862

864863
it('should mapping the correct fields when updating instance. see #10589', function() {
865-
const User = this.sequelize.define('User', {
864+
const User = this.sequelize.define('User', {
866865
id3: {
867866
field: 'id',
868867
type: Sequelize.INTEGER,

test/integration/support.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22

33
const Support = require('../support');
44

5+
const runningQueries = new Set();
6+
7+
before(function() {
8+
this.sequelize.addHook('beforeQuery', (options, query) => {
9+
runningQueries.add(query);
10+
});
11+
this.sequelize.addHook('afterQuery', (options, query) => {
12+
runningQueries.delete(query);
13+
});
14+
});
15+
516
beforeEach(function() {
6-
this.sequelize.test.trackRunningQueries();
717
return Support.clearDatabase(this.sequelize);
818
});
919

1020
afterEach(function() {
11-
try {
12-
this.sequelize.test.verifyNoRunningQueries();
13-
} catch (err) {
14-
err.message += ` in ${this.currentTest.fullTitle()}`;
15-
throw err;
21+
if (runningQueries.size === 0) {
22+
return;
1623
}
24+
throw new Error(`Expected 0 running queries. ${runningQueries.size} queries still running in ${this.currentTest.fullTitle()}`);
1725
});
1826

1927
module.exports = Support;

test/integration/transaction.test.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,7 @@ if (current.dialect.supports.transactions) {
124124
};
125125
// Attention: this test is a bit racy. If you find a nicer way to test this: go ahead
126126
return SumSumSum.sync({ force: true }).then(() => {
127-
return expect(Promise.join(transTest(80), transTest(80), transTest(80))).to.eventually.be.rejectedWith('could not serialize access due to read/write dependencies among transactions');
128-
}).delay(100).then(() => {
129-
if (this.sequelize.test.$runningQueries !== 0) {
130-
return Promise.delay(200);
131-
}
132-
return void 0;
133-
}).then(() => {
134-
if (this.sequelize.test.$runningQueries !== 0) {
135-
return Promise.delay(500);
136-
}
127+
return expect(Promise.all([transTest(80), transTest(80), transTest(80)])).to.eventually.be.rejectedWith('could not serialize access due to read/write dependencies among transactions');
137128
});
138129
});
139130
}

0 commit comments

Comments
 (0)