Jake Hillion
ff4ce07b05
All checks were successful
continuous-integration/drone/push Build is passing
96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
package mocks
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type MockPerfectBiStreamConn struct {
|
|
directionA chan byte
|
|
directionB chan byte
|
|
}
|
|
|
|
func NewMockPerfectBiStreamConn(bufSize int) MockPerfectBiStreamConn {
|
|
return MockPerfectBiStreamConn{
|
|
directionA: make(chan byte, bufSize),
|
|
directionB: make(chan byte, bufSize),
|
|
}
|
|
}
|
|
|
|
func (bc MockPerfectBiStreamConn) SideA() MockPerfectStreamConn {
|
|
return MockPerfectStreamConn{inbound: bc.directionA, outbound: bc.directionB}
|
|
}
|
|
|
|
func (bc MockPerfectBiStreamConn) SideB() MockPerfectStreamConn {
|
|
return MockPerfectStreamConn{inbound: bc.directionB, outbound: bc.directionA}
|
|
}
|
|
|
|
type MockPerfectStreamConn struct {
|
|
inbound chan byte
|
|
outbound chan byte
|
|
}
|
|
|
|
type Conn interface {
|
|
Read(b []byte) (n int, err error)
|
|
Write(b []byte) (n int, err error)
|
|
SetWriteDeadline(time.Time) error
|
|
|
|
// For printing
|
|
LocalAddr() net.Addr
|
|
RemoteAddr() net.Addr
|
|
}
|
|
|
|
func (c MockPerfectStreamConn) Read(p []byte) (n int, err error) {
|
|
for i := range p {
|
|
if i == 0 {
|
|
p[i] = <-c.inbound
|
|
} else {
|
|
select {
|
|
case b := <-c.inbound:
|
|
p[i] = b
|
|
default:
|
|
return i, nil
|
|
}
|
|
}
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
func (c MockPerfectStreamConn) Write(p []byte) (n int, err error) {
|
|
for _, b := range p {
|
|
c.outbound <- b
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
func (c MockPerfectStreamConn) SetWriteDeadline(time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
// Only used for printing flow information
|
|
func (c MockPerfectStreamConn) LocalAddr() net.Addr {
|
|
return &net.TCPAddr{
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
Port: 499,
|
|
}
|
|
}
|
|
|
|
func (c MockPerfectStreamConn) RemoteAddr() net.Addr {
|
|
return &net.TCPAddr{
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
Port: 500,
|
|
}
|
|
}
|
|
|
|
func (c MockPerfectStreamConn) NonBlockingRead(p []byte) (n int, err error) {
|
|
for i := range p {
|
|
select {
|
|
case b := <-c.inbound:
|
|
p[i] = b
|
|
default:
|
|
return i, nil
|
|
}
|
|
}
|
|
return len(p), nil
|
|
}
|