package export_wit_world
import (
test "wit_component/my_test_i"
witTypes "go.bytecodealliance.org/pkg/wit/types"
)
func Run() {
write := make(chan bool)
read := make(chan witTypes.Unit)
{
tx, rx := test.MakeFutureUnit()
go func() {
write <- tx.Write(witTypes.Unit{})
}()
go func() {
test.ReadFuture(rx)
read <- witTypes.Unit{}
}()
(<-read)
assert(<-write)
}
{
tx, rx := test.MakeFutureUnit()
go func() {
write <- tx.Write(witTypes.Unit{})
}()
go func() {
test.DropFuture(rx)
read <- witTypes.Unit{}
}()
(<-read)
assert(!(<-write))
}
{
tx, rx := test.MakeFutureUnit()
syncBarrier := make(chan struct{})
panicCh := make(chan any, 2)
for range 2 {
go func() {
<-syncBarrier
panicCh <- checkPanicValue(func() {
rx.Read()
})
}()
}
close(syncBarrier)
go func() {
tx.Write(witTypes.Unit{})
}()
p1, p2 := <-panicCh, <-panicCh
assert((p1 == nil && p2 == "nil handle") || (p1 == "nil handle" && p2 == nil))
}
{
tx, rx := test.MakeFutureUnit()
syncBarrier := make(chan struct{})
panicCh := make(chan any, 2)
for range 2 {
go func() {
<-syncBarrier
panicCh <- checkPanicValue(func() {
tx.Write(witTypes.Unit{})
})
}()
}
close(syncBarrier)
go func() {
rx.Read()
}()
p1, p2 := <-panicCh, <-panicCh
assert((p1 == nil && p2 == "nil handle") || (p1 == "nil handle" && p2 == nil))
}
}
func assert(v bool) {
if !v {
panic("assertion failed")
}
}
func checkPanicValue(f func()) (value any) {
defer func() {
value = recover()
}()
f()
return nil
}