Files
frankenphp/scaling_test.go
Alexander Stecher 98573ed7c0 refactor: extract the state module and make the backoff error instead of panic
This PR:
- moves state.go to its own module
- moves the phpheaders test the phpheaders module
- simplifies backoff.go
- makes the backoff error instead of panic (so it can be tested)
- removes some unused C structs
2025-12-02 23:10:12 +01:00

66 lines
1.6 KiB
Go

package frankenphp
import (
"io"
"log/slog"
"testing"
"time"
"github.com/dunglas/frankenphp/internal/state"
"github.com/stretchr/testify/assert"
)
func TestScaleARegularThreadUpAndDown(t *testing.T) {
assert.NoError(t, Init(
WithNumThreads(1),
WithMaxThreads(2),
WithLogger(slog.New(slog.NewTextHandler(io.Discard, nil))),
))
autoScaledThread := phpThreads[1]
// scale up
scaleRegularThread()
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
assert.IsType(t, &regularThread{}, autoScaledThread.handler)
// on down-scale, the thread will be marked as inactive
setLongWaitTime(autoScaledThread)
deactivateThreads()
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
Shutdown()
}
func TestScaleAWorkerThreadUpAndDown(t *testing.T) {
workerName := "worker1"
workerPath := testDataPath + "/transition-worker-1.php"
assert.NoError(t, Init(
WithNumThreads(2),
WithMaxThreads(3),
WithWorkers(workerName, workerPath, 1,
WithWorkerEnv(map[string]string{}),
WithWorkerWatchMode([]string{}),
WithWorkerMaxFailures(0),
),
WithLogger(slog.New(slog.NewTextHandler(io.Discard, nil))),
))
autoScaledThread := phpThreads[2]
// scale up
scaleWorkerThread(getWorkerByPath(workerPath))
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
// on down-scale, the thread will be marked as inactive
setLongWaitTime(autoScaledThread)
deactivateThreads()
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
Shutdown()
}
func setLongWaitTime(thread *phpThread) {
thread.state.SetWaitTime(time.Now().Add(-time.Hour))
}