summaryrefslogtreecommitdiff
path: root/pkg/worker_watcher/stack_test.go
blob: 5287a6dcfea4eda5d0742b474e340bc53399f6f9 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package worker_watcher //nolint:golint,stylecheck
import (
	"context"
	"os/exec"
	"testing"
	"time"

	"github.com/spiral/roadrunner/v2/pkg/worker"
	"github.com/stretchr/testify/assert"
)

func TestNewWorkersStack(t *testing.T) {
	stack := NewWorkersStack(0)
	assert.Equal(t, uint64(0), stack.actualNumOfWorkers)
	assert.Equal(t, []*worker.SyncWorkerImpl{}, stack.workers)
}

func TestStack_Push(t *testing.T) {
	stack := NewWorkersStack(1)

	w, err := worker.InitBaseWorker(&exec.Cmd{})
	assert.NoError(t, err)

	sw := worker.From(w)

	stack.Push(sw)
	assert.Equal(t, uint64(1), stack.actualNumOfWorkers)
}

func TestStack_Pop(t *testing.T) {
	stack := NewWorkersStack(1)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")

	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)

	sw := worker.From(w)

	stack.Push(sw)
	assert.Equal(t, uint64(1), stack.actualNumOfWorkers)

	_, _ = stack.Pop()
	assert.Equal(t, uint64(0), stack.actualNumOfWorkers)
}

func TestStack_FindAndRemoveByPid(t *testing.T) {
	stack := NewWorkersStack(1)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")
	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)

	assert.NoError(t, w.Start())

	sw := worker.From(w)

	stack.Push(sw)
	assert.Equal(t, uint64(1), stack.actualNumOfWorkers)

	stack.FindAndRemoveByPid(w.Pid())
	assert.Equal(t, uint64(0), stack.actualNumOfWorkers)
}

func TestStack_IsEmpty(t *testing.T) {
	stack := NewWorkersStack(1)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")

	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)

	sw := worker.From(w)
	stack.Push(sw)

	assert.Equal(t, uint64(1), stack.actualNumOfWorkers)

	assert.Equal(t, false, stack.IsEmpty())
}

func TestStack_Workers(t *testing.T) {
	stack := NewWorkersStack(1)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")
	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)
	assert.NoError(t, w.Start())

	sw := worker.From(w)
	stack.Push(sw)

	wrks := stack.Workers()
	assert.Equal(t, 1, len(wrks))
	assert.Equal(t, w.Pid(), wrks[0].Pid())
}

func TestStack_Reset(t *testing.T) {
	stack := NewWorkersStack(1)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")
	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)
	assert.NoError(t, w.Start())

	sw := worker.From(w)
	stack.Push(sw)

	assert.Equal(t, uint64(1), stack.actualNumOfWorkers)
	stack.Reset()
	assert.Equal(t, uint64(0), stack.actualNumOfWorkers)
}

func TestStack_Destroy(t *testing.T) {
	stack := NewWorkersStack(1)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")
	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)
	assert.NoError(t, w.Start())

	sw := worker.From(w)
	stack.Push(sw)

	stack.Destroy(context.Background())
	assert.Equal(t, uint64(0), stack.actualNumOfWorkers)
}

func TestStack_DestroyWithWait(t *testing.T) {
	stack := NewWorkersStack(2)
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")
	w, err := worker.InitBaseWorker(cmd)
	assert.NoError(t, err)
	assert.NoError(t, w.Start())

	sw := worker.From(w)
	stack.Push(sw)
	stack.Push(sw)
	assert.Equal(t, uint64(2), stack.actualNumOfWorkers)

	go func() {
		wrk, _ := stack.Pop()
		time.Sleep(time.Second * 3)
		stack.Push(wrk)
	}()
	time.Sleep(time.Second)
	stack.Destroy(context.Background())
	assert.Equal(t, uint64(0), stack.actualNumOfWorkers)
}