1
1
'use strict' ;
2
- const { once } = require ( 'events' ) ;
2
+ const { on , once } = require ( 'events' ) ;
3
3
const { Readable, Writable } = require ( 'stream' ) ;
4
4
5
5
const { MessageStream } = require ( '../../../src/cmap/message_stream' ) ;
@@ -26,11 +26,14 @@ describe('MessageStream', function () {
26
26
let firstHello ;
27
27
let secondHello ;
28
28
let thirdHello ;
29
+ let partial ;
29
30
30
31
beforeEach ( function ( ) {
31
32
firstHello = generateOpMsgBuffer ( response ) ;
32
33
secondHello = generateOpMsgBuffer ( response ) ;
33
34
thirdHello = generateOpMsgBuffer ( response ) ;
35
+ partial = Buffer . alloc ( 5 ) ;
36
+ partial . writeInt32LE ( 100 , 0 ) ;
34
37
} ) ;
35
38
36
39
it ( 'only reads the last message in the buffer' , async function ( ) {
@@ -46,6 +49,22 @@ describe('MessageStream', function () {
46
49
// Make sure there is nothing left in the buffer.
47
50
expect ( messageStream . buffer . length ) . to . equal ( 0 ) ;
48
51
} ) ;
52
+
53
+ it ( 'does not read partial messages' , async function ( ) {
54
+ const inputStream = bufferToStream (
55
+ Buffer . concat ( [ firstHello , secondHello , thirdHello , partial ] )
56
+ ) ;
57
+ const messageStream = new MessageStream ( ) ;
58
+ messageStream . isStreamingProtocol = true ;
59
+
60
+ inputStream . pipe ( messageStream ) ;
61
+ const messages = await once ( messageStream , 'message' ) ;
62
+ const msg = messages [ 0 ] ;
63
+ msg . parse ( ) ;
64
+ expect ( msg ) . to . have . property ( 'documents' ) . that . deep . equals ( [ response ] ) ;
65
+ // Make sure the buffer wasn't read to the end.
66
+ expect ( messageStream . buffer . length ) . to . equal ( 5 ) ;
67
+ } ) ;
49
68
} ) ;
50
69
51
70
context ( 'when the stream is not using the streaming protocol' , function ( ) {
@@ -62,52 +81,48 @@ describe('MessageStream', function () {
62
81
thirdHello = generateOpMsgBuffer ( response ) ;
63
82
} ) ;
64
83
65
- it ( 'reads all messages in the buffer' , function ( done ) {
84
+ it ( 'reads all messages in the buffer' , async function ( ) {
66
85
const inputStream = bufferToStream ( Buffer . concat ( [ firstHello , secondHello , thirdHello ] ) ) ;
67
86
const messageStream = new MessageStream ( ) ;
68
87
69
- messageStream . on ( 'message' , msg => {
88
+ inputStream . pipe ( messageStream ) ;
89
+ for await ( const messages of on ( messageStream , 'message' ) ) {
70
90
messageCount ++ ;
91
+ const msg = messages [ 0 ] ;
71
92
msg . parse ( ) ;
72
93
expect ( msg ) . to . have . property ( 'documents' ) . that . deep . equals ( [ response ] ) ;
73
94
// Test will not complete until 3 messages processed.
74
95
if ( messageCount === 3 ) {
75
- done ( ) ;
96
+ return ;
76
97
}
77
- } ) ;
78
-
79
- inputStream . pipe ( messageStream ) ;
98
+ }
80
99
} ) ;
81
100
} ) ;
82
101
83
102
context ( 'when the messages are invalid' , function ( ) {
84
103
context ( 'when the message size is negative' , function ( ) {
85
- it ( 'emits an error' , function ( done ) {
104
+ it ( 'emits an error' , async function ( ) {
86
105
const inputStream = bufferToStream ( Buffer . from ( 'ffffffff' , 'hex' ) ) ;
87
106
const messageStream = new MessageStream ( ) ;
88
107
89
- messageStream . on ( 'error' , err => {
90
- expect ( err ) . to . have . property ( 'message' ) . that . equals ( 'Invalid message size: -1' ) ;
91
- done ( ) ;
92
- } ) ;
93
-
94
108
inputStream . pipe ( messageStream ) ;
109
+ const errors = await once ( messageStream , 'error' )
110
+ const err = errors [ 0 ] ;
111
+ expect ( err ) . to . have . property ( 'message' ) . that . equals ( 'Invalid message size: -1' ) ;
95
112
} ) ;
96
113
} ) ;
97
114
98
115
context ( 'when the message size exceeds the bson maximum' , function ( ) {
99
- it ( 'emits an error' , function ( done ) {
116
+ it ( 'emits an error' , async function ( ) {
100
117
const inputStream = bufferToStream ( Buffer . from ( '01000004' , 'hex' ) ) ;
101
118
const messageStream = new MessageStream ( ) ;
102
119
103
- messageStream . on ( 'error' , err => {
104
- expect ( err )
105
- . to . have . property ( 'message' )
106
- . that . equals ( 'Invalid message size: 67108865, max allowed: 67108864' ) ;
107
- done ( ) ;
108
- } ) ;
109
-
110
120
inputStream . pipe ( messageStream ) ;
121
+ const errors = await once ( messageStream , 'error' ) ;
122
+ const err = errors [ 0 ] ;
123
+ expect ( err )
124
+ . to . have . property ( 'message' )
125
+ . that . equals ( 'Invalid message size: 67108865, max allowed: 67108864' ) ;
111
126
} ) ;
112
127
} ) ;
113
128
} ) ;
0 commit comments