Skip to content

Commit

Permalink
Modify test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey600 committed Sep 15, 2017
1 parent 5f55ba8 commit 19d4645
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cache:
directories:
- node_modules
script:
- npm run lint
- npm test
after_script:
- npm run coverage
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ZD.prototype.getInvoker = function (path, opt) {

/**
* Expose `Invoker`. To create a Invoker with URIs directly.
*
* @type {Invoker}
*/
ZD.Invoker = Invoker;
Expand Down
13 changes: 9 additions & 4 deletions lib/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ Codec.prototype._parseLength = function (head) {
Codec.prototype._parseBody = function (heap, cb) {
// 检验魔数
if (heap[0] !== MAGIC_HIGH || heap[1] !== MAGIC_LOW) {
return cb('Magic header is wrong!');
return cb(new Error('Magic header is wrong!'));
}

// 检查序列化协议
if (2 !== (heap[2] & SERIALIZATION_MASK)) { // hessian2 的协议ID为 2
return cb('Unknown serialization protocol! Only hessian2 is supported.');
return cb(new Error('Unknown serialization protocol! Only hessian2 is supported.'));
}

// 打印标志位
Expand All @@ -313,7 +313,9 @@ Codec.prototype._parseBody = function (heap, cb) {

if (heap[3] !== response.CODE.OK) {
// error捕获
return cb(decodeURIComponent(heap.slice(HEADER_LENGTH + 2, heap.length - 1).toString()));
return cb(new Error(
decodeURIComponent(heap.slice(HEADER_LENGTH + 2, heap.length - 1).toString())
));
}

var buf;
Expand Down Expand Up @@ -344,9 +346,12 @@ Codec.prototype._parseBody = function (heap, cb) {
return cb(err);
}

if (ret instanceof Error || 0 === flag) {
if (ret instanceof Error) {
return cb(ret);
}
if (0 === flag) {
return cb(new Error(ret));
}
return cb(false, ret);
};

Expand Down
36 changes: 16 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ describe('test', function () {
it('excute success', function (done) {
zd.connect();

var invoker = zd.getInvoker('com.demo.Service', {
version: '1.0.0',
poolMax: 1
});
var invoker = zd.getInvoker('com.demo.Service', { version: '1.0.0' });
var method = 'get';
var arg1 = { $class: 'java.lang.String', $: '123456789' };

Expand Down Expand Up @@ -110,9 +107,9 @@ describe('test', function () {
mm(zookeeper, 'createClient', function (conn, opt) {
var zp = new MyZookeeper(conn, opt);
zp.getChildren = function (path, watcher, cb) {
setTimeout(function () {
cb(new Error('asdfs'));
}, 50);
setImmediate(function () {
cb(new Error());
});
};
return zp;
});
Expand All @@ -136,9 +133,9 @@ describe('test', function () {
mm(zookeeper, 'createClient', function (conn, opt) {
var zp = new MyZookeeper(conn, opt);
zp.getChildren = function (path, watcher, cb) {
setTimeout(function () {
setImmediate(function () {
cb(false, []);
}, 50);
});
};
return zp;
});
Expand All @@ -162,9 +159,9 @@ describe('test', function () {
mm(zookeeper, 'createClient', function (conn, opt) {
var zp = new MyZookeeper(conn, opt);
zp.getChildren = function (path, watcher, cb) {
setTimeout(function () {
setImmediate(function () {
cb(false, ['xxxxx']);
}, 50);
});
};
return zp;
});
Expand Down Expand Up @@ -218,7 +215,6 @@ describe('test', function () {
mm(zookeeper, 'createClient', function (conn, opt) {
return new MyZookeeper(conn, opt);
});
// noinspection JSUnresolvedVariable
mm(MySocket.prototype, 'destroy', function () {
setImmediate(function () {
this.emit('close', new Error());
Expand All @@ -232,7 +228,6 @@ describe('test', function () {
var method = 'get';
var arg1 = { $class: 'java.lang.String', $: '123456789' };
invoker.excute(method, [arg1], function (err, data) {
// todo
expect(err).not.to.be.a(Error);
expect(data).to.be(undefined);

Expand Down Expand Up @@ -299,7 +294,7 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xda, 0xbb, 0x02, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x91, 0xc8, 0x7b]));
codec.decodeResponse(function (err, data) {
expect(err).to.be(false);
expect(err).not.to.be.a(Error);
expect(data).to.be(123);
done();
});
Expand All @@ -311,7 +306,7 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xaa, 0xbb, 0x02, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92]));
codec.decodeResponse(function (err, data) {
expect(err).to.be.a('string');
expect(err).to.be.a(Error);
expect(data).to.be(undefined);
done();
});
Expand All @@ -323,7 +318,7 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xda, 0xbb, 0x22, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92]));
codec.decodeResponse(function (err, data) {
expect(err).to.be(false);
expect(err).not.to.be.a(Error);
expect(data).to.be(undefined);
done();
});
Expand All @@ -335,7 +330,7 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xda, 0xbb, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92]));
codec.decodeResponse(function (err, data) {
expect(err).to.be.a('string');
expect(err).to.be.a(Error);
expect(data).to.be(undefined);
done();
});
Expand All @@ -347,7 +342,7 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xda, 0xbb, 0x02, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92]));
codec.decodeResponse(function (err, data) {
expect(err).to.be.a('string');
expect(err).to.be.a(Error);
expect(data).to.be(undefined);
done();
});
Expand Down Expand Up @@ -383,7 +378,7 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xda, 0xbb, 0x02, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x92]));
codec.decodeResponse(function (err, data) {
expect(err).to.be(false);
expect(err).not.to.be.a(Error);
expect(data).to.be(undefined);
done();
});
Expand All @@ -395,7 +390,8 @@ describe('test', function () {
codec.pushChunk(new Buffer([0xda, 0xbb, 0x02, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x90, 0xc8, 0x7b]));
codec.decodeResponse(function (err, data) {
expect(err).to.be(123);
expect(err).to.be.a(Error);
expect(err.message).to.be('123');
expect(data).to.be(undefined);
done();
});
Expand Down

0 comments on commit 19d4645

Please sign in to comment.