forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prefork_test.go
97 lines (71 loc) · 2.24 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
// ⚡️ 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/ioutil"
"os"
"testing"
"time"
"github.com/gofiber/fiber/v2/utils"
)
func Test_App_Prefork_Child_Process(t *testing.T) {
// Reset test var
testPreforkMaster = true
setupIsChild(t)
defer teardownIsChild(t)
app := New()
err := app.prefork(NetworkTCP4, "invalid", nil)
utils.AssertEqual(t, false, err == nil)
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.prefork(NetworkTCP6, "[::]:", nil))
// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
if err != nil {
utils.AssertEqual(t, nil, err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, "127.0.0.1:", config))
}
func Test_App_Prefork_Master_Process(t *testing.T) {
// Reset test var
testPreforkMaster = true
app := New()
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, ":3000", nil))
dummyChildCmd = "invalid"
err := app.prefork(NetworkTCP4, "127.0.0.1:", nil)
utils.AssertEqual(t, false, err == nil)
}
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()
utils.AssertEqual(t, nil, err)
os.Stdout = w
New().startupProcess().startupMessage(":3000", false, "")
utils.AssertEqual(t, nil, w.Close())
out, err := ioutil.ReadAll(r)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 0, len(out))
}
func setupIsChild(t *testing.T) {
utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, envPreforkChildVal))
}
func teardownIsChild(t *testing.T) {
utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, ""))
}