Skip to content

Commit c95b157

Browse files
committed
Added test
Added stopped check safeguards for pause and resume functions
1 parent d99e149 commit c95b157

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,15 @@ ZongJi.prototype.stop = function () {
295295
};
296296

297297
ZongJi.prototype.pause = function () {
298-
this.connection.pause();
298+
if (!this.stopped) {
299+
this.connection.pause();
300+
}
299301
};
300302

301303
ZongJi.prototype.resume = function () {
302-
this.connection.resume();
304+
if (!this.stopped) {
305+
this.connection.resume();
306+
}
303307
};
304308

305309
// It includes every events by default.

test/events.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,77 @@ tap.test('With many columns', (test) => {
440440
);
441441
});
442442
});
443+
444+
tap.test('Pause/Resume Binlog', (test) => {
445+
const TEST_TABLE = 'pause_resume_test_table';
446+
447+
test.test(`prepare table ${TEST_TABLE}`, (test) => {
448+
testDb.execute([`DROP TABLE IF EXISTS ${TEST_TABLE}`, `CREATE TABLE ${TEST_TABLE} (col INT UNSIGNED)`], (err) => {
449+
if (err) {
450+
return test.fail(err);
451+
}
452+
453+
test.end();
454+
});
455+
});
456+
457+
tap.test('Pausing binlog stops events', (test) => {
458+
const events = [];
459+
const zongji = new ZongJi(settings.connection);
460+
test.teardown(() => zongji.stop());
461+
462+
zongji.on('ready', () => {
463+
zongji.pause();
464+
paused = true;
465+
testDb.execute(
466+
[
467+
`INSERT INTO ${TEST_TABLE} (col)
468+
VALUES (14)`
469+
],
470+
(err) => {
471+
if (err) {
472+
return test.fail(err);
473+
}
474+
}
475+
);
476+
setTimeout(() => {
477+
paused = false;
478+
zongji.resume();
479+
}, 20);
480+
});
481+
482+
zongji.start({
483+
startAtEnd: true,
484+
serverId: testDb.serverId(),
485+
includeEvents: ['tablemap', 'writerows']
486+
});
487+
488+
let paused = false;
489+
zongji.on('binlog', (evt) => {
490+
if (paused) {
491+
// We don't expect any events while paused
492+
test.fail();
493+
return;
494+
}
495+
496+
events.push(evt);
497+
if (events.length == 2) {
498+
expectEvents(
499+
test,
500+
events,
501+
[
502+
tableMapEvent(TEST_TABLE),
503+
{
504+
_type: 'WriteRows',
505+
_checkTableMap: checkTableMatches(TEST_TABLE),
506+
rows: [{ col: 14 }]
507+
}
508+
],
509+
1,
510+
() => test.end()
511+
);
512+
}
513+
});
514+
});
515+
test.end();
516+
});

0 commit comments

Comments
 (0)