Files
frankenphp/scaling_test.go
Indra Gunawan 87315a19ae feat: introduces worker name option, use label on worker metrics instead (#1376)
* add worker name option and use it in logs and metrics, update tests

* fix missing reference for collector

* update tests

* update docs

* fix conflict

* add missing allowedDirectives

* update tests
2025-03-22 12:32:59 +01:00

62 lines
1.4 KiB
Go

package frankenphp
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func TestScaleARegularThreadUpAndDown(t *testing.T) {
assert.NoError(t, Init(
WithNumThreads(1),
WithMaxThreads(2),
WithLogger(zap.NewNop()),
))
autoScaledThread := phpThreads[1]
// scale up
scaleRegularThread()
assert.Equal(t, stateReady, 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, map[string]string{}, []string{}),
WithLogger(zap.NewNop()),
))
autoScaledThread := phpThreads[2]
// scale up
scaleWorkerThread(workers[workerPath])
assert.Equal(t, stateReady, 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.mu.Lock()
thread.state.waitingSince = time.Now().Add(-time.Hour)
thread.state.mu.Unlock()
}