@@ -67,6 +67,33 @@ describe('JsonRpcSocket', () => {
67
67
await expect ( requestPromise ) . to . eventually . be . rejectedWith ( 'timed out' ) ;
68
68
} ) ;
69
69
70
+ it ( 'removes listener if subscription json rpc is rejected ' , async ( ) => {
71
+ wsServer . addListener ( 'connection' , ( socket ) => {
72
+ socket . on ( 'message' , ( dataBuffer : Buffer ) => {
73
+ const request = JSON . parse ( dataBuffer . toString ( ) ) as JsonRpcRequest ;
74
+ // initial response
75
+ const response = createJsonRpcErrorResponse ( request . id , JsonRpcErrorCodes . BadRequest , 'bad request' ) ;
76
+ socket . send ( Buffer . from ( JSON . stringify ( response ) ) ) ;
77
+ } ) ;
78
+ } ) ;
79
+
80
+ const client = await JsonRpcSocket . connect ( 'ws://127.0.0.1:9003' , { responseTimeout : 5 } ) ;
81
+ const requestId = uuidv4 ( ) ;
82
+ const subscribeId = uuidv4 ( ) ;
83
+ const request = createJsonRpcSubscriptionRequest (
84
+ requestId ,
85
+ 'rpc.subscribe.test.method' ,
86
+ { param1 : 'test-param1' , param2 : 'test-param2' } ,
87
+ subscribeId ,
88
+ ) ;
89
+
90
+ const responseListener = ( _response : JsonRpcSuccessResponse ) : void => { }
91
+
92
+ const subscription = await client . subscribe ( request , responseListener ) ;
93
+ expect ( subscription . response . error ) . to . not . be . undefined ;
94
+ expect ( client [ 'socket' ] . listenerCount ( 'message' ) ) . to . equal ( 0 ) ;
95
+ } ) ;
96
+
70
97
it ( 'opens a subscription' , async ( ) => {
71
98
wsServer . addListener ( 'connection' , ( socket ) => {
72
99
socket . on ( 'message' , ( dataBuffer : Buffer ) => {
@@ -230,5 +257,26 @@ describe('JsonRpcSocket', () => {
230
257
expect ( logMessage ) . to . equal ( 'JSON RPC Socket close ws://127.0.0.1:9003' ) ;
231
258
} ) ;
232
259
233
- xit ( 'calls onerror handler' , async ( ) => { } ) ;
260
+ it ( 'calls onerror handler' , async ( ) => {
261
+ // test injected handler
262
+ const onErrorHandler = { onerror : ( ) :void => { } } ;
263
+ const onErrorSpy = sinon . spy ( onErrorHandler , 'onerror' ) ;
264
+ const client = await JsonRpcSocket . connect ( 'ws://127.0.0.1:9003' , { onerror : onErrorHandler . onerror } ) ;
265
+ client [ 'socket' ] . emit ( 'error' , 'some error' ) ;
266
+
267
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ; // wait for close event to arrive
268
+ expect ( onErrorSpy . callCount ) . to . equal ( 1 , 'error' ) ;
269
+
270
+ // test default logger
271
+ const logInfoSpy = sinon . spy ( log , 'error' ) ;
272
+ const defaultClient = await JsonRpcSocket . connect ( 'ws://127.0.0.1:9003' ) ;
273
+ defaultClient [ 'socket' ] . emit ( 'error' , 'some error' ) ;
274
+
275
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5 ) ) ; // wait for close event to arrive
276
+ expect ( logInfoSpy . callCount ) . to . equal ( 1 , 'log' ) ;
277
+
278
+ // extract log message from argument
279
+ const logMessage :string = logInfoSpy . args [ 0 ] [ 0 ] ! ;
280
+ expect ( logMessage ) . to . equal ( 'JSON RPC Socket error ws://127.0.0.1:9003' ) ;
281
+ } ) ;
234
282
} ) ;
0 commit comments