@@ -3,7 +3,6 @@ const { JsonRpcEngine } = require('json-rpc-engine');
33const { createStreamMiddleware, createEngineStream } = require ( '../dist' ) ;
44
55test ( 'middleware - raw test' , ( t ) => {
6-
76 const jsonRpcConnection = createStreamMiddleware ( ) ;
87 const req = { id : 1 , jsonrpc : '2.0' , method : 'test' } ;
98 const initRes = { id : 1 , jsonrpc : '2.0' } ;
@@ -16,17 +15,21 @@ test('middleware - raw test', (t) => {
1615 } ) ;
1716
1817 // run middleware, expect end fn to be called
19- jsonRpcConnection . middleware ( req , initRes , ( ) => {
20- t . fail ( 'should not call next' ) ;
21- } , ( err ) => {
22- t . notOk ( err , 'should not error' ) ;
23- t . deepEqual ( initRes , res , 'got the expected response' ) ;
24- t . end ( ) ;
25- } ) ;
18+ jsonRpcConnection . middleware (
19+ req ,
20+ initRes ,
21+ ( ) => {
22+ t . fail ( 'should not call next' ) ;
23+ } ,
24+ ( err ) => {
25+ t . notOk ( err , 'should not error' ) ;
26+ t . deepEqual ( initRes , res , 'got the expected response' ) ;
27+ t . end ( ) ;
28+ } ,
29+ ) ;
2630} ) ;
2731
2832test ( 'engine to stream - raw test' , ( t ) => {
29-
3033 const engine = new JsonRpcEngine ( ) ;
3134 engine . push ( ( _req , res , _next , end ) => {
3235 res . result = 'test' ;
@@ -51,7 +54,6 @@ test('engine to stream - raw test', (t) => {
5154} ) ;
5255
5356test ( 'middleware and engine to stream' , ( t ) => {
54-
5557 // create guest
5658 const engineA = new JsonRpcEngine ( ) ;
5759 const jsonRpcConnection = createStreamMiddleware ( ) ;
@@ -67,9 +69,7 @@ test('middleware and engine to stream', (t) => {
6769 // connect both
6870 const clientSideStream = jsonRpcConnection . stream ;
6971 const hostSideStream = createEngineStream ( { engine : engineB } ) ;
70- clientSideStream
71- . pipe ( hostSideStream )
72- . pipe ( clientSideStream ) ;
72+ clientSideStream . pipe ( hostSideStream ) . pipe ( clientSideStream ) ;
7373
7474 // request and expected result
7575 const req = { id : 1 , jsonrpc : '2.0' , method : 'test' } ;
@@ -115,3 +115,14 @@ test('server notification in stream', (t) => {
115115
116116 engine . emit ( 'notification' , notif ) ;
117117} ) ;
118+
119+ test ( 'clean up listener' , ( t ) => {
120+ const n = 10 ;
121+ const engine = new JsonRpcEngine ( ) ;
122+ for ( let i = 0 ; i < n ; i ++ ) {
123+ const stream = createEngineStream ( { engine } ) ;
124+ stream . end ( ) ;
125+ }
126+ t . equal ( engine . listenerCount ( ) , 0 ) ;
127+ t . end ( ) ;
128+ } ) ;
0 commit comments