Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Chunk response unit-testing #87

Merged
merged 5 commits into from

2 participants

@renedx

@3rd-Eden, I'm not sure this is what you really want. But it does takes the parser in test for chunked responses. I've added (a little bit too much) comments on the test to make sure everyone understands what I'm doing. Because it's kinda messed up, haha.

It also required for the buffer function to be inside the memcached object. Did not found a nice way to access it otherwise (may be overlooking something here?).

Also found a scoping issue in the makeCallback erroring that I've fixed + added a unittest for the case that was triggering the crash.

Comments, shoot! :)

@3rd-Eden
Owner

it lgtm, i'll probably merge it in tonight

@3rd-Eden 3rd-Eden merged commit d75949c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 6, 2013
  1. @renedx

    Merge pull request #2 from renedx/chunk-fix

    renedx authored
    Chunk fix
Commits on Mar 11, 2013
  1. @renedx
  2. @renedx
  3. @renedx

    [Fix] Stupid typo.

    renedx authored
  4. @renedx

    Too much copy-paste ;)

    renedx authored
This page is out of date. Refresh to see the latest.
View
9 lib/memcached.js
@@ -179,6 +179,11 @@ Client.config = {
this.connections[server].pull(callback);
};
+ // Exposes buffer to test-suite
+ memcached.buffer = function(){
+ return privates.buffer.apply(this, arguments)
+ };
+
// Creates a multi stream, so it's easier to query agains multiple memcached
// servers.
memcached.multi = function memcachedMulti(keys, callback) {
@@ -600,7 +605,7 @@ Client.config = {
// Fix zero-line endings in the middle
var chunkLength = (chunks.length-1);
- if( chunks[chunkLength].length == 0 )
+ if (chunks[chunkLength].length == 0)
chunks.splice(chunkLength, 1)
S.responseBuffer = ""; // clear!
@@ -719,7 +724,7 @@ Client.config = {
// Small wrapper function that only executes errors when we have a callback
privates.errorResponse = function errorResponse(error, callback) {
if (typeof callback === 'function') {
- this.makeCallback(callback,error, false);
+ memcached.makeCallback(callback,error, false);
}
return false;
View
21 test/memcached-get-set.test.js
@@ -335,6 +335,27 @@ describe("Memcached GET SET", function() {
});
/**
+ * Set maximum amount of data (1MB), should trigger error, not crash.
+ */
+ it("set maximum data and check for correct error handling", function(done) {
+ var memcached = new Memcached(common.servers.single)
+ , message = fs.readFileSync(__dirname + '/fixtures/lipsum.txt').toString()
+ , testnr = ++global.testnumbers
+ , callbacks = 0;
+
+ memcached.set("test:" + testnr, new Array(100).join(message), 1000, function(error, ok){
+ ++callbacks;
+
+ assert.equal(error, 'Error: The length of the value is greater than 1048576');
+ ok.should.be.false;
+
+ memcached.end(); // close connections
+ assert.equal(callbacks, 1);
+ done();
+ });
+ });
+
+ /**
* Not only small strings, but also large strings should be processed
* without any issues.
*/
View
67 test/memcached-parser.test.js
@@ -0,0 +1,67 @@
+/**
+ * Test dependencies
+ */
+
+var assert = require('assert')
+ , fs = require('fs')
+ , common = require('./common')
+ , Memcached = require('../');
+
+global.testnumbers = global.testnumbers || +(Math.random(10) * 1000000).toFixed();
+
+/**
+ * Expresso test suite for all `parser` related
+ * memcached commands
+ */
+describe("Memcached parser", function() {
+ it("chunked response", function(done) {
+ var memcached = new Memcached(common.servers.single)
+ , message = common.alphabet(256)
+ , chunks = []
+ , chunk = 'VALUE tests::#{key} 2 {length}'
+ , chunkJSON = JSON.stringify({
+ 'lines': []
+ , 'message': message
+ , 'id': null
+ })
+ , testnr = ++global.testnumbers
+ , callbacks = 0;
+
+ // Build up our tests
+ var S = {
+ 'responseBuffer': ''
+ , 'bufferArray': []
+ , 'metaData': []
+ , 'streamID': 0
+ };
+
+ // Build up our chunk data
+ chunks.push( chunk.replace( '{key}', 1 ).replace( '{length}', chunkJSON.length ) )
+ chunks.push( chunkJSON )
+ chunks.push( chunk.replace( '{key}', 2 ).replace( '{length}', chunkJSON.length ) )
+
+ // Insert first chunk
+ memcached.buffer(S, chunks.join( '\r\n' ) + '\r\n')
+
+ // We check for bufferArray length otherwise it will crash on 'SyntaxError: Unexpected token V'
+ assert.equal( S.bufferArray.length, 3 )
+
+ // Now add the value of the last response key in previous chunk
+ chunks.unshift( chunkJSON )
+
+ // Add it for the second chunk also
+ chunks.push( chunkJSON )
+
+ // Insert second chunk
+ memcached.buffer(S, chunks.join( '\r\n' ) + '\r\nEND\r\n')
+
+ // Check if everything is cleared up nicely.
+ assert.equal( S.responseBuffer.length, 0 )
+ assert.equal( S.bufferArray.length, 0 )
+ assert.equal( S.metaData.length, 0 )
+
+ memcached.end();
+ done()
+ });
+
+});
Something went wrong with that request. Please try again.