2020-02-08 22:23:33 +00:00
|
|
|
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
|
2016-05-03 23:52:03 +00:00
|
|
|
// Node module: loopback-connector-mysql
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
'use strict';
|
2020-08-28 06:43:09 +00:00
|
|
|
const Transaction = require('loopback-datasource-juggler').Transaction;
|
2015-05-18 19:02:25 +00:00
|
|
|
require('./init.js');
|
|
|
|
require('should');
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
let db, Post, Review;
|
2015-05-18 19:02:25 +00:00
|
|
|
|
2015-05-29 15:43:26 +00:00
|
|
|
describe('transactions with promise', function() {
|
2015-05-18 19:02:25 +00:00
|
|
|
before(function(done) {
|
2017-07-24 16:48:26 +00:00
|
|
|
db = global.getDataSource({collation: 'utf8_general_ci',
|
|
|
|
createDatabase: true});
|
2015-05-18 19:02:25 +00:00
|
|
|
db.once('connected', function() {
|
|
|
|
Post = db.define('PostTX', {
|
|
|
|
title: {type: String, length: 255, index: true},
|
2016-08-10 18:41:03 +00:00
|
|
|
content: {type: String},
|
2015-05-18 19:02:25 +00:00
|
|
|
}, {mysql: {engine: 'INNODB'}});
|
|
|
|
Review = db.define('ReviewTX', {
|
|
|
|
author: String,
|
2016-08-10 18:41:03 +00:00
|
|
|
content: {type: String},
|
2015-05-18 19:02:25 +00:00
|
|
|
}, {mysql: {engine: 'INNODB'}});
|
|
|
|
Post.hasMany(Review, {as: 'reviews', foreignKey: 'postId'});
|
|
|
|
db.automigrate(['PostTX', 'ReviewTX'], done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-17 20:16:59 +00:00
|
|
|
after(function(done) {
|
|
|
|
// disconnect from this db to avoid too many connection error
|
|
|
|
// due to multiple instance of connection pool
|
|
|
|
db.disconnect();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
let currentTx;
|
|
|
|
let hooks = [];
|
2015-05-18 19:02:25 +00:00
|
|
|
// Return an async function to start a transaction and create a post
|
|
|
|
function createPostInTx(post, timeout) {
|
|
|
|
return function(done) {
|
|
|
|
// Transaction.begin(db.connector, Transaction.READ_COMMITTED,
|
2020-08-28 06:43:09 +00:00
|
|
|
const promise = Post.beginTransaction({
|
2015-05-18 19:02:25 +00:00
|
|
|
isolationLevel: Transaction.READ_COMMITTED,
|
2016-08-10 18:41:03 +00:00
|
|
|
timeout: timeout,
|
2015-05-18 19:02:25 +00:00
|
|
|
});
|
|
|
|
promise.then(function(tx) {
|
|
|
|
(typeof tx.id).should.be.eql('string');
|
|
|
|
currentTx = tx;
|
|
|
|
hooks = [];
|
|
|
|
tx.observe('before commit', function(context, next) {
|
|
|
|
hooks.push('before commit');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
tx.observe('after commit', function(context, next) {
|
|
|
|
hooks.push('after commit');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
tx.observe('before rollback', function(context, next) {
|
|
|
|
hooks.push('before rollback');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
tx.observe('after rollback', function(context, next) {
|
|
|
|
hooks.push('after rollback');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}).then(function() {
|
|
|
|
Post.create(post, {transaction: currentTx}).then(
|
|
|
|
function(p) {
|
|
|
|
p.reviews.create({
|
|
|
|
author: 'John',
|
2016-08-10 18:41:03 +00:00
|
|
|
content: 'Review for ' + p.title,
|
2015-05-18 19:02:25 +00:00
|
|
|
}, {transaction: currentTx}).then(
|
|
|
|
function(c) {
|
|
|
|
done(null, c);
|
2020-08-28 06:43:09 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2015-05-18 19:02:25 +00:00
|
|
|
}).catch(done);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-24 16:48:26 +00:00
|
|
|
// Return an async function to find matching posts and assert number of
|
|
|
|
// records to equal to the count
|
2015-05-18 19:02:25 +00:00
|
|
|
function expectToFindPosts(where, count, inTx) {
|
|
|
|
return function(done) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const options = {};
|
2015-05-18 19:02:25 +00:00
|
|
|
if (inTx) {
|
|
|
|
options.transaction = currentTx;
|
|
|
|
}
|
|
|
|
Post.find({where: where}, options).then(
|
|
|
|
function(posts) {
|
|
|
|
posts.length.should.be.eql(count);
|
|
|
|
if (count) {
|
|
|
|
// Find related reviews
|
|
|
|
// Please note the empty {} is required, otherwise, the options
|
|
|
|
// will be treated as a filter
|
|
|
|
posts[0].reviews({}, options).then(function(reviews) {
|
|
|
|
reviews.length.should.be.eql(count);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
2020-08-28 06:43:09 +00:00
|
|
|
},
|
|
|
|
).catch(done);
|
2015-05-18 19:02:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('commit', function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
const post = {title: 't1', content: 'c1'};
|
2015-05-18 19:02:25 +00:00
|
|
|
before(createPostInTx(post));
|
|
|
|
|
|
|
|
it('should not see the uncommitted insert', expectToFindPosts(post, 0));
|
|
|
|
|
|
|
|
it('should see the uncommitted insert from the same transaction',
|
|
|
|
expectToFindPosts(post, 1, true));
|
|
|
|
|
|
|
|
it('should commit a transaction', function(done) {
|
|
|
|
currentTx.commit().then(function() {
|
|
|
|
hooks.should.be.eql(['before commit', 'after commit']);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should see the committed insert', expectToFindPosts(post, 1));
|
|
|
|
|
|
|
|
it('should report error if the transaction is not active', function(done) {
|
|
|
|
currentTx.commit().catch(function(err) {
|
|
|
|
(err).should.be.instanceof(Error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rollback', function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
const post = {title: 't2', content: 'c2'};
|
2015-05-18 19:02:25 +00:00
|
|
|
before(createPostInTx(post));
|
|
|
|
|
|
|
|
it('should not see the uncommitted insert', expectToFindPosts(post, 0));
|
|
|
|
|
|
|
|
it('should see the uncommitted insert from the same transaction',
|
|
|
|
expectToFindPosts(post, 1, true));
|
|
|
|
|
|
|
|
it('should rollback a transaction', function(done) {
|
|
|
|
currentTx.rollback().then(function() {
|
|
|
|
hooks.should.be.eql(['before rollback', 'after rollback']);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not see the rolledback insert', expectToFindPosts(post, 0));
|
|
|
|
|
|
|
|
it('should report error if the transaction is not active', function(done) {
|
|
|
|
currentTx.rollback().catch(function(err) {
|
|
|
|
(err).should.be.instanceof(Error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('timeout', function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
const post = {title: 't3', content: 'c3'};
|
2015-05-18 19:02:25 +00:00
|
|
|
before(createPostInTx(post, 500));
|
|
|
|
|
|
|
|
it('should invoke the timeout hook', function(done) {
|
|
|
|
currentTx.observe('timeout', function(context, next) {
|
|
|
|
next();
|
2015-05-29 15:43:26 +00:00
|
|
|
// It will only proceed upon timeout
|
2015-05-18 19:02:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-29 15:43:26 +00:00
|
|
|
it('should rollback the transaction if timeout', function(done) {
|
|
|
|
Post.find({where: {title: 't3'}}, {transaction: currentTx},
|
|
|
|
function(err, posts) {
|
|
|
|
if (err) return done(err);
|
|
|
|
posts.length.should.be.eql(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-05-18 19:02:25 +00:00
|
|
|
});
|
2015-05-29 15:43:26 +00:00
|
|
|
});
|