Skip to content

Commit b227bc1

Browse files
xujihui1985fengmk2
authored andcommitted
feat: support doomed transaction scope on test cases (ali-sdk#58)
1 parent 6f362c1 commit b227bc1

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/client.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,41 @@ proto.beginTransactionScope = function* (scope, ctx) {
9898
}
9999
};
100100

101+
/**
102+
* doomed to be rollbacked after transaction scope
103+
* useful on writing test that depend on database
104+
*
105+
* @param {Function} scope - scope with code
106+
* @param {Object} [ctx] - transaction env context, like koa's ctx.
107+
* To make sure only one active transaction on this ctx.
108+
* @return {Object} - scope return result
109+
*/
110+
proto.beginDoomedTransactionScope = function* (scope, ctx) {
111+
ctx = ctx || {};
112+
if (!ctx._transactionConnection) {
113+
ctx._transactionConnection = yield this.beginTransaction();
114+
ctx._transactionScopeCount = 1;
115+
} else {
116+
ctx._transactionScopeCount++;
117+
}
118+
const tran = ctx._transactionConnection;
119+
try {
120+
const result = yield scope(tran);
121+
ctx._transactionScopeCount--;
122+
if (ctx._transactionScopeCount === 0) {
123+
ctx._transactionConnection = null;
124+
}
125+
return result;
126+
} catch (err) {
127+
if (ctx._transactionConnection) {
128+
ctx._transactionConnection = null;
129+
}
130+
throw err;
131+
} finally {
132+
yield tran.rollback();
133+
}
134+
};
135+
101136
proto.end = function(callback) {
102137
// callback style
103138
if (callback) {

test/client.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,34 @@ describe('client.test.js', function() {
415415
});
416416
});
417417

418+
describe('beginDoomedTransactionScope(scope)', function() {
419+
420+
it('should insert 0 rows in a doomed transaction with ctx', function* () {
421+
const ctx = {};
422+
const db = this.db;
423+
424+
function* insert() {
425+
return yield db.beginDoomedTransactionScope(function* (conn) {
426+
yield conn.query('insert into ??(name, email, gmt_create, gmt_modified) \
427+
values(?, ?, now(), now())',
428+
[ table, prefix + 'beginDoomedTransactionScopeCtx1', prefix + 'm@beginDoomedTransactionScopeCtx1.com' ]);
429+
yield conn.query('insert into ??(name, email, gmt_create, gmt_modified) \
430+
values(?, ?, now(), now())',
431+
[ table, prefix + 'beginDoomedTransactionScopeCtx2', prefix + 'm@beginDoomedTransactionScopeCtx1.com' ]);
432+
return true;
433+
}, ctx);
434+
}
435+
436+
yield insert();
437+
438+
const rows = yield db.query('select * from ?? where email=? order by id',
439+
[ table, prefix + 'm@beginDoomedTransactionScopeCtx1.com' ]);
440+
assert.equal(rows.length, 0);
441+
assert.equal(ctx._transactionConnection, null);
442+
assert.equal(ctx._transactionScopeCount, 0);
443+
});
444+
});
445+
418446
describe('get(table, obj, options), select(table, options)', function() {
419447
before(function* () {
420448
let result = yield this.db.insert(table, {

0 commit comments

Comments
 (0)