@@ -2,13 +2,12 @@ import { strict as assert } from 'node:assert';
2
2
import { spy } from 'sinon' ;
3
3
import { once } from 'node:events' ;
4
4
import RedisSocket , { RedisSocketOptions } from './socket' ;
5
+ import testUtils , { GLOBAL } from '../test-utils' ;
6
+ import { setTimeout } from 'timers/promises' ;
5
7
6
8
describe ( 'Socket' , ( ) => {
7
9
function createSocket ( options : RedisSocketOptions ) : RedisSocket {
8
- const socket = new RedisSocket (
9
- ( ) => Promise . resolve ( ) ,
10
- options
11
- ) ;
10
+ const socket = new RedisSocket ( ( ) => Promise . resolve ( ) , options ) ;
12
11
13
12
socket . on ( 'error' , ( ) => {
14
13
// ignore errors
@@ -84,4 +83,66 @@ describe('Socket', () => {
84
83
assert . equal ( socket . isOpen , false ) ;
85
84
} ) ;
86
85
} ) ;
86
+
87
+ describe ( 'socketTimeout' , ( ) => {
88
+ const timeout = 50 ;
89
+ testUtils . testWithClient (
90
+ 'should timeout with positive socketTimeout values' ,
91
+ async client => {
92
+ let timedOut = false ;
93
+
94
+ assert . equal ( client . isReady , true , 'client.isReady' ) ;
95
+ assert . equal ( client . isOpen , true , 'client.isOpen' ) ;
96
+
97
+ client . on ( 'error' , err => {
98
+ assert . equal (
99
+ err . message ,
100
+ `Socket timeout timeout. Expecting data, but didn't receive any in ${ timeout } ms.`
101
+ ) ;
102
+
103
+ assert . equal ( client . isReady , false , 'client.isReady' ) ;
104
+
105
+ // This is actually a bug with the onSocketError implementation,
106
+ // the client should be closed before the error is emitted
107
+ process . nextTick ( ( ) => {
108
+ assert . equal ( client . isOpen , false , 'client.isOpen' ) ;
109
+ } ) ;
110
+
111
+ timedOut = true ;
112
+ } ) ;
113
+ await setTimeout ( timeout * 2 ) ;
114
+ if ( ! timedOut ) assert . fail ( 'Should have timed out by now' ) ;
115
+ } ,
116
+ {
117
+ ...GLOBAL . SERVERS . OPEN ,
118
+ clientOptions : {
119
+ socket : {
120
+ socketTimeout : timeout
121
+ }
122
+ }
123
+ }
124
+ ) ;
125
+
126
+ testUtils . testWithClient (
127
+ 'should not timeout with undefined socketTimeout' ,
128
+ async client => {
129
+
130
+ assert . equal ( client . isReady , true , 'client.isReady' ) ;
131
+ assert . equal ( client . isOpen , true , 'client.isOpen' ) ;
132
+
133
+ client . on ( 'error' , err => {
134
+ assert . fail ( 'Should not have timed out or errored in any way' ) ;
135
+ } ) ;
136
+ await setTimeout ( 100 ) ;
137
+ } ,
138
+ {
139
+ ...GLOBAL . SERVERS . OPEN ,
140
+ clientOptions : {
141
+ socket : {
142
+ socketTimeout : undefined
143
+ }
144
+ }
145
+ }
146
+ ) ;
147
+ } ) ;
87
148
} ) ;
0 commit comments