From 810b98d4bc612e6cc5d7db642432d276c7898a22 Mon Sep 17 00:00:00 2001 From: Zhiyi Pan Date: Mon, 21 Aug 2023 16:13:29 -0700 Subject: [PATCH] Clean code and tests --- channel.go | 13 ++++------- channel_test.go | 57 +++++++++++++++++-------------------------------- 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/channel.go b/channel.go index 3597949b..cca873de 100644 --- a/channel.go +++ b/channel.go @@ -98,6 +98,7 @@ type ChannelOptions struct { // It requires underlying operating system support MPTCP. // If EnableMPTCP is false or no MPTCP support, the connection will use normal TCP. // It's set to false by default. + // If a Dialer is passed as option, this value will be ignored. EnableMPTCP bool // The reporter to use for reporting stats for this channel. @@ -414,15 +415,9 @@ func (ch *Channel) ListenAndServe(hostPort string) error { return errAlreadyListening } - var l net.Listener - var err error - if ch.enableMPTCP { - lc := &net.ListenConfig{} - lc.SetMultipathTCP(true) - l, err = lc.Listen(context.Background(), "tcp", hostPort) - } else { - l, err = net.Listen("tcp", hostPort) - } + lc := net.ListenConfig{} + lc.SetMultipathTCP(ch.enableMPTCP) + l, err := lc.Listen(context.Background(), "tcp", hostPort) if err != nil { mutable.RUnlock() return err diff --git a/channel_test.go b/channel_test.go index 350152cb..0c09d94c 100644 --- a/channel_test.go +++ b/channel_test.go @@ -44,46 +44,27 @@ func toMap(fields LogFields) map[string]interface{} { } func TestNewChannel(t *testing.T) { - ch, err := NewChannel("svc", &ChannelOptions{ - ProcessName: "pname", - }) - require.NoError(t, err, "NewChannel failed") - - assert.Equal(t, LocalPeerInfo{ - ServiceName: "svc", - PeerInfo: PeerInfo{ - ProcessName: "pname", - HostPort: ephemeralHostPort, - IsEphemeral: true, - Version: PeerVersion{ - Language: "go", - LanguageVersion: strings.TrimPrefix(runtime.Version(), "go"), - TChannelVersion: VersionInfo, - }, - }, - }, ch.PeerInfo(), "Wrong local peer info") -} - -func TestNewChannelEnableMPTCP(t *testing.T) { - ch, err := NewChannel("svc", &ChannelOptions{ - ProcessName: "pname", - EnableMPTCP: true, - }) - require.NoError(t, err, "NewChannel failed") - - assert.Equal(t, LocalPeerInfo{ - ServiceName: "svc", - PeerInfo: PeerInfo{ + for _, mptcp := range []bool{true, false} { + ch, err := NewChannel("svc", &ChannelOptions{ ProcessName: "pname", - HostPort: ephemeralHostPort, - IsEphemeral: true, - Version: PeerVersion{ - Language: "go", - LanguageVersion: strings.TrimPrefix(runtime.Version(), "go"), - TChannelVersion: VersionInfo, + EnableMPTCP: mptcp, + }) + require.NoError(t, err, "NewChannel failed") + + assert.Equal(t, LocalPeerInfo{ + ServiceName: "svc", + PeerInfo: PeerInfo{ + ProcessName: "pname", + HostPort: ephemeralHostPort, + IsEphemeral: true, + Version: PeerVersion{ + Language: "go", + LanguageVersion: strings.TrimPrefix(runtime.Version(), "go"), + TChannelVersion: VersionInfo, + }, }, - }, - }, ch.PeerInfo(), "Wrong local peer info") + }, ch.PeerInfo(), "Wrong local peer info") + } } func TestLoggers(t *testing.T) {