diff --git a/.golangci.yml b/.golangci.yml index 1b1c8e4b9..c2b81e0c3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,10 +2,15 @@ linters: disable-all: true enable: - bodyclose + - funlen - lll - misspell linters-settings: + funlen: + lines: 80 + statements: 30 + lll: line-length: 100 tab-width: 1 diff --git a/testhelper/upstream.go b/testhelper/upstream.go new file mode 100644 index 000000000..0238f37d6 --- /dev/null +++ b/testhelper/upstream.go @@ -0,0 +1,54 @@ +package testhelper + +import ( + "net" + "testing" +) + +type Upstream struct { + listener net.Listener + logger testing.TB + Connections chan net.Conn +} + +func NewUpstream(t testing.TB, ignoreData bool) *Upstream { + result := &Upstream{} + result.listen() + result.accept(ignoreData) + + return result +} + +func (u *Upstream) listen() { + listener, err := net.Listen("tcp", "localhost:0") + if err != nil { + u.logger.Fatalf("Failed to create TCP server: %v", err) + } + u.listener = listener +} + +func (u *Upstream) accept(ignoreData bool) { + u.Connections = make(chan net.Conn) + go func(u *Upstream) { + conn, err := u.listener.Accept() + if err != nil { + u.logger.Fatalf("Unable to accept TCP connection: %v", err) + } + if ignoreData { + buf := make([]byte, 4000) + for err == nil { + _, err = conn.Read(buf) + } + } else { + u.Connections <- conn + } + }(u) +} + +func (u *Upstream) Close() { + u.listener.Close() +} + +func (u *Upstream) Addr() string { + return u.listener.Addr().String() +}