-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
SocketStreamTest.class.st
278 lines (230 loc) · 8.43 KB
/
SocketStreamTest.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"
SUnit tests for socket streams
"
Class {
#name : 'SocketStreamTest',
#superclass : 'ClassTestCase',
#instVars : [
'clientStream',
'serverStream'
],
#category : 'Network-Tests-Kernel',
#package : 'Network-Tests',
#tag : 'Kernel'
}
{ #category : 'coverage' }
SocketStreamTest >> classToBeTested [
^ SocketStream
]
{ #category : 'stream protocol' }
SocketStreamTest >> closeServerAndSendOnceFromClient [
serverStream close.
"The first send after the other end is closed will not have problems as long as it fits within the send buffer."
self
shouldnt: [
clientStream
nextPutAll: 'A line of text';
flush ]
raise: NetworkError.
"Wait for the state of the underlying socket to update--normally this happens near-instantly,
but when running tests in a batch of other sockets-related tests, e.g. in CI, often a subsequent send
will still succeed without the wait, causing the test to fail."
(Delay forMilliseconds: 100) wait
]
{ #category : 'running' }
SocketStreamTest >> setUp [
| listener clientSocket serverSocket |
super setUp.
listener := Socket newTCP.
[listener listenOn: 0 backlogSize: 4.
clientSocket := Socket newTCP.
clientSocket connectTo: #[127 0 0 1] port: listener localPort.
clientSocket waitForConnectionFor: 1.
self assert: clientSocket isConnected.
serverSocket := listener waitForAcceptFor: 1.
self assert: serverSocket isConnected.
] ensure:[listener destroy].
clientStream := SocketStream on: clientSocket.
serverStream := SocketStream on: serverSocket
]
{ #category : 'running' }
SocketStreamTest >> tearDown [
clientStream ifNotNil:[clientStream destroy].
serverStream ifNotNil:[serverStream destroy].
super tearDown
]
{ #category : 'stream protocol' }
SocketStreamTest >> testFlushLargeMessageOtherEndClosed [
"A large send will be broken into chunks and fail once the TCP send buffer
is full. Ensure we actually exceed the size of the send buffer, but also try
to reduce it first so we don't need an excessively large message. Some platforms
(Linux) have minimum values for this setting that prevent us from relying on
lowering it alone. Attempt to set a 65KiB buffer and double whatever we get."
| bufferSize |
clientStream socket setOption: 'SO_SNDBUF' value: 2 ** 16.
bufferSize := (clientStream socket getOption: 'SO_SNDBUF') second.
serverStream close.
self
should: [
clientStream
nextPutAll: (String new: bufferSize * 2 withAll: $a);
flush ]
raise: ConnectionClosed
]
{ #category : 'stream protocol' }
SocketStreamTest >> testFlushOtherEndClosed [
"Ensure that #flush properly raises an error when called when the other end is closed."
self closeServerAndSendOnceFromClient.
self
should: [
clientStream
nextPutAll: 'Another line of text';
flush ]
raise: ConnectionClosed
]
{ #category : 'stream protocol' }
SocketStreamTest >> testNextIntoClose [
"Ensure that #next:into: will function properly when the connection is closed"
clientStream nextPutAll:'A line of text'; flush.
[(Delay forMilliseconds: 100) wait.
clientStream close] fork.
self assert: (serverStream next: 100 into: (String new: 100) startingAt: 1)
equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testNextIntoCloseNonSignaling [
"Ensure that #next:into: will function properly when the connection is closed"
serverStream shouldSignal: false.
clientStream nextPutAll:'A line of text'; flush.
[(Delay forMilliseconds: 100) wait.
clientStream close] fork.
self assert: (serverStream next: 100 into: (String new: 100) startingAt: 1)
equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testNextPutAllFlushOtherEndClosed [
"#nextPutAllFlush: does its own error handling, so needs to be tested separately."
self closeServerAndSendOnceFromClient.
self
should: [ clientStream nextPutAllFlush: 'Another line of text' ]
raise: ConnectionClosed
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpTo [
"Tests correct behavior of #upTo:"
clientStream nextPutAll: 'A line of text', String cr, 'with more text'; flush.
self assert: (serverStream upTo: Character cr) equals: 'A line of text'.
[(Delay forSeconds: 1) wait.
clientStream nextPutAll: String cr; flush] fork.
self assert: (serverStream upTo: Character cr) equals: 'with more text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAfterCloseNonSignaling [
"Tests correct behavior of #upToAll"
| resp |
clientStream nextPutAll: 'A line of text'.
clientStream close.
serverStream shouldSignal: false.
resp := serverStream upTo: Character cr.
self assert: resp equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAfterCloseSignaling [
"Tests correct behavior of #upToAll"
clientStream nextPutAll:'A line of text'.
clientStream close.
self should: [serverStream upTo: Character cr] raise: ConnectionClosed
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAll [
"Tests correct behavior of #upToAll"
clientStream nextPutAll: 'A line of text', String crlf, 'with more text'; flush.
self assert: (serverStream upToAll: String crlf) equals: 'A line of text'.
[(Delay forSeconds: 1) wait.
clientStream nextPutAll: String crlf; flush] fork.
self assert: (serverStream upToAll: String crlf) equals: 'with more text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAllAfterCloseNonSignaling [
"Tests correct behavior of #upToAll"
| resp |
clientStream nextPutAll: 'A line of text'.
clientStream close.
serverStream shouldSignal: false.
resp := serverStream upToAll: String crlf.
self assert: resp equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAllAfterCloseSignaling [
"Tests correct behavior of #upToAll"
clientStream nextPutAll:'A line of text'.
clientStream close.
self should: [serverStream upToAll: String crlf] raise: ConnectionClosed
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAllLimit [
"Tests correct behavior of #upToAll:limit:"
clientStream nextPutAll:'A line of text'; flush.
self assert: (serverStream upToAll: String crlf limit: 5) equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToAllTimeout [
"Tests correct behavior of #upToAll"
clientStream nextPutAll: 'A line of text'.
serverStream timeout: 1.
self should: [ serverStream upToAll: String crlf ] raise: ConnectionTimedOut
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToEndClose [
"Ensure that #upToEnd will function properly when the connection is closed"
clientStream nextPutAll:'A line of text'; flush.
[(Delay forMilliseconds: 100) wait.
clientStream close] fork.
self assert: (serverStream upToEnd)
equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToEndCloseNonSignaling [
"Ensure that #upToEnd will function properly when the connection is closed"
serverStream shouldSignal: false.
clientStream nextPutAll:'A line of text'; flush.
[(Delay forMilliseconds: 100) wait.
clientStream close] fork.
self assert: (serverStream upToEnd)
equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToMax [
"Tests correct behavior of #upToAll:max:"
clientStream nextPutAll:'A line of text'; flush.
self assert: (serverStream upTo: Character cr limit: 5) equals: 'A line of text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToSmallInteger [
"Tests correct behavior of #upTo:"
clientStream nextPutAll: 'A line of text', String cr, 'with more text'; flush.
self assert: (serverStream upTo: Character cr) equals: 'A line of text'.
[(Delay forSeconds: 1) wait.
clientStream nextPutAll: String cr; flush] fork.
self assert: (serverStream upTo: 13) equals: 'with more text'
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToTimeout [
"Tests correct behavior of #upToAll"
clientStream nextPutAll: 'A line of text'.
serverStream timeout: 1.
self should: [serverStream upTo: Character cr] raise: ConnectionTimedOut
]
{ #category : 'stream protocol' }
SocketStreamTest >> testUpToWithByteArray [
"Tests correct behavior of #upTo:"
|byteArray|
byteArray := ('A line of text', String cr, 'with more text') asByteArray.
clientStream binary.
serverStream binary.
clientStream nextPutAll: byteArray; flush.
self assert: (serverStream upTo: Character cr) asString equals: 'A line of text'.
[(Delay forSeconds: 1) wait.
clientStream nextPut: Character cr; flush] fork.
self assert: (serverStream upTo: Character cr) asString equals: 'with more text'
]