|
| 1 | +;; This test contains two components $C and $D that test cancelling reads |
| 2 | +;; and writes in the presence and absence of partial reads/writes. |
| 3 | +;; |
| 4 | +;; $C exports a function 'start-stream' that creates and holds onto a writable |
| 5 | +;; stream in the global $sw as well as various operations that operate on $sw. |
| 6 | +;; $D calls $C.start-stream to get the readable end and then drives the test. |
| 7 | +(component |
| 8 | + (component $C |
| 9 | + (core module $Memory (memory (export "mem") 1)) |
| 10 | + (core instance $memory (instantiate $Memory)) |
| 11 | + (core module $CM |
| 12 | + (import "" "mem" (memory 1)) |
| 13 | + (import "" "task.return" (func $task.return (param i32))) |
| 14 | + (import "" "stream.new" (func $stream.new (result i64))) |
| 15 | + (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32))) |
| 16 | + (import "" "stream.cancel-write" (func $stream.cancel-write (param i32) (result i32))) |
| 17 | + (import "" "stream.close-writable" (func $stream.close-writable (param i32))) |
| 18 | + |
| 19 | + (global $sw (mut i32) (i32.const 0)) |
| 20 | + |
| 21 | + (func $start-stream (export "start-stream") (result i32) |
| 22 | + ;; create a new stream, return the readable end to the caller |
| 23 | + (local $ret64 i64) |
| 24 | + (local.set $ret64 (call $stream.new)) |
| 25 | + (global.set $sw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32)))) |
| 26 | + (i32.wrap_i64 (local.get $ret64)) |
| 27 | + ) |
| 28 | + |
| 29 | + (func $write4 (export "write4") |
| 30 | + ;; write 6 bytes into the stream, expecting to rendezvous with a stream.read |
| 31 | + (local $ret i32) |
| 32 | + (i32.store (i32.const 8) (i32.const 0xabcd)) |
| 33 | + (local.set $ret (call $stream.write (global.get $sw) (i32.const 8) (i32.const 4))) |
| 34 | + (if (i32.ne (i32.const 0x40 (; COMPLETED=0 | (4<<4) ;)) (local.get $ret)) |
| 35 | + (then unreachable)) |
| 36 | + ) |
| 37 | + |
| 38 | + (func $write4-and-close (export "write4-and-close") |
| 39 | + (call $write4) |
| 40 | + (call $stream.close-writable (global.get $sw)) |
| 41 | + ) |
| 42 | + |
| 43 | + (func $start-blocking-write (export "start-blocking-write") |
| 44 | + (local $ret i32) |
| 45 | + |
| 46 | + ;; prepare the write buffer |
| 47 | + (i64.store (i32.const 8) (i64.const 0x123456789abcdef)) |
| 48 | + |
| 49 | + ;; start one blocking write and immediately cancel it |
| 50 | + (local.set $ret (call $stream.write (global.get $sw) (i32.const 8) (i32.const 8))) |
| 51 | + (if (i32.ne (i32.const -1 (; BLOCKED ;)) (local.get $ret)) |
| 52 | + (then unreachable)) |
| 53 | + (local.set $ret (call $stream.cancel-write (global.get $sw))) |
| 54 | + (if (i32.ne (i32.const 0x2 (; CANCELLED ;)) (local.get $ret)) |
| 55 | + (then unreachable)) |
| 56 | + |
| 57 | + ;; start a second blockign write and leave it pending |
| 58 | + (local.set $ret (call $stream.write (global.get $sw) (i32.const 8) (i32.const 8))) |
| 59 | + (if (i32.ne (i32.const -1 (; BLOCKED ;)) (local.get $ret)) |
| 60 | + (then unreachable)) |
| 61 | + ) |
| 62 | + |
| 63 | + (func $cancel-after-read4 (export "cancel-after-read4") |
| 64 | + (local $ret i32) |
| 65 | + (local.set $ret (call $stream.cancel-write (global.get $sw))) |
| 66 | + (if (i32.ne (i32.const 0x42 (; TODO: CANCELLED=2 | (4<<4) ;)) (local.get $ret)) |
| 67 | + (then unreachable)) |
| 68 | + ) |
| 69 | + ) |
| 70 | + (type $ST (stream u8)) |
| 71 | + (canon task.return (result u32) (core func $task.return)) |
| 72 | + (canon stream.new $ST (core func $stream.new)) |
| 73 | + (canon stream.write $ST async (memory $memory "mem") (core func $stream.write)) |
| 74 | + (canon stream.cancel-write $ST (core func $stream.cancel-write)) |
| 75 | + (canon stream.close-writable $ST (core func $stream.close-writable)) |
| 76 | + (core instance $cm (instantiate $CM (with "" (instance |
| 77 | + (export "mem" (memory $memory "mem")) |
| 78 | + (export "task.return" (func $task.return)) |
| 79 | + (export "stream.new" (func $stream.new)) |
| 80 | + (export "stream.write" (func $stream.write)) |
| 81 | + (export "stream.cancel-write" (func $stream.cancel-write)) |
| 82 | + (export "stream.close-writable" (func $stream.close-writable)) |
| 83 | + )))) |
| 84 | + (func (export "start-stream") (result (stream u8)) (canon lift (core func $cm "start-stream"))) |
| 85 | + (func (export "write4") (canon lift (core func $cm "write4"))) |
| 86 | + (func (export "write4-and-close") (canon lift (core func $cm "write4-and-close"))) |
| 87 | + (func (export "start-blocking-write") (canon lift (core func $cm "start-blocking-write"))) |
| 88 | + (func (export "cancel-after-read4") (canon lift (core func $cm "cancel-after-read4"))) |
| 89 | + ) |
| 90 | + |
| 91 | + (component $D |
| 92 | + (import "c" (instance $c |
| 93 | + (export "start-stream" (func (result (stream u8)))) |
| 94 | + (export "write4" (func)) |
| 95 | + (export "write4-and-close" (func)) |
| 96 | + (export "start-blocking-write" (func)) |
| 97 | + (export "cancel-after-read4" (func)) |
| 98 | + )) |
| 99 | + |
| 100 | + (core module $Memory (memory (export "mem") 1)) |
| 101 | + (core instance $memory (instantiate $Memory)) |
| 102 | + (core module $DM |
| 103 | + (import "" "mem" (memory 1)) |
| 104 | + (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32))) |
| 105 | + (import "" "stream.cancel-read" (func $stream.cancel-read (param i32) (result i32))) |
| 106 | + (import "" "stream.close-readable" (func $stream.close-readable (param i32))) |
| 107 | + (import "" "start-stream" (func $start-stream (result i32))) |
| 108 | + (import "" "write4" (func $write4)) |
| 109 | + (import "" "write4-and-close" (func $write4-and-close)) |
| 110 | + (import "" "start-blocking-write" (func $start-blocking-write)) |
| 111 | + (import "" "cancel-after-read4" (func $cancel-after-read4)) |
| 112 | + |
| 113 | + (func $run (export "run") (result i32) |
| 114 | + (local $ret i32) |
| 115 | + (local $sr i32) |
| 116 | + |
| 117 | + ;; call 'start-stream' to get the stream we'll be working with |
| 118 | + (local.set $sr (call $start-stream)) |
| 119 | + (if (i32.ne (i32.const 1) (local.get $sr)) |
| 120 | + (then unreachable)) |
| 121 | + |
| 122 | + ;; start read that will block |
| 123 | + (local.set $ret (call $stream.read (local.get $sr) (i32.const 8) (i32.const 100))) |
| 124 | + (if (i32.ne (i32.const -1 (; BLOCKED;)) (local.get $ret)) |
| 125 | + (then unreachable)) |
| 126 | + |
| 127 | + ;; cancelling it will finish without anything having been written |
| 128 | + (local.set $ret (call $stream.cancel-read (local.get $sr))) |
| 129 | + (if (i32.ne (i32.const 0x2 (; CANCELLED ;)) (local.get $ret)) |
| 130 | + (then unreachable)) |
| 131 | + |
| 132 | + ;; read, block, call $C to write 4 bytes into the buffer, |
| 133 | + ;; then cancel, which should show "4+cancelled" |
| 134 | + (local.set $ret (call $stream.read (local.get $sr) (i32.const 8) (i32.const 100))) |
| 135 | + (if (i32.ne (i32.const -1 (; BLOCKED;)) (local.get $ret)) |
| 136 | + (then unreachable)) |
| 137 | + (call $write4) |
| 138 | + (local.set $ret (call $stream.cancel-read (local.get $sr))) |
| 139 | + (if (i32.ne (i32.const 0x42 (; TODO: CANCELLED=2 | (4<<4) ;)) (local.get $ret)) |
| 140 | + (then unreachable)) |
| 141 | + (if (i32.ne (i32.const 0xabcd) (i32.load (i32.const 8))) |
| 142 | + (then unreachable)) |
| 143 | + |
| 144 | + ;; read, block, call $C to write 4 bytes into the buffer and close, |
| 145 | + ;; then cancel, which should show "4+closed" |
| 146 | + (local.set $ret (call $stream.read (local.get $sr) (i32.const 8) (i32.const 100))) |
| 147 | + (if (i32.ne (i32.const -1 (; BLOCKED;)) (local.get $ret)) |
| 148 | + (then unreachable)) |
| 149 | + (call $write4-and-close) |
| 150 | + (local.set $ret (call $stream.cancel-read (local.get $sr))) |
| 151 | + (if (i32.ne (i32.const 0x41 (; TODO: CANCELLED=2 | (4<<4) ;)) (local.get $ret)) |
| 152 | + (then unreachable)) |
| 153 | + (if (i32.ne (i32.const 0xabcd) (i32.load (i32.const 8))) |
| 154 | + (then unreachable)) |
| 155 | + (call $stream.close-readable (local.get $sr)) |
| 156 | + |
| 157 | + ;; get a new $sr |
| 158 | + (local.set $sr (call $start-stream)) |
| 159 | + (if (i32.ne (i32.const 1) (local.get $sr)) |
| 160 | + (then unreachable)) |
| 161 | + |
| 162 | + ;; start outstanding write in $C, read 4 of it, then call back into $C |
| 163 | + ;; which will cancel and see 4 written. |
| 164 | + (call $start-blocking-write) |
| 165 | + (local.set $ret (call $stream.read (local.get $sr) (i32.const 8) (i32.const 4))) |
| 166 | + (if (i32.ne (i32.const 0x40 (; COMPLETED=0 | (4<<4) ;)) (local.get $ret)) |
| 167 | + (then unreachable)) |
| 168 | + (if (i32.ne (i32.const 0x89abcdef) (i32.load (i32.const 8))) |
| 169 | + (then unreachable)) |
| 170 | + (call $cancel-after-read4) |
| 171 | + |
| 172 | + ;; return 42 to the top-level assert_return |
| 173 | + (i32.const 42) |
| 174 | + ) |
| 175 | + ) |
| 176 | + (type $ST (stream u8)) |
| 177 | + (canon stream.read $ST async (memory $memory "mem") (core func $stream.read)) |
| 178 | + (canon stream.cancel-read $ST (core func $stream.cancel-read)) |
| 179 | + (canon stream.close-readable $ST (core func $stream.close-readable)) |
| 180 | + (canon lower (func $c "start-stream") (core func $start-stream')) |
| 181 | + (canon lower (func $c "write4") (core func $write4')) |
| 182 | + (canon lower (func $c "write4-and-close") (core func $write4-and-close')) |
| 183 | + (canon lower (func $c "start-blocking-write") (core func $start-blocking-write')) |
| 184 | + (canon lower (func $c "cancel-after-read4") (core func $cancel-after-read4')) |
| 185 | + (core instance $dm (instantiate $DM (with "" (instance |
| 186 | + (export "mem" (memory $memory "mem")) |
| 187 | + (export "stream.read" (func $stream.read)) |
| 188 | + (export "stream.cancel-read" (func $stream.cancel-read)) |
| 189 | + (export "stream.close-readable" (func $stream.close-readable)) |
| 190 | + (export "start-stream" (func $start-stream')) |
| 191 | + (export "write4" (func $write4')) |
| 192 | + (export "write4-and-close" (func $write4-and-close')) |
| 193 | + (export "start-blocking-write" (func $start-blocking-write')) |
| 194 | + (export "cancel-after-read4" (func $cancel-after-read4')) |
| 195 | + )))) |
| 196 | + (func (export "run") (result u32) (canon lift (core func $dm "run"))) |
| 197 | + ) |
| 198 | + |
| 199 | + (instance $c (instantiate $C)) |
| 200 | + (instance $d (instantiate $D (with "c" (instance $c)))) |
| 201 | + (func (export "run") (alias export $d "run")) |
| 202 | +) |
| 203 | +(assert_return (invoke "run") (u32.const 42)) |
0 commit comments