1515 */
1616'use strict' ;
1717const assert = require ( 'assert' ) ;
18+ const events = require ( 'events' ) ;
1819
1920const Encoder = require ( '../../lib/encoder' ) ;
2021const streams = require ( '../../lib/streams' ) ;
@@ -948,6 +949,28 @@ describe('Parser', function () {
948949 } ) ;
949950 } ) ;
950951 describe ( 'with multiple chunk lengths piped' , function ( ) {
952+
953+ const expected = [
954+ { columnLength : 3 , rowLength : 10 } ,
955+ { columnLength : 5 , rowLength : 5 } ,
956+ { columnLength : 6 , rowLength : 15 } ,
957+ { columnLength : 6 , rowLength : 15 } ,
958+ { columnLength : 1 , rowLength : 20 }
959+ ] ;
960+
961+ class TestEmitter extends events . EventEmitter { }
962+ const emitter = new TestEmitter ( ) ;
963+ const doneParsing = new Promise ( ( resolve ) => {
964+
965+ let cnt = 0 ;
966+ emitter . on ( 'parseDone' , ( ) => {
967+ cnt += 1 ;
968+ if ( cnt === expected . length ) {
969+ resolve ( ) ;
970+ }
971+ } ) ;
972+ } ) ;
973+
951974 const protocol = new streams . Protocol ( { objectMode : true } ) ;
952975 const parser = newInstance ( ) ;
953976 protocol . pipe ( parser ) ;
@@ -961,6 +984,7 @@ describe('Parser', function () {
961984 }
962985 byRowCompleted = item . byRowCompleted ;
963986 if ( byRowCompleted ) {
987+ emitter . emit ( 'parseDone' ) ;
964988 continue ;
965989 }
966990 assert . strictEqual ( item . header . opcode , types . opcodes . result ) ;
@@ -969,13 +993,6 @@ describe('Parser', function () {
969993 result [ item . header . streamId ] . push ( item . row ) ;
970994 }
971995 } ) ;
972- const expected = [
973- { columnLength : 3 , rowLength : 10 } ,
974- { columnLength : 5 , rowLength : 5 } ,
975- { columnLength : 6 , rowLength : 15 } ,
976- { columnLength : 6 , rowLength : 15 } ,
977- { columnLength : 1 , rowLength : 20 }
978- ] ;
979996 [ 1 , 2 , 7 , 11 ] . forEach ( function ( chunkLength ) {
980997 it ( 'should emit rows chunked with chunk length of ' + chunkLength , function ( done ) {
981998 result = { } ;
@@ -992,15 +1009,18 @@ describe('Parser', function () {
9921009 }
9931010 protocol . _transform ( buffer . slice ( j , end ) , null , helper . throwop ) ;
9941011 }
995- process . nextTick ( ( ) => {
996- assert . ok ( byRowCompleted ) ;
997- //assert result
998- expected . forEach ( function ( expectedItem , index ) {
999- assert . ok ( result [ index ] , 'Result not found for index ' + index ) ;
1000- assert . strictEqual ( result [ index ] . length , expectedItem . rowLength ) ;
1001- assert . strictEqual ( result [ index ] [ 0 ] . keys ( ) . length , expectedItem . columnLength ) ;
1012+ doneParsing . then ( ( ) => {
1013+
1014+ process . nextTick ( ( ) => {
1015+ assert . ok ( byRowCompleted ) ;
1016+ //assert result
1017+ expected . forEach ( function ( expectedItem , index ) {
1018+ assert . ok ( result [ index ] , 'Result not found for index ' + index ) ;
1019+ assert . strictEqual ( result [ index ] . length , expectedItem . rowLength ) ;
1020+ assert . strictEqual ( result [ index ] [ 0 ] . keys ( ) . length , expectedItem . columnLength ) ;
1021+ } ) ;
1022+ done ( ) ;
10021023 } ) ;
1003- done ( ) ;
10041024 } ) ;
10051025 } ) ;
10061026 } ) ;
0 commit comments