forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefork_test.go
105 lines (76 loc) · 2.4 KB
/
prefork_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
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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📄 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
// 💖 Maintained and modified for Fiber by @renewerner87
package fiber
import (
"crypto/tls"
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_App_Prefork_Child_Process(t *testing.T) {
// Reset test var
testPreforkMaster = true
setupIsChild(t)
defer teardownIsChild(t)
app := New()
err := app.prefork("invalid", nil, listenConfigDefault())
require.Error(t, err)
go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()
require.NoError(t, app.prefork("[::1]:", nil, ListenConfig{ListenerNetwork: NetworkTCP6}))
// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
if err != nil {
require.NoError(t, err)
}
//nolint:gosec // We're in a test so using old ciphers is fine
config := &tls.Config{Certificates: []tls.Certificate{cer}}
go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()
require.NoError(t, app.prefork("127.0.0.1:", config, listenConfigDefault()))
}
func Test_App_Prefork_Master_Process(t *testing.T) {
// Reset test var
testPreforkMaster = true
app := New()
go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()
require.NoError(t, app.prefork(":3000", nil, listenConfigDefault()))
dummyChildCmd.Store("invalid")
err := app.prefork("127.0.0.1:", nil, listenConfigDefault())
require.Error(t, err)
dummyChildCmd.Store("go")
}
func Test_App_Prefork_Child_Process_Never_Show_Startup_Message(t *testing.T) {
setupIsChild(t)
defer teardownIsChild(t)
rescueStdout := os.Stdout
defer func() { os.Stdout = rescueStdout }()
r, w, err := os.Pipe()
require.NoError(t, err)
os.Stdout = w
New().startupProcess().startupMessage(":3000", false, "", listenConfigDefault())
require.NoError(t, w.Close())
out, err := io.ReadAll(r)
require.NoError(t, err)
require.Empty(t, out)
}
func setupIsChild(t *testing.T) {
t.Helper()
require.NoError(t, os.Setenv(envPreforkChildKey, envPreforkChildVal)) //nolint:tenv // Ignore error
}
func teardownIsChild(t *testing.T) {
t.Helper()
require.NoError(t, os.Setenv(envPreforkChildKey, "")) //nolint:tenv // Ignore error
}