Skip to content

Commit

Permalink
channelz: add LocalAddr to listen sockets and test (#7062)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley committed Mar 22, 2024
1 parent a975978 commit c003fdf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,13 @@ func (s *Server) Serve(lis net.Listener) error {

ls := &listenSocket{
Listener: lis,
channelz: channelz.RegisterSocket(&channelz.Socket{SocketType: channelz.SocketTypeListen, Parent: s.channelz, RefName: lis.Addr().String(), SocketOptions: channelz.GetSocketOption(lis)}),
channelz: channelz.RegisterSocket(&channelz.Socket{
SocketType: channelz.SocketTypeListen,
Parent: s.channelz,
RefName: lis.Addr().String(),
LocalAddr: lis.Addr(),
SocketOptions: channelz.GetSocketOption(lis)},
),
}
s.lis[ls] = true

Expand Down
43 changes: 43 additions & 0 deletions test/channelz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,49 @@ func (s) TestCZGetServer(t *testing.T) {
}
}

func (s) TestCZGetSocket(t *testing.T) {
e := tcpClearRREnv
te := newTest(t, e)
lis := te.listenAndServe(&testServer{security: e.security}, net.Listen)
defer te.tearDown()

if err := verifyResultWithDelay(func() (bool, error) {
ss, _ := channelz.GetServers(0, 0)
if len(ss) != 1 {
return false, fmt.Errorf("len(ss) = %v; want %v", len(ss), 1)
}

serverID := ss[0].ID
srv := channelz.GetServer(serverID)
if srv == nil {
return false, fmt.Errorf("server %d does not exist", serverID)
}
if srv.ID != serverID {
return false, fmt.Errorf("srv.ID = %d; want %v", srv.ID, serverID)
}

skts := srv.ListenSockets()
if got, want := len(skts), 1; got != want {
return false, fmt.Errorf("len(skts) = %v; want %v", got, want)
}
var sktID int64
for sktID = range skts {
}

skt := channelz.GetSocket(sktID)
if skt == nil {
return false, fmt.Errorf("socket %v does not exist", sktID)
}

if got, want := skt.LocalAddr, lis.Addr(); got != want {
return false, fmt.Errorf("socket %v LocalAddr=%v; want %v", sktID, got, want)
}
return true, nil
}); err != nil {
t.Fatal(err)
}
}

func (s) TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
testcases := []struct {
total int
Expand Down

0 comments on commit c003fdf

Please sign in to comment.