Files
ownwire-go-sdk/conn_inmem_test.go

45 lines
942 B
Go
Raw Permalink Normal View History

// This file defines an in-memory implementation of the Conn interface that is
// used ONLY by unit tests.
//
// The inmem_conn replaces a real WebSocket or network connection with simple
// Go channels. This allows handshake, sequencing, and message logic to be
// tested deterministically without spinning up a server, opening sockets,
// or relying on timing.
package ownwire_sdk
import (
"context"
)
type inmem_conn struct {
write_ch chan string
read_ch chan string
}
func new_inmem_conn() *inmem_conn {
return &inmem_conn{
write_ch: make(chan string, 16),
read_ch: make(chan string, 16),
}
}
func (c *inmem_conn) WriteText(ctx context.Context, s string) error {
select {
case c.write_ch <- s:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (c *inmem_conn) ReadText(ctx context.Context) (string, error) {
select {
case s := <-c.read_ch:
return s, nil
case <-ctx.Done():
return "", ctx.Err()
}
}