summaryrefslogtreecommitdiff
path: root/internal/debug/server_test.go
blob: de48fbbc1914a63f363c5bb5729892fac4323933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package debug_test

import (
	"context"
	"math/rand"
	"net"
	"net/http"
	"strconv"
	"testing"
	"time"

	"github.com/roadrunner-server/roadrunner/v2023/internal/debug"

	"github.com/stretchr/testify/assert"
)

func TestServer_StartingAndStopping(t *testing.T) {
	rand.Seed(time.Now().UnixNano())

	var (
		s    = debug.NewServer()
		port = strconv.Itoa(rand.Intn(10000) + 10000) //nolint:gosec
	)

	go func() { assert.ErrorIs(t, s.Start(":"+port), http.ErrServerClosed) }()

	defer func() { assert.NoError(t, s.Stop(context.Background())) }()

	for i := 0; i < 100; i++ { // wait for server started state
		if l, err := net.Dial("tcp", ":"+port); err != nil {
			<-time.After(time.Millisecond)
		} else {
			_ = l.Close()

			break
		}
	}

	for _, uri := range []string{ // assert that pprof handlers exists
		"http://127.0.0.1:" + port + "/debug/pprof/",
		"http://127.0.0.1:" + port + "/debug/pprof/cmdline",
		// "http://127.0.0.1:" + port + "/debug/pprof/profile",
		"http://127.0.0.1:" + port + "/debug/pprof/symbol",
		// "http://127.0.0.1:" + port + "/debug/pprof/trace",
	} {
		ctx, cancel := context.WithTimeout(context.Background(), time.Second)

		req, _ := http.NewRequestWithContext(ctx, http.MethodHead, uri, http.NoBody)
		resp, err := http.DefaultClient.Do(req)
		assert.NoError(t, err)
		assert.Equal(t, http.StatusOK, resp.StatusCode)

		_ = resp.Body.Close()

		cancel()
	}
}