Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

channelz: add LocalAddr to listen sockets and test #7062

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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("there should only be one server, not %d", len(ss))
}

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("server want id %d, but got %d", serverID, srv.ID)
}

skts := srv.ListenSockets()
if got, want := len(skts), 1; got != want {
return false, fmt.Errorf("server has %v sockets; want %v", got, want)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional nit: keep consistent with go style guide and also I prefer consistent format across errors (and our codebase): https://g3doc.corp.google.com/go/g3doc/style/decisions.md?cl=head#got-before-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 has 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