|
| 1 | +package node |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "cosmossdk.io/log" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/rollkit/rollkit/pkg/service" |
| 16 | +) |
| 17 | + |
| 18 | +func TestStartInstrumentationServer(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + require := require.New(t) |
| 21 | + assert := assert.New(t) |
| 22 | + |
| 23 | + var config = getTestConfig(t, 1) |
| 24 | + config.Instrumentation.Prometheus = true |
| 25 | + config.Instrumentation.PrometheusListenAddr = "127.0.0.1:26660" |
| 26 | + config.Instrumentation.Pprof = true |
| 27 | + config.Instrumentation.PprofListenAddr = "127.0.0.1:26661" |
| 28 | + |
| 29 | + node := &FullNode{ |
| 30 | + nodeConfig: config, |
| 31 | + BaseService: *service.NewBaseService(log.NewTestLogger(t), "TestNode", nil), |
| 32 | + } |
| 33 | + |
| 34 | + prometheusSrv, pprofSrv := node.startInstrumentationServer() |
| 35 | + |
| 36 | + require.NotNil(prometheusSrv, "Prometheus server should be initialized") |
| 37 | + require.NotNil(pprofSrv, "Pprof server should be initialized") |
| 38 | + |
| 39 | + time.Sleep(100 * time.Millisecond) |
| 40 | + |
| 41 | + resp, err := http.Get(fmt.Sprintf("http://%s/metrics", config.Instrumentation.PrometheusListenAddr)) |
| 42 | + require.NoError(err, "Failed to get Prometheus metrics") |
| 43 | + defer func() { |
| 44 | + err := resp.Body.Close() |
| 45 | + if err != nil { |
| 46 | + t.Logf("Error closing response body: %v", err) |
| 47 | + } |
| 48 | + }() |
| 49 | + assert.Equal(http.StatusOK, resp.StatusCode, "Prometheus metrics endpoint should return 200 OK") |
| 50 | + body, err := io.ReadAll(resp.Body) |
| 51 | + require.NoError(err) |
| 52 | + assert.Contains(string(body), "# HELP", "Prometheus metrics body should contain HELP lines") // Check for typical metrics content |
| 53 | + |
| 54 | + resp, err = http.Get(fmt.Sprintf("http://%s/debug/pprof/", config.Instrumentation.PprofListenAddr)) |
| 55 | + require.NoError(err, "Failed to get Pprof index") |
| 56 | + defer func() { |
| 57 | + err := resp.Body.Close() |
| 58 | + if err != nil { |
| 59 | + t.Logf("Error closing response body: %v", err) |
| 60 | + } |
| 61 | + }() |
| 62 | + assert.Equal(http.StatusOK, resp.StatusCode, "Pprof index endpoint should return 200 OK") |
| 63 | + body, err = io.ReadAll(resp.Body) |
| 64 | + require.NoError(err) |
| 65 | + assert.Contains(string(body), "Types of profiles available", "Pprof index body should contain expected text") |
| 66 | + |
| 67 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 68 | + defer cancel() |
| 69 | + |
| 70 | + if prometheusSrv != nil { |
| 71 | + err = prometheusSrv.Shutdown(shutdownCtx) |
| 72 | + assert.NoError(err, "Prometheus server shutdown should not return error") |
| 73 | + } |
| 74 | + if pprofSrv != nil { |
| 75 | + err = pprofSrv.Shutdown(shutdownCtx) |
| 76 | + assert.NoError(err, "Pprof server shutdown should not return error") |
| 77 | + } |
| 78 | +} |
0 commit comments