Permalink
Browse files

[fix] stop parsing after we have enountered an error

[test] added missing tests for invalid responses
  • Loading branch information...
1 parent d86dcb1 commit e00627a83c96c20542059477b5f01430807020a3 @3rd-Eden committed Jan 30, 2013
Showing with 53 additions and 3 deletions.
  1. +3 −3 index.js
  2. +50 −0 test/protocol.test.js
View
@@ -435,11 +435,11 @@ Parser.prototype.parse = function parse() {
// UNKNOWN RESPONSE, something went really fucked up wrong, we should
// probably destroy the parser.
err = new Error('Unknown response');
- err.CODE = 'EPARSERFUCKUPLULZ';
+ err.code = 'EPARSERFUCKUPLULZ';
err.data = data.slice(i);
- // DIE
- this.destroy(err);
+ // DIE, and don't continue parsing
+ return this.destroy(err);
}
}
View
@@ -576,6 +576,56 @@ describe('memcached-stream', function () {
memcached.write(data+ 'BANANANANA');
});
});
+
+ describe('INVALID response', function () {
+ var data = 'END\r\nHELLO WORLD\r\n';
+
+ it('emits an error event when it encounters invalid data', function (done) {
+ var memcached = new Parser();
+
+ memcached.on('error', function (err) {
+ expect(err).to.be.instanceOf(Error);
+ expect(err.code).to.equal('EPARSERFUCKUPLULZ');
+ expect(err.data).to.equal('HELLO WORLD\r\n');
+
+ // should clear the cache
+ process.nextTick(function () {
+ expect(memcached.queue).to.equal('');
+
+ done();
+ });
+ });
+
+ memcached.write(data);
+ });
+
+ it('correctly cleans the queue', function (done) {
+ var memcached = new Parser();
+
+ memcached.on('error', function (err) {
+ process.nextTick(function () {
+ expect(memcached.queue).to.equal('');
+
+ done();
+ });
+ });
+
+ memcached.write(data+ 'BANANANANA');
+ });
+
+ it('destroys the parser', function (done) {
+ var memcached = new Parser();
+
+ memcached.on('error', function (err) {
+ expect(memcached.writable).to.equal(false);
+ expect(memcached.destroyed).to.equal(true);
+
+ done();
+ });
+
+ memcached.write(data);
+ });
+ });
});
describe('extendend protocol support', function () {

0 comments on commit e00627a

Please sign in to comment.