-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathscaling_test.go
More file actions
90 lines (70 loc) · 2.37 KB
/
scaling_test.go
File metadata and controls
90 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package frankenphp
import (
"path/filepath"
"testing"
"time"
"github.com/dunglas/frankenphp/internal/state"
"github.com/stretchr/testify/assert"
)
func TestScaleARegularThreadUpAndDown(t *testing.T) {
t.Cleanup(Shutdown)
assert.NoError(t, Init(
WithNumThreads(1),
WithMaxThreads(2),
))
autoScaledThread := phpThreads[1]
// scale up
scaleRegularThread()
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
assert.IsType(t, ®ularThread{}, autoScaledThread.handler)
// on down-scale, the thread will be marked as inactive
setLongWaitTime(t, autoScaledThread)
deactivateThreads()
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
}
func TestScaleAWorkerThreadUpAndDown(t *testing.T) {
t.Cleanup(Shutdown)
workerName := "worker1"
workerPath := filepath.Join(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),
),
))
autoScaledThread := phpThreads[2]
// scale up
scaleWorkerThread(workersByPath[workerPath])
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
// on down-scale, the thread will be marked as inactive
setLongWaitTime(t, autoScaledThread)
deactivateThreads()
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
}
func TestMaxIdleTimePreventsEarlyDeactivation(t *testing.T) {
t.Cleanup(Shutdown)
assert.NoError(t, Init(
WithNumThreads(1),
WithMaxThreads(2),
WithMaxIdleTime(time.Hour),
))
autoScaledThread := phpThreads[1]
// scale up
scaleRegularThread()
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
// set wait time to 30 minutes (less than 1 hour max idle time)
autoScaledThread.state.SetWaitTime(time.Now().Add(-30 * time.Minute))
deactivateThreads()
assert.IsType(t, ®ularThread{}, autoScaledThread.handler, "thread should still be active after 30min with 1h max idle time")
// set wait time to over 1 hour (exceeds max idle time)
autoScaledThread.state.SetWaitTime(time.Now().Add(-time.Hour - time.Minute))
deactivateThreads()
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler, "thread should be deactivated after exceeding max idle time")
}
func setLongWaitTime(t *testing.T, thread *phpThread) {
t.Helper()
thread.state.SetWaitTime(time.Now().Add(-time.Hour))
}