converge/pkg/testsupport/inmemoryconnection.go
Erik Brakkee ed04ac035a discovered net.Pipe for testing tcp connnections which makes the
previously developed ChannelReadWriter and InmemoryConnection obsolete.
2024-08-20 11:28:09 +02:00

34 lines
970 B
Go

package testsupport
import "context"
type InmemoryConnection struct {
ctx context.Context
frontToBack chan ([]byte)
backToFront chan ([]byte)
addr string
}
func NewInmemoryConnection(ctx context.Context, addr string) *InmemoryConnection {
pipe := InmemoryConnection{
ctx: ctx,
// TODO: somehow does not work with unbuffered channel and yamux
frontToBack: make(chan []byte, 0),
backToFront: make(chan []byte, 0),
addr: addr,
}
return &pipe
}
func (bitpipe *InmemoryConnection) Front() *ChannelReadWriter {
return pipe(bitpipe.ctx, bitpipe.backToFront, bitpipe.frontToBack, bitpipe.addr)
}
func (bitpipe *InmemoryConnection) Back() *ChannelReadWriter {
return pipe(bitpipe.ctx, bitpipe.frontToBack, bitpipe.backToFront, bitpipe.addr)
}
func pipe(ctx context.Context, receiveBuffer <-chan []byte, sendBuffer chan<- []byte, remoteAddr string) *ChannelReadWriter {
return NewChannelReadWriter(ctx, receiveBuffer, sendBuffer)
}