Skip to content

Commit 5771bb3

Browse files
committed
add ping-pong Go tests
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent f426ea9 commit 5771bb3

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
test "wit_component/my_test_i"
6+
"wit_component/wit_async"
7+
"wit_component/wit_types"
8+
)
9+
10+
type Unit struct{}
11+
12+
//go:wasmimport [export]wasi:cli/run@0.3.0-rc-2025-09-16 [task-return]run
13+
func taskReturn(result int32)
14+
15+
//go:wasmexport [async-lift]wasi:cli/run@0.3.0-rc-2025-09-16#run
16+
func run() uint32 {
17+
return wit_async.Run(func() {
18+
{
19+
f1 := make(chan *wit_types.FutureReader[string])
20+
f2 := make(chan Unit)
21+
22+
tx, rx := test.MakeFutureString()
23+
go func() {
24+
f1 <- test.Ping(rx, "world")
25+
}()
26+
27+
go func() {
28+
tx.Write("hello")
29+
f2 <- Unit{}
30+
}()
31+
32+
(<-f2)
33+
rx2 := (<-f1)
34+
assertEqual(rx2.Read(), "helloworld")
35+
}
36+
37+
{
38+
f1 := make(chan Unit)
39+
f2 := make(chan Unit)
40+
41+
tx, rx := test.MakeFutureString()
42+
go func() {
43+
assertEqual(test.Pong(rx), "helloworld")
44+
f1 <- Unit{}
45+
}()
46+
47+
go func() {
48+
tx.Write("helloworld")
49+
f2 <- Unit{}
50+
}()
51+
52+
(<-f2)
53+
(<-f1)
54+
}
55+
56+
taskReturn(0)
57+
})
58+
}
59+
60+
//go:wasmexport [callback][async-lift]wasi:cli/run@0.3.0-rc-2025-09-16#run
61+
func callback(event0 uint32, event1 uint32, event2 uint32) uint32 {
62+
return wit_async.Callback(event0, event1, event2)
63+
}
64+
65+
func assertEqual[T comparable](a, b T) {
66+
if a != b {
67+
panic(fmt.Sprintf("%v not equal to %v", a, b))
68+
}
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package export_my_test_i
2+
3+
import (
4+
"wit_component/my_test_i"
5+
. "wit_component/wit_types"
6+
)
7+
8+
func Ping(x *FutureReader[string], y string) *FutureReader[string] {
9+
message := x.Read() + y
10+
tx, rx := my_test_i.MakeFutureString()
11+
go func() {
12+
tx.Write(message)
13+
}()
14+
return rx
15+
}
16+
17+
func Pong(x *FutureReader[string]) string {
18+
return x.Read()
19+
}

0 commit comments

Comments
 (0)