Skip to content

Commit d6c17b8

Browse files
author
David Grieser
committed
Simplify and fix test.
1 parent df14116 commit d6c17b8

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

cmd/root.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cmd
33
import (
44
"net"
55
"net/http"
6-
"net/http/pprof"
6+
_ "net/http/pprof"
77

88
log "github.com/sirupsen/logrus"
99
"github.com/spf13/cobra"
@@ -25,21 +25,14 @@ var rootCmd = &cobra.Command{
2525
PersistentPreRun: func(cmd *cobra.Command, args []string) {
2626
if enableProfile {
2727
go func() {
28-
mux := http.NewServeMux()
29-
mux.HandleFunc("/debug/pprof/", pprof.Index)
30-
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
31-
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
32-
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
33-
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
34-
35-
listener, err := net.Listen("tcp", ":0")
28+
// pprof handlers are auto-registered on the default ServeMux when imported.
29+
listener, err := net.Listen("tcp", "127.0.0.1:")
3630
if err != nil {
3731
log.Errorf("pprof server failed to listen: %v", err)
3832
return
3933
}
40-
log.Infof("Starting pprof server on http://localhost%s/debug/pprof/", listener.Addr().String())
41-
err = http.Serve(listener, mux)
42-
if err != nil {
34+
log.Infof("Starting pprof server on http://%s/debug/pprof/", listener.Addr().String())
35+
if err = http.Serve(listener, nil); err != nil {
4336
log.Errorf("pprof server error: %v", err)
4437
}
4538
}()

cmd/root_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestProfileFlag(t *testing.T) {
4141
hclContent := `
4242
job "dummy" {
4343
command = "/bin/sh"
44-
args = ["-c", "echo dummy job running; sleep 30"] // Increased sleep to 30s
44+
args = ["-c", "echo dummy job running; sleep 10"]
4545
}
4646
`
4747
if err := os.WriteFile(filepath.Join(jobsDir, "dummy.hcl"), []byte(hclContent), 0644); err != nil {
@@ -105,7 +105,7 @@ job "dummy" {
105105
go func() {
106106
defer wg.Done()
107107
reader := bufio.NewReader(stderrPipe) // Read from stderrPipe
108-
pprofLogRegex := regexp.MustCompile(`Starting pprof server on http://localhost(?:[^:]+)?:(\d+)/debug/pprof/`)
108+
pprofLogRegex := regexp.MustCompile(`Starting pprof server on http://127.0.0.1:(\d+)/debug/pprof/`)
109109
for {
110110
line, err := reader.ReadString('\n')
111111
if len(line) > 0 {
@@ -141,11 +141,11 @@ job "dummy" {
141141
t.Logf("[%s] Successfully found pprof server URL: %s", time.Since(startTime), pprofURL)
142142
case <-ctx.Done(): // This is the main test context
143143
t.Logf("[%s] Main context done while waiting for pprof URL.", time.Since(startTime))
144-
cmd.Process.Kill() // Ensure process is killed if context times out
145-
wg.Wait() // Wait for goroutine to finish
144+
cmd.Process.Kill() // Ensure process is killed if context times out
145+
wg.Wait() // Wait for goroutine to finish
146146
logContent := errBuf.String() // Use the full stderr buffer for logging
147147
t.Fatalf("Test timed out waiting for pprof server log. Stderr:\n%s", logContent)
148-
case <-time.After(15 * time.Second): // Specific timeout for finding the log line, increased slightly
148+
case <-time.After(5 * time.Second): // Specific timeout for finding the log line
149149
t.Logf("[%s] Timed out waiting for pprof log line via channel.", time.Since(startTime))
150150
cmd.Process.Kill() // Ensure process is killed
151151
wg.Wait() // Wait for goroutine to finish
@@ -217,7 +217,7 @@ job "dummy" {
217217

218218
if !success {
219219
finalErrorDescriptive := "No successful HTTP GET." // Renamed to avoid conflict if needed, or ensure proper scope
220-
if httpErr != nil { // httpErr should hold the error from the last attempt
220+
if httpErr != nil { // httpErr should hold the error from the last attempt
221221
finalErrorDescriptive = httpErr.Error()
222222
}
223223
statusCode := 0
@@ -244,7 +244,7 @@ job "dummy" {
244244
// HTTP Check related variables (resp, httpErr, success) are already defined before this block.
245245
// The defer func above will handle cmd.Wait() and final process state logging.
246246
// We only need to kill the process here if the HTTP checks complete *successfully*
247-
// and the process hasn't naturally exited due to its own short lifecycle (unlikely with sleep 30).
247+
// and the process hasn't naturally exited due to its own short lifecycle (unlikely with sleep 10).
248248
if success { // If HTTP check was successful
249249
if cmd.Process != nil && (cmd.ProcessState == nil || !cmd.ProcessState.Exited()) {
250250
t.Logf("[%s] HTTP check successful, ensuring mittnite process (PID: %d) is terminated.", time.Since(startTime), cmd.Process.Pid)

0 commit comments

Comments
 (0)