test/test_backend.rb in polyphony-0.53.0 vs test/test_backend.rb in polyphony-0.53.1
- old
+ new
@@ -239,38 +239,121 @@
end
def test_splice
i1, o1 = IO.pipe
i2, o2 = IO.pipe
+ len = nil
spin {
- o2.splice(i1, 1000)
+ len = o2.splice(i1, 1000)
o2.close
}
o1.write('foobar')
result = i2.read
assert_equal 'foobar', result
+ assert_equal 6, len
end
def test_splice_to_eof
i1, o1 = IO.pipe
i2, o2 = IO.pipe
+ len = nil
f = spin {
- o2.splice_to_eof(i1, 1000)
+ len = o2.splice_to_eof(i1, 1000)
o2.close
}
o1.write('foo')
result = i2.readpartial(1000)
assert_equal 'foo', result
o1.write('bar')
result = i2.readpartial(1000)
assert_equal 'bar', result
- ensure
- f.interrupt
+ o1.close
f.await
+ assert_equal 6, len
+ ensure
+ if f.alive?
+ f.interrupt
+ f.await
+ end
+ end
+end
+
+class BackendChainTest < MiniTest::Test
+ def setup
+ super
+ @prev_backend = Thread.current.backend
+ @backend = Polyphony::Backend.new
+ Thread.current.backend = @backend
+ end
+
+ def teardown
+ @backend.finalize
+ Thread.current.backend = @prev_backend
+ end
+
+ def test_simple_write_chain
+ i, o = IO.pipe
+
+ result = Thread.backend.chain(
+ [:write, o, 'hello'],
+ [:write, o, ' world']
+ )
+
+ assert_equal 6, result
+ o.close
+ assert_equal 'hello world', i.read
+ end
+
+ def chunk_header(len)
+ "Content-Length: #{len}\r\n\r\n"
+ end
+
+ def serve_io(from, to)
+ i, o = IO.pipe
+ backend = Thread.current.backend
+ while true
+ len = o.splice(from, 8192)
+ break if len == 0
+
+ backend.chain(
+ [:write, to, chunk_header(len)],
+ [:splice, i, to, len]
+ )
+ end
+ to.close
+ end
+
+ def test_chain_with_splice
+ from_r, from_w = IO.pipe
+ to_r, to_w = IO.pipe
+
+ result = nil
+ f = spin { serve_io(from_r, to_w) }
+
+ from_w << 'Hello world!'
+ from_w.close
+
+ assert_equal "Content-Length: 12\r\n\r\nHello world!", to_r.read
+ end
+
+ def test_invalid_op
+ i, o = IO.pipe
+
+ assert_raises(RuntimeError) {
+ Thread.backend.chain(
+ [:read, o]
+ )
+ }
+
+ assert_raises(TypeError) {
+ Thread.backend.chain(
+ [:write, o]
+ )
+ }
end
end