@@ -59,13 +59,13 @@ describe('NodeChannel', () => {
59
59
} )
60
60
61
61
describe ( '.setupReceiveTimeout()' , ( ) => {
62
- it ( 'should call socket.setTimeout(receiveTimeout)' , ( ) => {
62
+ it ( 'should not call socket.setTimeout(receiveTimeout)' , ( ) => {
63
63
const receiveTimeout = 42
64
64
const channel = createMockedChannel ( true )
65
65
66
66
channel . setupReceiveTimeout ( receiveTimeout )
67
67
68
- expect ( channel . _conn . getCalls ( ) . setTimeout [ 1 ] ) . toEqual ( [ receiveTimeout ] )
68
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 1 )
69
69
} )
70
70
71
71
it ( 'should unsubscribe to the on connect and on timeout created on the create socket' , ( ) => {
@@ -108,6 +108,122 @@ describe('NodeChannel', () => {
108
108
expect ( channel . _conn . getCalls ( ) . off ) . toEqual ( [ ] )
109
109
} )
110
110
} )
111
+
112
+ describe ( '.startReceiveTimeout()' , ( ) => {
113
+ describe ( 'receive timeout is setup' , ( ) => {
114
+ it ( 'should call socket.setTimeout(receiveTimeout) when called first' , ( ) => {
115
+ const { receiveTimeout, channel } = setup ( )
116
+
117
+ channel . startReceiveTimeout ( )
118
+
119
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 2 )
120
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 1 ] ) . toEqual ( [ receiveTimeout ] )
121
+ } )
122
+
123
+ it ( 'should not call socket.setTimeout(receiveTimeout) if stream already started' , ( ) => {
124
+ const { receiveTimeout, channel } = setup ( )
125
+
126
+ // setup
127
+ channel . startReceiveTimeout ( )
128
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 2 )
129
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 1 ] ) . toEqual ( [ receiveTimeout ] )
130
+
131
+ // start again
132
+ channel . startReceiveTimeout ( )
133
+
134
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 2 )
135
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 1 ] ) . toEqual ( [ receiveTimeout ] )
136
+ } )
137
+
138
+ it ( 'should call socket.setTimeout(receiveTimeout) when after stop' , ( ) => {
139
+ const { receiveTimeout, channel } = setup ( )
140
+
141
+ // setup
142
+ channel . startReceiveTimeout ( )
143
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 2 )
144
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 1 ] ) . toEqual ( [ receiveTimeout ] )
145
+ channel . stopReceiveTimeout ( )
146
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 3 )
147
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 2 ] ) . toEqual ( [ 0 ] )
148
+
149
+ // start again
150
+ channel . startReceiveTimeout ( )
151
+
152
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 4 )
153
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 3 ] ) . toEqual ( [ receiveTimeout ] )
154
+ } )
155
+
156
+ function setup ( ) {
157
+ const channel = createMockedChannel ( true )
158
+ const receiveTimeout = 42
159
+ channel . setupReceiveTimeout ( receiveTimeout )
160
+ return { channel, receiveTimeout}
161
+ }
162
+ } )
163
+
164
+ describe ( 'receive timemout is not setup' , ( ) => {
165
+ it ( 'should call not socket.setTimeout(receiveTimeout) when not started' , ( ) => {
166
+ const channel = createMockedChannel ( true )
167
+
168
+ // start again
169
+ channel . startReceiveTimeout ( )
170
+
171
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 1 )
172
+ } )
173
+ } )
174
+ } )
175
+
176
+ describe ( '.stopReceiveTimeout()' , ( ) => {
177
+ describe ( 'when receive timeout is setup' , ( ) => {
178
+ it ( 'should not call socket.setTimeout(0) when not started' , ( ) => {
179
+ const { channel } = setup ( )
180
+
181
+ channel . stopReceiveTimeout ( )
182
+
183
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 1 )
184
+ } )
185
+
186
+ it ( 'should call socket.setTimeout(0) when already started' , ( ) => {
187
+ const { channel } = setup ( )
188
+
189
+ channel . startReceiveTimeout ( )
190
+
191
+ channel . stopReceiveTimeout ( )
192
+
193
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 3 )
194
+ expect ( channel . _conn . getCalls ( ) . setTimeout [ 2 ] ) . toEqual ( [ 0 ] )
195
+ } )
196
+
197
+ it ( 'should not call socket.setTimeout(0) when already stopped' , ( ) => {
198
+ const { channel } = setup ( )
199
+
200
+ channel . startReceiveTimeout ( )
201
+ channel . stopReceiveTimeout ( )
202
+
203
+ channel . stopReceiveTimeout ( )
204
+
205
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 3 )
206
+ } )
207
+
208
+ function setup ( ) {
209
+ const channel = createMockedChannel ( true )
210
+ const receiveTimeout = 42
211
+ channel . setupReceiveTimeout ( receiveTimeout )
212
+ return { channel, receiveTimeout}
213
+ }
214
+ } )
215
+
216
+ describe ( 'when receive timeout is not setup' , ( ) => {
217
+ it ( 'should not call socket.setTimeout(0)' , ( ) => {
218
+ const channel = createMockedChannel ( true )
219
+
220
+ channel . startReceiveTimeout ( )
221
+ channel . stopReceiveTimeout ( )
222
+
223
+ expect ( channel . _conn . getCalls ( ) . setTimeout . length ) . toEqual ( 1 )
224
+ } )
225
+ } )
226
+ } )
111
227
} )
112
228
113
229
function createMockedChannel ( connected , config = { } ) {
0 commit comments