converge/pkg/testsupport/inmemoryconnection.go
Erik Brakkee d3d4c7242a restructuring test code by introducing a testsupport package
Making it easy 6to start a porof server in tests.
2024-09-08 11:16:49 +02:00

33 lines
905 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,
frontToBack: make(chan []byte),
backToFront: make(chan []byte),
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)
}