phpmainthread_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package frankenphp
  2. import (
  3. "io"
  4. "math/rand/v2"
  5. "net/http/httptest"
  6. "path/filepath"
  7. "sync"
  8. "sync/atomic"
  9. "testing"
  10. "time"
  11. "github.com/dunglas/frankenphp/internal/phpheaders"
  12. "github.com/stretchr/testify/assert"
  13. "go.uber.org/zap"
  14. )
  15. var testDataPath, _ = filepath.Abs("./testdata")
  16. func TestStartAndStopTheMainThreadWithOneInactiveThread(t *testing.T) {
  17. logger = zap.NewNop() // the logger needs to not be nil
  18. assert.NoError(t, initPHPThreads(1)) // reserve 1 thread
  19. assert.Len(t, phpThreads, 1)
  20. assert.Equal(t, 0, phpThreads[0].threadIndex)
  21. assert.True(t, phpThreads[0].state.is(stateInactive))
  22. drainPHPThreads()
  23. assert.Nil(t, phpThreads)
  24. }
  25. func TestTransitionRegularThreadToWorkerThread(t *testing.T) {
  26. logger = zap.NewNop()
  27. assert.NoError(t, initPHPThreads(1))
  28. // transition to regular thread
  29. convertToRegularThread(phpThreads[0])
  30. assert.IsType(t, &regularThread{}, phpThreads[0].handler)
  31. // transition to worker thread
  32. worker := getDummyWorker("transition-worker-1.php")
  33. convertToWorkerThread(phpThreads[0], worker)
  34. assert.IsType(t, &workerThread{}, phpThreads[0].handler)
  35. assert.Len(t, worker.threads, 1)
  36. // transition back to inactive thread
  37. convertToInactiveThread(phpThreads[0])
  38. assert.IsType(t, &inactiveThread{}, phpThreads[0].handler)
  39. assert.Len(t, worker.threads, 0)
  40. drainPHPThreads()
  41. assert.Nil(t, phpThreads)
  42. }
  43. func TestTransitionAThreadBetween2DifferentWorkers(t *testing.T) {
  44. logger = zap.NewNop()
  45. assert.NoError(t, initPHPThreads(1))
  46. firstWorker := getDummyWorker("transition-worker-1.php")
  47. secondWorker := getDummyWorker("transition-worker-2.php")
  48. // convert to first worker thread
  49. convertToWorkerThread(phpThreads[0], firstWorker)
  50. firstHandler := phpThreads[0].handler.(*workerThread)
  51. assert.Same(t, firstWorker, firstHandler.worker)
  52. assert.Len(t, firstWorker.threads, 1)
  53. assert.Len(t, secondWorker.threads, 0)
  54. // convert to second worker thread
  55. convertToWorkerThread(phpThreads[0], secondWorker)
  56. secondHandler := phpThreads[0].handler.(*workerThread)
  57. assert.Same(t, secondWorker, secondHandler.worker)
  58. assert.Len(t, firstWorker.threads, 0)
  59. assert.Len(t, secondWorker.threads, 1)
  60. drainPHPThreads()
  61. assert.Nil(t, phpThreads)
  62. }
  63. func TestTransitionThreadsWhileDoingRequests(t *testing.T) {
  64. numThreads := 10
  65. numRequestsPerThread := 100
  66. isRunning := atomic.Bool{}
  67. isRunning.Store(true)
  68. wg := sync.WaitGroup{}
  69. worker1Path := testDataPath + "/transition-worker-1.php"
  70. worker2Path := testDataPath + "/transition-worker-2.php"
  71. assert.NoError(t, Init(
  72. WithNumThreads(numThreads),
  73. WithWorkers(worker1Path, 1, map[string]string{"ENV1": "foo"}, []string{}),
  74. WithWorkers(worker2Path, 1, map[string]string{"ENV1": "foo"}, []string{}),
  75. WithLogger(zap.NewNop()),
  76. ))
  77. // randomly transition threads between regular, inactive and 2 worker threads
  78. go func() {
  79. for {
  80. for i := 0; i < numThreads; i++ {
  81. switch rand.IntN(4) {
  82. case 0:
  83. convertToRegularThread(phpThreads[i])
  84. case 1:
  85. convertToWorkerThread(phpThreads[i], workers[worker1Path])
  86. case 2:
  87. convertToWorkerThread(phpThreads[i], workers[worker2Path])
  88. case 3:
  89. convertToInactiveThread(phpThreads[i])
  90. }
  91. time.Sleep(time.Millisecond)
  92. if !isRunning.Load() {
  93. return
  94. }
  95. }
  96. }
  97. }()
  98. // randomly do requests to the 3 endpoints
  99. wg.Add(numThreads)
  100. for i := 0; i < numThreads; i++ {
  101. go func(i int) {
  102. for j := 0; j < numRequestsPerThread; j++ {
  103. switch rand.IntN(3) {
  104. case 0:
  105. assertRequestBody(t, "http://localhost/transition-worker-1.php", "Hello from worker 1")
  106. case 1:
  107. assertRequestBody(t, "http://localhost/transition-worker-2.php", "Hello from worker 2")
  108. case 2:
  109. assertRequestBody(t, "http://localhost/transition-regular.php", "Hello from regular thread")
  110. }
  111. }
  112. wg.Done()
  113. }(i)
  114. }
  115. wg.Wait()
  116. isRunning.Store(false)
  117. Shutdown()
  118. }
  119. // Note: this test is here since it would break compilation when put into the phpheaders package
  120. func TestAllCommonHeadersAreCorrect(t *testing.T) {
  121. for header, phpHeader := range phpheaders.CommonRequestHeaders {
  122. expectedPHPHeader := phpheaders.GetUnCommonHeader(header)
  123. assert.Equal(t, phpHeader+"\x00", expectedPHPHeader, "header is not well formed: "+phpHeader)
  124. }
  125. }
  126. func getDummyWorker(fileName string) *worker {
  127. if workers == nil {
  128. workers = make(map[string]*worker)
  129. }
  130. worker, _ := newWorker(workerOpt{
  131. fileName: testDataPath + "/" + fileName,
  132. num: 1,
  133. })
  134. return worker
  135. }
  136. func assertRequestBody(t *testing.T, url string, expected string) {
  137. r := httptest.NewRequest("GET", url, nil)
  138. w := httptest.NewRecorder()
  139. req, err := NewRequestWithContext(r, WithRequestDocumentRoot(testDataPath, false))
  140. assert.NoError(t, err)
  141. err = ServeHTTP(w, req)
  142. assert.NoError(t, err)
  143. resp := w.Result()
  144. body, _ := io.ReadAll(resp.Body)
  145. assert.Equal(t, expected, string(body))
  146. }