phpmainthread_test.go 4.3 KB

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