@@ -64,6 +64,29 @@ local function strem(ch, n, m)
6464 return n , get_bin_by ( (ch ):rep (n ), m )
6565end
6666
67+ local function Stream (ch , n , m )
68+ local size , reader
69+
70+ local _stream = {}
71+
72+ function _stream :read (...)
73+ _stream .called_ctx = self
74+ _stream .called_co = coroutine.running ()
75+ return reader (... )
76+ end
77+
78+ function _stream :size ()
79+ return size
80+ end
81+
82+ function _stream :reset ()
83+ size , reader = strem (ch , n , m )
84+ return self
85+ end
86+
87+ return _stream :reset ()
88+ end
89+
6790local ENABLE = true
6891
6992local _ENV = TEST_CASE ' write_callback' if ENABLE then
@@ -165,6 +188,31 @@ function test_write_pass_03()
165188 assert_equal (c , c :perform ())
166189end
167190
191+ function test_write_coro ()
192+ local co1 , co2
193+ local called
194+
195+ co1 = coroutine.create (function ()
196+ c = assert (curl .easy {
197+ url = url ;
198+ writefunction = function ()
199+ called = coroutine.running ()
200+ return true
201+ end
202+ })
203+ coroutine.yield ()
204+ end )
205+
206+ co2 = coroutine.create (function ()
207+ assert_equal (c , c :perform ())
208+ end )
209+
210+ coroutine.resume (co1 )
211+ coroutine.resume (co2 )
212+
213+ assert_equal (co2 , called )
214+ end
215+
168216end
169217
170218local _ENV = TEST_CASE ' progress_callback' if ENABLE then
@@ -380,7 +428,7 @@ local _ENV = TEST_CASE'read_stream_callback' if ENABLE and is_curl_ge(7,30,0) th
380428
381429local url = " http://httpbin.org/post"
382430
383- local c , f , t
431+ local m , c , f , t
384432
385433local function json_data ()
386434 return json .decode (table.concat (t ))
@@ -399,17 +447,86 @@ end
399447function teardown ()
400448 if f then f :free () end
401449 if c then c :close () end
402- t , f , c = nil
450+ if m then m :close () end
451+ t , f , c , m = nil
403452end
404453
405454function test ()
406455 assert_equal (f , f :add_stream (' SSSSS' , strem (' X' , 128 , 13 )))
407456 assert_equal (c , c :setopt_httppost (f ))
457+
458+ -- should be called only stream callback
459+ local read_called
460+ assert_equal (c , c :setopt_readfunction (function ()
461+ read_called = true
462+ end ))
463+
464+ assert_equal (c , c :perform ())
465+
466+ assert_nil (read_called )
467+
468+ assert_equal (200 , c :getinfo_response_code ())
469+ local data = assert_table (json_data ())
470+ assert_table (data .form )
471+ assert_equal ((' X' ):rep (128 ), data .form .SSSSS )
472+ end
473+
474+ function test_object ()
475+ local s = Stream (' X' , 128 , 13 )
476+
477+ assert_equal (f , f :add_stream (' SSSSS' , s :size (), s ))
478+ assert_equal (c , c :setopt_httppost (f ))
408479 assert_equal (c , c :perform ())
480+
481+ assert_equal (s , s .called_ctx )
482+
483+ assert_equal (200 , c :getinfo_response_code ())
484+ local data = assert_table (json_data ())
485+ assert_table (data .form )
486+ assert_equal ((' X' ):rep (128 ), data .form .SSSSS )
487+ end
488+
489+ function test_co_multi ()
490+ local s = Stream (' X' , 128 , 13 )
491+ assert_equal (f , f :add_stream (' SSSSS' , s :size (), s ))
492+ assert_equal (c , c :setopt_httppost (f ))
493+
494+ m = assert (scurl .multi ())
495+ assert_equal (m , m :add_handle (c ))
496+
497+ co = coroutine.create (function ()
498+ while 1 == m :perform () do end
499+ end )
500+
501+ coroutine.resume (co )
502+
503+ assert_equal (co , s .called_co )
504+
505+ assert_equal (200 , c :getinfo_response_code ())
506+ local data = assert_table (json_data ())
507+ assert_table (data .form )
508+ assert_equal ((' X' ):rep (128 ), data .form .SSSSS )
509+ end
510+
511+ function test_co ()
512+ local s = Stream (' X' , 128 , 13 )
513+
514+ assert_equal (f , f :add_stream (' SSSSS' , s :size (), s ))
515+ assert_equal (c , c :setopt_httppost (f ))
516+
517+ co = coroutine.create (function ()
518+ assert_equal (c , c :perform ())
519+ end )
520+
521+ coroutine.resume (co )
522+
523+ assert_equal (co , s .called_co )
524+
409525 assert_equal (200 , c :getinfo_response_code ())
410526 local data = assert_table (json_data ())
411527 assert_table (data .form )
412528 assert_equal ((' X' ):rep (128 ), data .form .SSSSS )
529+
413530end
414531
415532function test_abort_01 ()
0 commit comments