38 lines
798 B
Go
38 lines
798 B
Go
|
|
package ownwire_sdk_test
|
||
|
|
|
||
|
|
import "context"
|
||
|
|
|
||
|
|
// sdk_test_inmem_conn is a test-only in-memory Conn implementation.
|
||
|
|
// It lets tests drive protocol logic deterministically without a real websocket
|
||
|
|
// or a server.
|
||
|
|
type sdk_test_inmem_conn struct {
|
||
|
|
write_ch chan string
|
||
|
|
read_ch chan string
|
||
|
|
}
|
||
|
|
|
||
|
|
func sdk_test_new_inmem_conn() *sdk_test_inmem_conn {
|
||
|
|
return &sdk_test_inmem_conn{
|
||
|
|
write_ch: make(chan string, 16),
|
||
|
|
read_ch: make(chan string, 16),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *sdk_test_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 *sdk_test_inmem_conn) ReadText(ctx context.Context) (string, error) {
|
||
|
|
select {
|
||
|
|
case s := <-c.read_ch:
|
||
|
|
return s, nil
|
||
|
|
case <-ctx.Done():
|
||
|
|
return "", ctx.Err()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|