@@ -40,6 +40,32 @@ describe('parsers', function () {
4040
4141 parsers . forEach ( function ( Parser ) {
4242 describe ( Parser . name , function ( ) {
43+ it ( 'chunks getting to big for the bufferPool' , function ( ) {
44+ // This is a edge case. Chunks should not exceed Math.pow(2, 16) bytes
45+ var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
46+ 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' +
47+ 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ' +
48+ 'ut aliquip ex ea commodo consequat. Duis aute irure dolor in' // 256 chars
49+ var bigString = ( new Array ( Math . pow ( 2 , 17 ) / lorem . length ) . join ( lorem ) ) // Math.pow(2, 17) chars long
50+ var replyCount = 0
51+ function checkReply ( reply ) {
52+ replyCount ++
53+ }
54+ var parser = new Parser ( {
55+ returnReply : checkReply ,
56+ returnError : returnError ,
57+ returnFatalError : returnFatalError
58+ } )
59+ parser . execute ( new Buffer ( '+test' ) )
60+ assert . strictEqual ( replyCount , 0 )
61+ parser . execute ( new Buffer ( '\r\n+' ) )
62+ assert . strictEqual ( replyCount , 1 )
63+ parser . execute ( new Buffer ( bigString ) )
64+ assert . strictEqual ( replyCount , 1 )
65+ parser . execute ( new Buffer ( '\r\n' ) )
66+ assert . strictEqual ( replyCount , 2 )
67+ } )
68+
4369 it ( 'handles multi-bulk reply and check context binding' , function ( ) {
4470 var replyCount = 0
4571 function Abc ( ) { }
@@ -430,9 +456,6 @@ describe('parsers', function () {
430456 } )
431457
432458 it ( 'handle big numbers' , function ( ) {
433- if ( Parser . name === 'HiredisReplyParser' ) {
434- return this . skip ( )
435- }
436459 var replyCount = 0
437460 var number = 9007199254740991 // Number.MAX_SAFE_INTEGER
438461 function checkReply ( reply ) {
@@ -450,10 +473,58 @@ describe('parsers', function () {
450473 assert . strictEqual ( replyCount , 2 )
451474 } )
452475
453- it ( 'handle big data' , function ( ) {
454- if ( Parser . name === 'HiredisReplyParser' ) {
455- return this . skip ( )
476+ it ( 'handle big data with buffers' , function ( done ) {
477+ var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
478+ 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' +
479+ 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ' +
480+ 'ut aliquip ex ea commodo consequat. Duis aute irure dolor in' // 256 chars
481+ var bigStringArray = ( new Array ( Math . pow ( 2 , 16 ) / lorem . length ) . join ( lorem + ' ' ) ) . split ( ' ' ) // Math.pow(2, 16) chars long
482+ var startBigBuffer = new Buffer ( '\r\n$' + ( 128 * 1024 ) + '\r\n' )
483+ var startSecondBigBuffer = new Buffer ( '\r\n$' + ( 256 * 1024 ) + '\r\n' )
484+ var chunks = new Array ( 4 )
485+ for ( var i = 0 ; i < 4 ; i ++ ) {
486+ chunks [ i ] = new Buffer ( bigStringArray . join ( ' ' ) + '.' ) // Math.pow(2, 16) chars long
487+ }
488+ var replyCount = 0
489+ function checkReply ( reply ) {
490+ replyCount ++
456491 }
492+ var parser = new Parser ( {
493+ returnReply : checkReply ,
494+ returnError : returnError ,
495+ returnFatalError : returnFatalError ,
496+ returnBuffers : true
497+ } )
498+ parser . execute ( new Buffer ( '+test' ) )
499+ assert . strictEqual ( replyCount , 0 )
500+ parser . execute ( startBigBuffer )
501+ for ( i = 0 ; i < 2 ; i ++ ) {
502+ if ( Parser . name === 'JavascriptReplyParser' ) {
503+ assert . strictEqual ( parser . bufferCache . length , i + 1 )
504+ }
505+ parser . execute ( chunks [ i ] )
506+ }
507+ assert . strictEqual ( replyCount , 1 )
508+ parser . execute ( new Buffer ( '\r\n' ) )
509+ assert . strictEqual ( replyCount , 2 )
510+ parser . execute ( new Buffer ( '+test' ) )
511+ assert . strictEqual ( replyCount , 2 )
512+ parser . execute ( startSecondBigBuffer )
513+ for ( i = 0 ; i < 4 ; i ++ ) {
514+ if ( Parser . name === 'JavascriptReplyParser' ) {
515+ assert . strictEqual ( parser . bufferCache . length , i + 1 )
516+ }
517+ parser . execute ( chunks [ i ] )
518+ }
519+ assert . strictEqual ( replyCount , 3 )
520+ parser . execute ( new Buffer ( '\r\n' ) )
521+ assert . strictEqual ( replyCount , 4 )
522+ // Delay done so the bufferPool is cleared and tested
523+ // If the buffer is not cleared, the coverage is not going to be at 100%
524+ setTimeout ( done , 600 )
525+ } )
526+
527+ it ( 'handle big data' , function ( ) {
457528 var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
458529 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' +
459530 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ' +
@@ -476,7 +547,9 @@ describe('parsers', function () {
476547 } )
477548 parser . execute ( startBigBuffer )
478549 for ( i = 0 ; i < 64 ; i ++ ) {
479- assert . strictEqual ( parser . bufferCache . length , i + 1 )
550+ if ( Parser . name === 'JavascriptReplyParser' ) {
551+ assert . strictEqual ( parser . bufferCache . length , i + 1 )
552+ }
480553 parser . execute ( chunks [ i ] )
481554 }
482555 assert . strictEqual ( replyCount , 0 )
@@ -485,9 +558,6 @@ describe('parsers', function () {
485558 } )
486559
487560 it ( 'handle big data 2' , function ( ) {
488- if ( Parser . name === 'HiredisReplyParser' ) {
489- return this . skip ( )
490- }
491561 var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
492562 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' +
493563 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ' +
@@ -510,13 +580,49 @@ describe('parsers', function () {
510580 parser . execute ( new Buffer ( '+test' ) )
511581 parser . execute ( startBigBuffer )
512582 for ( i = 0 ; i < 64 ; i ++ ) {
513- assert . strictEqual ( parser . bufferCache . length , i + 1 )
583+ if ( Parser . name === 'JavascriptReplyParser' ) {
584+ assert . strictEqual ( parser . bufferCache . length , i + 1 )
585+ }
514586 parser . execute ( chunks [ i ] )
515587 }
516588 assert . strictEqual ( replyCount , 1 )
517589 parser . execute ( new Buffer ( '\r\n' ) )
518590 assert . strictEqual ( replyCount , 2 )
519591 } )
592+
593+ it ( 'handle big data 2 with buffers' , function ( ) {
594+ var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
595+ 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' +
596+ 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ' +
597+ 'ut aliquip ex ea commodo consequat. Duis aute irure dolor in' // 256 chars
598+ var bigStringArray = ( new Array ( Math . pow ( 2 , 16 ) / lorem . length ) . join ( lorem + ' ' ) ) . split ( ' ' ) // Math.pow(2, 16) chars long
599+ var startBigBuffer = new Buffer ( '$' + ( 4 * 1024 * 1024 ) + '\r\n' )
600+ var chunks = new Array ( 64 )
601+ for ( var i = 0 ; i < 64 ; i ++ ) {
602+ chunks [ i ] = new Buffer ( bigStringArray . join ( ' ' ) + '.' ) // Math.pow(2, 16) chars long
603+ }
604+ var replyCount = 0
605+ function checkReply ( reply ) {
606+ assert . strictEqual ( reply . length , 4 * 1024 * 1024 )
607+ replyCount ++
608+ }
609+ var parser = new Parser ( {
610+ returnReply : checkReply ,
611+ returnError : returnError ,
612+ returnFatalError : returnFatalError ,
613+ returnBuffers : true
614+ } )
615+ parser . execute ( startBigBuffer )
616+ for ( i = 0 ; i < 64 ; i ++ ) {
617+ if ( Parser . name === 'JavascriptReplyParser' ) {
618+ assert . strictEqual ( parser . bufferCache . length , i + 1 )
619+ }
620+ parser . execute ( chunks [ i ] )
621+ }
622+ assert . strictEqual ( replyCount , 0 )
623+ parser . execute ( new Buffer ( '\r\n' ) )
624+ assert . strictEqual ( replyCount , 1 )
625+ } )
520626 } )
521627 } )
522628} )
0 commit comments