File tree 2 files changed +65
-3
lines changed
2 files changed +65
-3
lines changed Original file line number Diff line number Diff line change @@ -140,7 +140,6 @@ def stream(body)
140
140
@input . write ( chunk )
141
141
end
142
142
rescue => error
143
- raise
144
143
ensure
145
144
@input . close_write ( error )
146
145
end
Original file line number Diff line number Diff line change 190
190
stream . write ( chunk )
191
191
end
192
192
rescue => error
193
+ ensure
193
194
stream . close ( error )
194
195
end
195
196
end
215
216
end
216
217
end
217
218
219
+ body . stream ( input )
220
+
218
221
expect do
219
- body . stream ( input )
222
+ body . read
220
223
end . to raise_exception ( RuntimeError , message : be =~ /Oh no!/ )
224
+ end
225
+ end
226
+
227
+ with "streaming in a different task" do
228
+ let ( :block ) do
229
+ proc do |stream |
230
+ while chunk = stream . read_partial
231
+ stream . write ( chunk )
232
+ end
233
+ rescue => error
234
+ ensure
235
+ stream . close ( error )
236
+ end
237
+ end
238
+
239
+ let ( :input ) { Protocol ::HTTP ::Body ::Writable . new }
240
+ let ( :output ) { Protocol ::HTTP ::Body ::Writable . new }
241
+
242
+ before do
243
+ parent = Async ::Task . current
244
+
245
+ @input_task = parent . async do
246
+ body . stream ( input )
247
+ end
248
+
249
+ @output_task = parent . async do
250
+ while chunk = body . read
251
+ output . write ( chunk )
252
+ end
253
+ rescue => error
254
+ ensure
255
+ output . close_write ( error )
256
+ end
257
+ end
258
+
259
+ after do
260
+ @input_task &.wait
261
+ @output_task &.wait
262
+ end
263
+
264
+ it "can stream a chunk" do
265
+ input . write ( "Hello" )
266
+ input . close_write
267
+ expect ( output . read ) . to be == "Hello"
268
+ end
269
+
270
+ it "can stream multiple chunks" do
271
+ input . write ( "Hello" )
272
+ input . write ( " " )
273
+ input . write ( "World" )
274
+ input . close_write
275
+
276
+ expect ( output . read ) . to be == "Hello"
277
+ expect ( output . read ) . to be == " "
278
+ expect ( output . read ) . to be == "World"
279
+ end
280
+
281
+ it "can stream an error" do
282
+ input . write ( "Hello" )
283
+ input . close_write ( RuntimeError . new ( "Oh no!" ) )
221
284
222
285
expect do
223
- body . read
286
+ output . read
224
287
end . to raise_exception ( RuntimeError , message : be =~ /Oh no!/ )
225
288
end
226
289
end
You can’t perform that action at this time.
0 commit comments