1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 14:13:29 +00:00
fiber/prefork_test.go

102 lines
2.3 KiB
Go
Raw Normal View History

2020-09-29 21:49:50 +02:00
// ⚡️ 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
2020-07-15 10:37:57 +08:00
package fiber
2020-07-15 15:20:10 +02:00
import (
2020-07-16 16:42:36 +08:00
"crypto/tls"
"io/ioutil"
2020-07-15 15:20:10 +02:00
"os"
"testing"
"time"
2020-07-15 10:37:57 +08:00
"github.com/gofiber/fiber/v2/utils"
2020-07-15 15:20:10 +02:00
)
2020-07-15 10:37:57 +08:00
2020-07-15 15:20:10 +02:00
func Test_App_Prefork_Child_Process(t *testing.T) {
// Reset test var
testPreforkMaster = true
2020-07-18 01:31:39 +02:00
setupIsChild(t)
defer teardownIsChild(t)
2020-07-15 10:37:57 +08:00
2020-07-15 15:20:10 +02:00
app := New()
2020-07-15 10:37:57 +08:00
2021-02-08 11:35:17 +08:00
err := app.prefork(NetworkTCP4, "invalid", nil)
2020-07-15 15:20:10 +02:00
utils.AssertEqual(t, false, err == nil)
2020-07-15 15:13:02 +08:00
2020-07-15 15:20:10 +02:00
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
2020-07-15 10:37:57 +08:00
utils.AssertEqual(t, nil, app.prefork(NetworkTCP6, "[::1]:", nil))
2020-07-16 16:42:36 +08:00
// Create tls certificate
2020-09-13 11:20:11 +02:00
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
2020-07-16 16:42:36 +08:00
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())
}()
2021-02-08 11:35:17 +08:00
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, "127.0.0.1:", config))
2020-07-15 15:20:10 +02:00
}
2020-07-15 10:37:57 +08:00
func Test_App_Prefork_Master_Process(t *testing.T) {
// Reset test var
2020-07-15 15:20:10 +02:00
testPreforkMaster = true
2020-07-15 10:37:57 +08:00
2020-07-15 15:20:10 +02:00
app := New()
2020-07-15 10:37:57 +08:00
2020-07-15 15:20:10 +02:00
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
2020-07-15 10:37:57 +08:00
2021-02-08 11:35:17 +08:00
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, ":3000", nil))
2020-07-15 15:20:10 +02:00
dummyChildCmd = "invalid"
2021-02-08 11:35:17 +08:00
err := app.prefork(NetworkTCP4, "127.0.0.1:", nil)
2020-07-15 15:20:10 +02:00
utils.AssertEqual(t, false, err == nil)
}
2020-07-15 17:43:30 +02:00
2020-07-16 16:42:36 +08:00
func Test_App_Prefork_Child_Process_Never_Show_Startup_Message(t *testing.T) {
setupIsChild(t)
defer teardownIsChild(t)
2020-07-16 16:42:36 +08:00
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, "")
2020-07-16 16:42:36 +08:00
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) {
t.Helper()
utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, envPreforkChildVal))
}
func teardownIsChild(t *testing.T) {
t.Helper()
utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, ""))
}