-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevlog_e2e_test.go
More file actions
85 lines (68 loc) · 1.72 KB
/
devlog_e2e_test.go
File metadata and controls
85 lines (68 loc) · 1.72 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package devlog_test
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"runtime"
"sync"
"testing"
"github.com/networkteam/devlog"
"github.com/networkteam/devlog/collector"
)
func TestE2E(t *testing.T) {
var memBefore, memAfter runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&memBefore)
dlog := devlog.NewWithOptions(devlog.Options{
HTTPClientOptions: &collector.HTTPClientOptions{
MaxBodySize: 1024 * 1024,
CaptureRequestBody: true,
CaptureResponseBody: true,
},
HTTPServerOptions: &collector.HTTPServerOptions{
MaxBodySize: 1024 * 1024,
CaptureRequestBody: true,
CaptureResponseBody: true,
},
})
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Copy the request body to response to simulate processing
io.Copy(w, r.Body)
}))
handler := dlog.CollectHTTPServer(mux)
server := httptest.NewServer(handler)
client := server.Client()
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
resp, err := client.Post(server.URL, "application/octet-stream", bytes.NewReader(make([]byte, 1024*1024))) // Send 1MB of data
if err != nil {
t.Errorf("Failed to make request: %v", err)
continue
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 OK, got %d", resp.StatusCode)
}
}
}()
}
wg.Wait()
server.Close()
client = nil
server = nil
dlog.Close()
dlog = nil
mux = nil
runtime.GC()
runtime.ReadMemStats(&memAfter)
memGrowth := memAfter.HeapAlloc - memBefore.HeapAlloc
if memGrowth > 2*1024*1024 {
t.Errorf("Memory leak detected: grew by %d bytes", memGrowth)
}
}