Skip to content

Commit da19bf5

Browse files
authored
Port Wasmtime's async-closed-stream test (#569)
1 parent 870a743 commit da19bf5

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

test/async/closed-stream.wast

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
;; This test contains two components $C and $D that test that if the writable side
2+
;; of a stream is dropped, the other side registers a STREAM DROPPED status
3+
;; when attempting to read from the stream.
4+
(component definition $Tester
5+
;; Creates a stream and keeps a handle to the writable end of it.
6+
(component $C
7+
(core module $Memory (memory (export "mem") 1))
8+
(core instance $memory (instantiate $Memory))
9+
(core module $CM
10+
(import "" "mem" (memory 1))
11+
(import "" "stream.new" (func $stream.new (result i64)))
12+
(import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))
13+
(import "" "stream.drop-writable" (func $stream.drop-writable (param i32)))
14+
15+
;; Store the writable end of a stream
16+
(global $sw (mut i32) (i32.const 0))
17+
18+
;; Create a new stream, return the readable end to the caller
19+
(func $start-stream (export "start-stream") (result i32)
20+
(local $ret64 i64)
21+
(local.set $ret64 (call $stream.new))
22+
(global.set $sw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32))))
23+
(i32.wrap_i64 (local.get $ret64))
24+
)
25+
26+
;; Drop the writable end of a stream
27+
(func $drop-writable (export "drop-writable")
28+
(call $stream.drop-writable (global.get $sw))
29+
)
30+
)
31+
(type $ST (stream u8))
32+
(canon stream.new $ST (core func $stream.new))
33+
(canon stream.write $ST async (memory $memory "mem") (core func $stream.write))
34+
(canon stream.drop-writable $ST (core func $stream.drop-writable))
35+
(core instance $cm (instantiate $CM (with "" (instance
36+
(export "mem" (memory $memory "mem"))
37+
(export "stream.new" (func $stream.new))
38+
(export "stream.write" (func $stream.write))
39+
(export "stream.drop-writable" (func $stream.drop-writable))
40+
))))
41+
(func (export "start-stream") (result (stream u8)) (canon lift (core func $cm "start-stream")))
42+
(func (export "drop-writable") (canon lift (core func $cm "drop-writable")))
43+
)
44+
45+
;; Gets a readable stream from component $C and calls operations on it.
46+
(component $D
47+
(import "c" (instance $c
48+
(export "start-stream" (func (result (stream u8))))
49+
(export "drop-writable" (func))
50+
))
51+
52+
(core module $Memory (memory (export "mem") 1))
53+
(core instance $memory (instantiate $Memory))
54+
(core module $Core
55+
(import "" "mem" (memory 1))
56+
(import "" "stream.new" (func $stream.new (result i64)))
57+
(import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))
58+
(import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))
59+
(import "" "stream.drop-writable" (func $stream.drop-writable (param i32)))
60+
(import "" "start-stream" (func $start-stream (result i32)))
61+
(import "" "drop-writable" (func $drop-writable))
62+
63+
(func (export "read-from-closed-stream")
64+
(local $ret i32) (local $sr i32)
65+
66+
;; call 'start-stream' to get the stream we'll be working with
67+
(local.set $sr (call $start-stream))
68+
(if (i32.ne (i32.const 1) (local.get $sr))
69+
(then unreachable))
70+
71+
;; drop the writable end and then attempt to read from it
72+
(call $drop-writable)
73+
(local.set $ret (call $stream.read (local.get $sr) (i32.const 8) (i32.const 4)))
74+
(if (i32.ne (i32.const 1 (; DROPPED ;)) (local.get $ret))
75+
(then unreachable))
76+
)
77+
)
78+
(type $ST (stream u8))
79+
(canon stream.new $ST (core func $stream.new))
80+
(canon stream.read $ST async (memory $memory "mem") (core func $stream.read))
81+
(canon stream.write $ST async (memory $memory "mem") (core func $stream.write))
82+
(canon stream.drop-writable $ST (core func $stream.drop-writable))
83+
(canon lower (func $c "start-stream") (core func $start-stream'))
84+
(canon lower (func $c "drop-writable") (core func $drop-writable'))
85+
(core instance $core (instantiate $Core (with "" (instance
86+
(export "mem" (memory $memory "mem"))
87+
(export "stream.new" (func $stream.new))
88+
(export "stream.read" (func $stream.read))
89+
(export "stream.write" (func $stream.write))
90+
(export "stream.drop-writable" (func $stream.drop-writable))
91+
(export "start-stream" (func $start-stream'))
92+
(export "drop-writable" (func $drop-writable'))
93+
))))
94+
(func (export "read-from-closed-stream") (canon lift (core func $core "read-from-closed-stream")))
95+
)
96+
(instance $c (instantiate $C))
97+
(instance $d (instantiate $D (with "c" (instance $c))))
98+
(func (export "read-from-closed-stream") (alias export $d "read-from-closed-stream"))
99+
)
100+
101+
(component instance $new-tester-instance $Tester)
102+
(invoke "read-from-closed-stream")

0 commit comments

Comments
 (0)