Skip to content

Commit

Permalink
Clean code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyipanuber committed Aug 21, 2023
1 parent 4c7e3df commit 810b98d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
13 changes: 4 additions & 9 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
57 changes: 19 additions & 38 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 810b98d

Please sign in to comment.