package export_my_test_test_interface
import (
"runtime"
. "wit_component/my_test_test_interface"
. "go.bytecodealliance.org/pkg/wit/types"
)
type TestThing struct {
pinner runtime.Pinner
handle int32
value string
}
func (self *TestThing) Get() string {
return self.value
}
func (self *TestThing) OnDrop() {}
func MakeTestThing(value string) *TestThing {
return &TestThing{runtime.Pinner{}, 0, value}
}
func ShortReadsTest(stream *StreamReader[*TestThing]) *StreamReader[*TestThing] {
tx, rx := MakeStreamTestThing()
go func() {
defer stream.Drop()
defer tx.Drop()
things := []*TestThing{}
for !stream.WriterDropped() {
buffer := make([]*TestThing, 1)
count := stream.Read(buffer)
if count == 1 {
things = append(things, buffer[0])
}
}
tx.WriteAll(things)
}()
return rx
}
func ShortReadsLeaf(stream *StreamReader[*LeafThing]) *StreamReader[*LeafThing] {
tx, rx := MakeStreamMyTestLeafInterfaceLeafThing()
go func() {
defer stream.Drop()
defer tx.Drop()
things := []*LeafThing{}
for !stream.WriterDropped() {
buffer := make([]*LeafThing, 1)
count := stream.Read(buffer)
if count == 1 {
things = append(things, buffer[0])
}
}
tx.WriteAll(things)
}()
return rx
}
func DroppedReaderTest(f1, f2 *FutureReader[*TestThing]) (*FutureReader[*TestThing], *FutureReader[*TestThing]) {
tx1, rx1 := MakeFutureTestThing()
tx2, rx2 := MakeFutureTestThing()
go func() {
f1.Drop()
thing := f2.Read()
assert(!tx1.Write(thing))
assert(tx2.Write(thing))
}()
return rx1, rx2
}
func DroppedReaderLeaf(f1, f2 *FutureReader[*LeafThing]) (*FutureReader[*LeafThing], *FutureReader[*LeafThing]) {
tx1, rx1 := MakeFutureMyTestLeafInterfaceLeafThing()
tx2, rx2 := MakeFutureMyTestLeafInterfaceLeafThing()
go func() {
f1.Drop()
thing := f2.Read()
assert(!tx1.Write(thing))
assert(tx2.Write(thing))
}()
return rx1, rx2
}
func assert(v bool) {
if !v {
panic("assertion failed")
}
}