summaryrefslogtreecommitdiff
path: root/pkg/worker_watcher/stack_test.go
blob: 7cdb88f10a233f9fba20e65bdd4cc499c373a3e4 (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
package worker_watcher //nolint:golint,stylecheck
import (
	"context"
	"os/exec"
	"testing"

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

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

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

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

	stack.Push(w)
	assert.Equal(t, int64(1), stack.actualNumOfWorkers)
}

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

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

	stack.Push(w)
	assert.Equal(t, int64(1), stack.actualNumOfWorkers)

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

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

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

	stack.Push(w)
	assert.Equal(t, int64(1), stack.actualNumOfWorkers)

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

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

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

	stack.Push(w)
	assert.Equal(t, int64(1), stack.actualNumOfWorkers)

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

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

	stack.Push(w)

	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()
	cmd := exec.Command("php", "../tests/client.php", "echo", "pipes")
	w, err := workerImpl.InitBaseWorker(cmd)
	assert.NoError(t, err)
	assert.NoError(t, w.Start())

	stack.Push(w)
	assert.Equal(t, int64(1), stack.actualNumOfWorkers)
	stack.Reset()
	assert.Equal(t, int64(0), stack.actualNumOfWorkers)
}

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

	stack.Push(w)
	stack.Destroy(context.Background())
	assert.Equal(t, int64(0), stack.actualNumOfWorkers)
}