70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package mocks
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type MockPerfectBiPacketConn struct {
|
|
directionA chan []byte
|
|
directionB chan []byte
|
|
}
|
|
|
|
func NewMockPerfectBiPacketConn(bufSize int) MockPerfectBiPacketConn {
|
|
return MockPerfectBiPacketConn{
|
|
directionA: make(chan []byte, bufSize),
|
|
directionB: make(chan []byte, bufSize),
|
|
}
|
|
}
|
|
|
|
func (bc MockPerfectBiPacketConn) SideA() MockPerfectPacketConn {
|
|
return MockPerfectPacketConn{inbound: bc.directionA, outbound: bc.directionB}
|
|
}
|
|
|
|
func (bc MockPerfectBiPacketConn) SideB() MockPerfectPacketConn {
|
|
return MockPerfectPacketConn{inbound: bc.directionB, outbound: bc.directionA}
|
|
}
|
|
|
|
type MockPerfectPacketConn struct {
|
|
inbound chan []byte
|
|
outbound chan []byte
|
|
}
|
|
|
|
func (c MockPerfectPacketConn) Write(b []byte) (int, error) {
|
|
c.outbound <- b
|
|
return len(b), nil
|
|
}
|
|
|
|
func (c MockPerfectPacketConn) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) {
|
|
c.outbound <- b
|
|
return len(b), nil
|
|
}
|
|
|
|
func (c MockPerfectPacketConn) LocalAddr() net.Addr {
|
|
return &net.UDPAddr{
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
Port: 1234,
|
|
}
|
|
}
|
|
|
|
func (c MockPerfectPacketConn) SetReadDeadline(time.Time) error {
|
|
return nil
|
|
}
|
|
|
|
func (c MockPerfectPacketConn) ReadFromUDP(b []byte) (int, *net.UDPAddr, error) {
|
|
p := <-c.inbound
|
|
return copy(b, p), &net.UDPAddr{
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
Port: 1234,
|
|
}, nil
|
|
}
|
|
|
|
func (c MockPerfectPacketConn) NonBlockingRead(p []byte) (n int, err error) {
|
|
select {
|
|
case b := <-c.inbound:
|
|
return copy(p, b), nil
|
|
default:
|
|
return 0, nil
|
|
}
|
|
}
|