Skip to content

Commit 60faef3

Browse files
author
Sherry Yao
committedAug 6, 2024·
Considered pprofServer as nil and simplified test path for HTTP requests
1 parent ad16243 commit 60faef3

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed
 

‎cmdutil/debug/debug.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,18 @@ func NewPProfServer(config PProfServerConfig, l logrus.FieldLogger) *PProfServer
125125
//
126126
// It implements oklog group's runFn.
127127
func (s *PProfServer) Run() error {
128+
if s.pprofServer == nil {
129+
return fmt.Errorf("pprofServer is nil")
130+
}
131+
128132
s.logger.WithFields(logrus.Fields{
129133
"at": "binding",
130134
"service": "pprof",
131135
"addr": s.addr,
132136
}).Info()
133137

134-
if s.pprofServer != nil {
135-
if err := s.pprofServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
136-
return err
137-
}
138+
if err := s.pprofServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
139+
return err
138140
}
139141

140142
<-s.done

‎cmdutil/debug/debug_test.go

+20-29
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,27 @@ func TestNewPProfServer(t *testing.T) {
6363
}
6464
runtime.SetMutexProfileFraction(tt.expectedMutexFraction) // Reset to the expected value
6565

66-
urls := []string{
67-
"http://" + server.addr + "/debug/pprof/",
68-
"http://" + server.addr + "/debug/pprof/heap",
69-
"http://" + server.addr + "/debug/pprof/goroutine",
70-
"http://" + server.addr + "/debug/pprof/threadcreate",
71-
"http://" + server.addr + "/debug/pprof/block",
72-
"http://" + server.addr + "/debug/pprof/mutex",
73-
}
74-
75-
// Perform HTTP GET requests to ensure the server is running and all handlers respond correctly
66+
// Perform HTTP GET request to the root path
67+
url := "http://" + server.addr + "/debug/pprof/"
7668
client := &http.Client{}
77-
for _, url := range urls {
78-
t.Run("GET "+url, func(t *testing.T) {
79-
req, err := http.NewRequest("GET", url, nil)
80-
if err != nil {
81-
t.Errorf("http.NewRequest(%s) error = %v", url, err)
82-
}
83-
84-
resp, err := client.Do(req)
85-
if err != nil {
86-
t.Errorf("http.Client.Do() error = %v", err)
87-
}
88-
89-
if resp.StatusCode != http.StatusOK {
90-
t.Errorf("http.Client.Do() status = %v, want %v", resp.StatusCode, http.StatusOK)
91-
}
92-
93-
resp.Body.Close()
94-
})
95-
}
69+
70+
t.Run("GET "+url, func(t *testing.T) {
71+
req, err := http.NewRequest("GET", url, nil)
72+
if err != nil {
73+
t.Errorf("http.NewRequest(%s) error = %v", url, err)
74+
}
75+
76+
resp, err := client.Do(req)
77+
if err != nil {
78+
t.Errorf("http.Client.Do() error = %v", err)
79+
}
80+
81+
if resp.StatusCode != http.StatusOK {
82+
t.Errorf("http.Client.Do() status = %v, want %v", resp.StatusCode, http.StatusOK)
83+
}
84+
85+
resp.Body.Close()
86+
})
9687

9788
// Stop the server
9889
server.Stop(nil)

0 commit comments

Comments
 (0)
Please sign in to comment.