Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyu714 committed Sep 15, 2022
1 parent e2d94b3 commit 3deae38
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"ink-docstrap": "^1.3.2",
"log4js": "^6.3.0",
"mocha": "^9.1.3",
"sinon": "^12.0.1"
"sinon": "^14.0.0"
},
"homepage": "https://github.com/awslabs/amazon-kinesis-client-nodejs",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion test/kcl/action_handler_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SPDX-License-Identifier: Apache-2.0

var chai = require('chai');
var expect = chai.expect;
var should = chai.should();
var sinon = require('sinon');
var util = require('util');

Expand Down Expand Up @@ -78,7 +79,7 @@ describe('action_handler_tests', function() {

it('should write action to stdout', function(done) {
actionHandler.sendAction({action : 'initialize', shardId : 'shardId-000001'}, function(err) {
expect(err).to.be.undefined();
should.equal(err, undefined);
expect(stdoutHook.readLast()).to.equal('{"action":"initialize","shardId":"shardId-000001"}');
done();
});
Expand Down
21 changes: 10 additions & 11 deletions test/kcl/checkpointer_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ SPDX-License-Identifier: Apache-2.0
'use strict';


var chai = require('chai');
var expect = require('chai').expect;
var sinon = require('sinon');
var util = require('util');

var Checkpointer = require('../../lib/kcl/checkpointer');
var KCLManager = require('../../lib/kcl/kcl_manager');
Expand All @@ -24,7 +22,7 @@ describe('checkpointer_tests', function() {
});

beforeEach(function() {
sandbox = sinon.sandbox.create();
sandbox = sinon.createSandbox();
});

afterEach(function() {
Expand All @@ -38,37 +36,37 @@ describe('checkpointer_tests', function() {
it('should emit a checkpoint action and consume response action', function(done) {
var seq = Math.floor((Math.random() * 1000000)).toString();
// Mock KCLManager checkpoint and short-circuit dummy response.
sandbox.stub(kclManager, 'checkpoint', function(seq) {
sandbox.stub(kclManager, 'checkpoint').callsFake(function(seq) {
checkpointer.onCheckpointerResponse(null, seq);
});

checkpointer.checkpoint(seq, function(err, seq) {
expect(err).to.be.null();
expect(err).to.be.equal(null);
done();
});
});

it('should emit a checkpoint action and consume response when no sequence number', function(done) {
sandbox.stub(kclManager, 'checkpoint', function(seq) {
expect(seq).to.be.null();
sandbox.stub(kclManager, 'checkpoint').callsFake(function(seq) {
expect(seq).to.be.equal(null);
checkpointer.onCheckpointerResponse(null, seq);
});

checkpointer.checkpoint(function(err) {
expect(err).to.be.null();
expect(err).to.be.equal(null);
done();
});
});

it('should raise an error when error is received from MultiLangDaemon', function(done) {
var seq = Math.floor((Math.random() * 1000000)).toString();
// Mock KCLManager checkpoint and short-circuit dummy response.
sandbox.stub(kclManager, 'checkpoint', function(seq) {
sandbox.stub(kclManager, 'checkpoint').callsFake(function(seq) {
checkpointer.onCheckpointerResponse('ThrottlingException', seq);
});

checkpointer.checkpoint(seq, function(err) {
expect(err).not.to.be.null();
expect(err).not.to.be.equal(null);
expect(err).to.equal('ThrottlingException');
done();
});
Expand All @@ -77,8 +75,9 @@ describe('checkpointer_tests', function() {
it('should raise an error on checkpoint when previous checkpoint is not complete', function(done) {
var seq = Math.floor((Math.random() * 1000000)).toString();
// Mock KCLManager checkpoint to have outstanding checkpoint.
sandbox.stub(kclManager, 'checkpoint', function(seq) {
sandbox.stub(kclManager, 'checkpoint').callsFake(function(seq) {
});

checkpointer.checkpoint(seq, function(err) {
});

Expand Down
5 changes: 3 additions & 2 deletions test/kcl/io_handler_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SPDX-License-Identifier: Apache-2.0

var chai = require('chai');
var expect = chai.expect;
var should = chai.should();
var sinon = require('sinon');
var util = require('util');

Expand Down Expand Up @@ -83,10 +84,10 @@ describe('io_handler_tests', function() {
var callback = sinon.spy();
ioHandler.on('line', callback);
process.stdin.emit('data', 'line1\n');
expect(callback.calledOnce).to.be.true();
expect(callback.calledOnce).to.be.equal(true);
ioHandler.destroy();
process.stdin.emit('data', 'line2\n');
expect(callback.calledTwice).to.be.false();
expect(callback.calledTwice).to.be.equal(false);
ioHandler.removeListener('line', callback);
done();
});
Expand Down
30 changes: 14 additions & 16 deletions test/kcl/kcl_process_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

'use strict';

var chai = require('chai');
var expect = require('chai').expect;
var sinon = require('sinon');
var util = require('util');

var kcl = require('../..');

Expand Down Expand Up @@ -51,28 +49,28 @@ describe('kcl_process_tests', function () {
var sandbox = null;
var kclProcess = null;

var initialize = {action: 'initialize', shardId: 'shardId-000000000001'};
var initialize = { action: 'initialize', shardId: 'shardId-000000000001' };
var initializeString = JSON.stringify(initialize) + '\n';
var initializeResponse = JSON.stringify({action: 'status', responseFor: initialize.action});
var initializeResponse = JSON.stringify({ action: 'status', responseFor: initialize.action });

var processRecords = {
action: 'processRecords',
records: [{'data': 'bWVvdw==', 'partitionKey': 'cat', 'sequenceNumber': '456'}]
records: [{ 'data': 'bWVvdw==', 'partitionKey': 'cat', 'sequenceNumber': '456' }]
};
var processRecordsString = JSON.stringify(processRecords) + '\n';
var processRecordsResponse = JSON.stringify({action: 'status', responseFor: processRecords.action});
var processRecordsResponse = JSON.stringify({ action: 'status', responseFor: processRecords.action });

var checkpoint = {action: 'checkpoint', sequenceNumber: '456'};
var checkpoint = { action: 'checkpoint', sequenceNumber: '456' };
var checkpointString = JSON.stringify(checkpoint);
var checkpointResponse = JSON.stringify({action: checkpoint.action, checkpoint: checkpoint.sequenceNumber}) + '\n';
var checkpointResponse = JSON.stringify({ action: checkpoint.action, checkpoint: checkpoint.sequenceNumber }) + '\n';

var shardEnded = {action: 'shardEnded'};
var shardEnded = { action: 'shardEnded' };
var shardEndedString = JSON.stringify(shardEnded) + '\n';
var shardEndedResponse = JSON.stringify({action: 'status', responseFor: shardEnded.action});
var shardEndedResponse = JSON.stringify({ action: 'status', responseFor: shardEnded.action });

beforeEach(function () {
kclProcess = kcl(new RecordProcessor());
sandbox = sinon.sandbox.create();
sandbox = sinon.createSandbox();
});

afterEach(function () {
Expand All @@ -82,7 +80,7 @@ describe('kcl_process_tests', function () {

it('should initialize kcl and send back response', function (done) {
// Since we can't know when run() would finish processing all inputs, creating a stub for last call in the chain to force verification.
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction', function (data, callback) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction').callsFake(function (data, callback) {
let dataString = JSON.stringify(data);
console.log('Got response: ' + dataString);
callback();
Expand All @@ -97,7 +95,7 @@ describe('kcl_process_tests', function () {
});

it('should process records, checkpoint and then send back response', function (done) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction', function (data, callback) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction').callsFake(function (data, callback) {
let dataString = JSON.stringify(data);
console.log('Got response: ' + dataString);
callback();
Expand All @@ -121,7 +119,7 @@ describe('kcl_process_tests', function () {
});

it('should invoke shardEnd and send back response', function (done) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction', function (data, callback) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction').callsFake(function (data, callback) {
let dataString = JSON.stringify(data);
console.log('Got response: ' + dataString);
expect(dataString).to.equal(shardEndedResponse);
Expand All @@ -135,7 +133,7 @@ describe('kcl_process_tests', function () {

it('should process Initialize, one or more processRecords and shutdown in order', function (done) {
// Since we can't know when run() would finish processing all inputs, creating a stub for last call in the chain to force verification !
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction', function (data, callback) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction').callsFake(function (data, callback) {
let dataString = JSON.stringify(data);
console.log('Got response: ' + dataString);
callback();
Expand Down Expand Up @@ -175,7 +173,7 @@ describe('kcl_process_tests', function () {

it('should process checkpoint error from MultiLangDaemon', function (done) {
// Since we can't know when run() would finish processing all inputs, creating a stub for last call in the chain to force verification !
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction', function (data, callback) {
sandbox.stub(kclProcess._kclManager._actionHandler, 'sendAction').callsFake(function (data, callback) {
let dataString = JSON.stringify(data);
console.log('Got response: ' + dataString);
callback();
Expand Down

0 comments on commit 3deae38

Please sign in to comment.