Implemented conn.go and hanshake.go + unit tests
This commit is contained in:
44
conn_inmem_test.go
Normal file
44
conn_inmem_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user