test: get rid of testUpstream

No need for this complexity.
This commit is contained in:
Simon Ser 2021-10-05 14:26:10 +02:00
parent c672763068
commit 0d2829c180

View File

@ -47,36 +47,12 @@ func createTestDownstream(t *testing.T, srv *Server) ircConn {
return newNetIRCConn(c2) return newNetIRCConn(c2)
} }
type testUpstream struct { func createTestUpstream(t *testing.T, db Database, user *User) (*Network, net.Listener) {
net.Listener
Accept chan ircConn
}
func createTestUpstream(t *testing.T, db Database, user *User) (*Network, *testUpstream) {
ln, err := net.Listen("tcp", "localhost:0") ln, err := net.Listen("tcp", "localhost:0")
if err != nil { if err != nil {
t.Fatalf("failed to create TCP listener: %v", err) t.Fatalf("failed to create TCP listener: %v", err)
} }
tu := &testUpstream{
Listener: ln,
Accept: make(chan ircConn),
}
go func() {
defer close(tu.Accept)
for {
c, err := ln.Accept()
if isErrClosed(err) {
break
} else if err != nil {
t.Fatalf("failed accepting connection: %v", err)
}
tu.Accept <- newNetIRCConn(c)
}
}()
network := &Network{ network := &Network{
Name: "testnet", Name: "testnet",
Addr: "irc+insecure://" + ln.Addr().String(), Addr: "irc+insecure://" + ln.Addr().String(),
@ -87,7 +63,15 @@ func createTestUpstream(t *testing.T, db Database, user *User) (*Network, *testU
t.Fatalf("failed to store test network: %v", err) t.Fatalf("failed to store test network: %v", err)
} }
return network, tu return network, ln
}
func mustAccept(t *testing.T, ln net.Listener) ircConn {
c, err := ln.Accept()
if err != nil {
t.Fatalf("failed accepting connection: %v", err)
}
return newNetIRCConn(c)
} }
func expectMessage(t *testing.T, c ircConn, cmd string) *irc.Message { func expectMessage(t *testing.T, c ircConn, cmd string) *irc.Message {
@ -169,7 +153,7 @@ func TestServer(t *testing.T) {
} }
defer srv.Shutdown() defer srv.Shutdown()
uc := <-upstream.Accept uc := mustAccept(t, upstream)
defer uc.Close() defer uc.Close()
registerUpstreamConn(t, uc) registerUpstreamConn(t, uc)