github.com/gofiber/fiber/v2@v2.47.0/prefork_test.go (about)

     1  // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
     2  // 📄 Github Repository: https://github.com/gofiber/fiber
     3  // 📌 API Documentation: https://docs.gofiber.io
     4  // 💖 Maintained and modified for Fiber by @renewerner87
     5  package fiber
     6  
     7  import (
     8  	"crypto/tls"
     9  	"io"
    10  	"os"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gofiber/fiber/v2/utils"
    15  )
    16  
    17  func Test_App_Prefork_Child_Process(t *testing.T) {
    18  	// Reset test var
    19  	testPreforkMaster = true
    20  
    21  	setupIsChild(t)
    22  	defer teardownIsChild(t)
    23  
    24  	app := New()
    25  
    26  	err := app.prefork(NetworkTCP4, "invalid", nil)
    27  	utils.AssertEqual(t, false, err == nil)
    28  
    29  	go func() {
    30  		time.Sleep(1000 * time.Millisecond)
    31  		utils.AssertEqual(t, nil, app.Shutdown())
    32  	}()
    33  
    34  	utils.AssertEqual(t, nil, app.prefork(NetworkTCP6, "[::1]:", nil))
    35  
    36  	// Create tls certificate
    37  	cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
    38  	if err != nil {
    39  		utils.AssertEqual(t, nil, err)
    40  	}
    41  	//nolint:gosec // We're in a test so using old ciphers is fine
    42  	config := &tls.Config{Certificates: []tls.Certificate{cer}}
    43  
    44  	go func() {
    45  		time.Sleep(1000 * time.Millisecond)
    46  		utils.AssertEqual(t, nil, app.Shutdown())
    47  	}()
    48  
    49  	utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, "127.0.0.1:", config))
    50  }
    51  
    52  func Test_App_Prefork_Master_Process(t *testing.T) {
    53  	// Reset test var
    54  	testPreforkMaster = true
    55  
    56  	app := New()
    57  
    58  	go func() {
    59  		time.Sleep(1000 * time.Millisecond)
    60  		utils.AssertEqual(t, nil, app.Shutdown())
    61  	}()
    62  
    63  	utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, ":3000", nil))
    64  
    65  	dummyChildCmd.Store("invalid")
    66  
    67  	err := app.prefork(NetworkTCP4, "127.0.0.1:", nil)
    68  	utils.AssertEqual(t, false, err == nil)
    69  
    70  	dummyChildCmd.Store("")
    71  }
    72  
    73  func Test_App_Prefork_Child_Process_Never_Show_Startup_Message(t *testing.T) {
    74  	setupIsChild(t)
    75  	defer teardownIsChild(t)
    76  
    77  	rescueStdout := os.Stdout
    78  	defer func() { os.Stdout = rescueStdout }()
    79  
    80  	r, w, err := os.Pipe()
    81  	utils.AssertEqual(t, nil, err)
    82  
    83  	os.Stdout = w
    84  
    85  	New().startupProcess().startupMessage(":3000", false, "")
    86  
    87  	utils.AssertEqual(t, nil, w.Close())
    88  
    89  	out, err := io.ReadAll(r)
    90  	utils.AssertEqual(t, nil, err)
    91  	utils.AssertEqual(t, 0, len(out))
    92  }
    93  
    94  func setupIsChild(t *testing.T) {
    95  	t.Helper()
    96  
    97  	utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, envPreforkChildVal))
    98  }
    99  
   100  func teardownIsChild(t *testing.T) {
   101  	t.Helper()
   102  
   103  	utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, ""))
   104  }