|
| 1 | +package debug |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "runtime" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewPProfServer(t *testing.T) { |
| 13 | + logger := logrus.New() |
| 14 | + |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + config PProfServerConfig |
| 18 | + expectedAddr string |
| 19 | + expectedMutexFraction int |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "DefaultAddr", |
| 23 | + config: PProfServerConfig{}, |
| 24 | + expectedAddr: "127.0.0.1:9998", |
| 25 | + expectedMutexFraction: defaultMutexProfileFraction, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "CustomAddr", |
| 29 | + config: PProfServerConfig{Addr: "127.0.0.1:9090"}, |
| 30 | + expectedAddr: "127.0.0.1:9090", |
| 31 | + expectedMutexFraction: defaultMutexProfileFraction, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "CustomMutexProfileFraction", |
| 35 | + config: PProfServerConfig{MutexProfileFraction: 5}, |
| 36 | + expectedAddr: "127.0.0.1:9998", |
| 37 | + expectedMutexFraction: 5, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + server := NewPProfServer(tt.config, logger) |
| 44 | + |
| 45 | + // Check server address |
| 46 | + if server.addr != tt.expectedAddr { |
| 47 | + t.Errorf("NewPProfServer() addr = %v, want %v", server.addr, tt.expectedAddr) |
| 48 | + } |
| 49 | + |
| 50 | + // Start the server |
| 51 | + go func() { |
| 52 | + if err := server.Run(); err != nil { |
| 53 | + t.Errorf("NewPProfServer() run error = %v", err) |
| 54 | + } |
| 55 | + }() |
| 56 | + |
| 57 | + // Give the server a moment to start |
| 58 | + time.Sleep(100 * time.Millisecond) |
| 59 | + |
| 60 | + // Check mutex profile fraction |
| 61 | + if got := runtime.SetMutexProfileFraction(0); got != tt.expectedMutexFraction { |
| 62 | + t.Errorf("runtime.SetMutexProfileFraction() = %v, want %v", got, tt.expectedMutexFraction) |
| 63 | + } |
| 64 | + runtime.SetMutexProfileFraction(tt.expectedMutexFraction) // Reset to the expected value |
| 65 | + |
| 66 | + // Perform HTTP GET request to the root path |
| 67 | + url := "http://" + server.addr + "/debug/pprof/" |
| 68 | + client := &http.Client{} |
| 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 | + }) |
| 87 | + |
| 88 | + // Stop the server |
| 89 | + server.Stop(nil) |
| 90 | + |
| 91 | + // Ensure the server is stopped |
| 92 | + select { |
| 93 | + case <-server.done: |
| 94 | + // success |
| 95 | + case <-time.After(1 * time.Second): |
| 96 | + t.Fatal("server did not stop in time") |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments