scaling_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package frankenphp
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "go.uber.org/zap"
  7. )
  8. func TestScaleARegularThreadUpAndDown(t *testing.T) {
  9. assert.NoError(t, Init(
  10. WithNumThreads(1),
  11. WithMaxThreads(2),
  12. WithLogger(zap.NewNop()),
  13. ))
  14. autoScaledThread := phpThreads[1]
  15. // scale up
  16. scaleRegularThread()
  17. assert.Equal(t, stateReady, autoScaledThread.state.get())
  18. assert.IsType(t, &regularThread{}, autoScaledThread.handler)
  19. // on down-scale, the thread will be marked as inactive
  20. setLongWaitTime(autoScaledThread)
  21. deactivateThreads()
  22. assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
  23. Shutdown()
  24. }
  25. func TestScaleAWorkerThreadUpAndDown(t *testing.T) {
  26. workerPath := testDataPath + "/transition-worker-1.php"
  27. assert.NoError(t, Init(
  28. WithNumThreads(2),
  29. WithMaxThreads(3),
  30. WithWorkers(workerPath, 1, map[string]string{}, []string{}),
  31. WithLogger(zap.NewNop()),
  32. ))
  33. autoScaledThread := phpThreads[2]
  34. // scale up
  35. scaleWorkerThread(workers[workerPath])
  36. assert.Equal(t, stateReady, autoScaledThread.state.get())
  37. // on down-scale, the thread will be marked as inactive
  38. setLongWaitTime(autoScaledThread)
  39. deactivateThreads()
  40. assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
  41. Shutdown()
  42. }
  43. func setLongWaitTime(thread *phpThread) {
  44. thread.state.mu.Lock()
  45. thread.state.waitingSince = time.Now().Add(-time.Hour)
  46. thread.state.mu.Unlock()
  47. }