-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpprofrec_test.go
50 lines (37 loc) · 926 Bytes
/
pprofrec_test.go
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
package pprofrec
import (
"bytes"
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStream(t *testing.T) {
f := Stream(StreamOpts{Frequency: 100 * time.Millisecond})
ctx, cancel := context.WithCancel(context.Background())
r, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
require.NoError(t, err)
w := &responseWriter{}
go func() {
f(w, r)
}()
time.Sleep(500 * time.Millisecond)
cancel()
assert.Contains(t, w.Buffer.String(), "MiB")
}
type responseWriter struct {
Buffer bytes.Buffer
StatusCode int
}
func (w *responseWriter) Header() http.Header {
return http.Header{}
}
func (w *responseWriter) Write(b []byte) (int, error) {
return w.Buffer.Write(b)
}
func (w *responseWriter) WriteHeader(statusCode int) {
w.StatusCode = statusCode
}
func (w *responseWriter) Flush() {}