@@ -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,51 @@ 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
+ assert . equal ( client . isOpen , true ) ;
94
+ client . on ( 'error' , err => {
95
+ assert . equal (
96
+ err . message ,
97
+ `Socket timeout timeout. Expecting data, but didn't receive any in ${ timeout } ms.`
98
+ ) ;
99
+ timedOut = true ;
100
+ } ) ;
101
+ await setTimeout ( timeout ! * 2 ) ;
102
+ if ( ! timedOut ) assert . fail ( 'Should have timed out' ) ;
103
+ } ,
104
+ {
105
+ ...GLOBAL . SERVERS . OPEN ,
106
+ clientOptions : {
107
+ socket : {
108
+ socketTimeout : timeout
109
+ }
110
+ }
111
+ }
112
+ ) ;
113
+
114
+ testUtils . testWithClient (
115
+ 'should not timeout with undefined socketTimeout' ,
116
+ async client => {
117
+ assert . equal ( client . isOpen , true ) ;
118
+ client . on ( 'error' , err => {
119
+ assert . fail ( 'Should not have timed out or errored in any way' ) ;
120
+ } ) ;
121
+ await setTimeout ( 100 ) ;
122
+ } ,
123
+ {
124
+ ...GLOBAL . SERVERS . OPEN ,
125
+ clientOptions : {
126
+ socket : {
127
+ socketTimeout : undefined
128
+ }
129
+ }
130
+ }
131
+ ) ;
132
+ } ) ;
87
133
} ) ;
0 commit comments