-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_test.go
More file actions
116 lines (91 loc) · 2.63 KB
/
platform_test.go
File metadata and controls
116 lines (91 loc) · 2.63 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package platform_test
import (
"context"
"io"
"net/http"
"runtime"
"testing"
"time"
"github.com/titpetric/platform"
"github.com/titpetric/platform/pkg/require"
)
func NewTestPlatform(tb testing.TB) *platform.Platform {
svc, err := platform.Start(tb.Context(), platform.NewTestOptions())
require.NoError(tb, err)
require.NotNil(tb, svc)
tb.Cleanup(svc.Stop)
return svc
}
func TestPlatform(t *testing.T) {
t.Run("single", func(t *testing.T) {
svc := platform.New(platform.NewTestOptions())
t.Cleanup(svc.Stop)
svc.Register(&platform.UnimplementedModule{
NameFn: func() string {
return "TestPlatform"
},
MountFn: func(_ context.Context, r platform.Router) error {
r.Get("/404", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("You found a valid route"))
})
return nil
},
})
svc.Use(platform.TestMiddleware())
t.Run("find", func(t *testing.T) {
var mod *platform.UnimplementedModule
require.True(t, svc.Find(&mod))
require.Equal(t, "TestPlatform", mod.Name())
})
plugins, mws := svc.Stats()
require.Equal(t, 1, plugins)
require.Equal(t, 1, mws)
require.NoError(t, svc.Start(t.Context()))
resp, err := http.Get(svc.URL() + "/404")
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, resp.Body.Close()) })
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, string(body), "You found a valid route")
})
t.Run("multi", func(t *testing.T) {
NewTestPlatform(t)
NewTestPlatform(t)
NewTestPlatform(t)
NewTestPlatform(t)
})
}
// This test case is an eyeball test. It starts and stops platforms in a loop and prints
// how many goroutines are alive. It doesn't make any assertion on the goroutine count,
// as tests are run in parallel. The eyeball test confirms stable goroutine levels.
func TestPlatform_goroutine_leaks(t *testing.T) {
if !testing.Verbose() {
t.Skip()
return
}
t.Run("stress", func(t *testing.T) {
t.Logf("start: %d", runtime.NumGoroutine())
for i := 0; i < 30; i++ {
svc, err := platform.Start(t.Context(), platform.NewTestOptions())
require.NoError(t, err)
require.NotNil(t, svc)
svc.Stop()
t.Logf("run[%d]: %d", i, runtime.NumGoroutine())
}
time.Sleep(time.Second)
runtime.GC()
t.Logf("final: %d", runtime.NumGoroutine())
// pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
})
}
func BenchmarkPlatform(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
svc, err := platform.Start(b.Context(), platform.NewTestOptions())
require.NoError(b, err)
require.NotNil(b, svc)
svc.Stop()
}
})
}