summaryrefslogtreecommitdiff
path: root/supervisor_test.go
blob: 34172d7d33058705644589e3a0888b5a0bc3e0ac (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
143
144
145
146
147
148
149
150
package roadrunner

import (
	"context"
	"os/exec"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

var cfgSupervised = Config{
	NumWorkers:      int64(1),
	AllocateTimeout: time.Second,
	DestroyTimeout:  time.Second,
	Supervisor: &SupervisorConfig{
		WatchTick:       1,
		TTL:             100,
		IdleTTL:         100,
		ExecTTL:         100,
		MaxWorkerMemory: 100,
	},
}

func TestSupervisedPool_Exec(t *testing.T) {
	ctx := context.Background()
	p, err := NewPool(
		ctx,
		func() *exec.Cmd { return exec.Command("php", "tests/memleak.php", "pipes") },
		NewPipeFactory(),
		cfgSupervised,
	)

	assert.NoError(t, err)
	assert.NotNil(t, p)
	stopCh := make(chan struct{})
	defer p.Destroy(context.Background())

	go func() {
		for {
			select {
			case <-stopCh:
				return
			default:
				workers := p.Workers()
				if len(workers) > 0 {
					s, err := WorkerProcessState(workers[0])
					assert.NoError(t, err)
					assert.NotNil(t, s)
					// since this is soft limit, double max memory limit watch
					if (s.MemoryUsage / MB) > cfgSupervised.Supervisor.MaxWorkerMemory*2 {
						assert.Fail(t, "max memory reached")
					}
				}
			}
		}
	}()

	for i := 0; i < 100; i++ {
		time.Sleep(time.Millisecond * 50)
		_, err = p.Exec(Payload{
			Context: []byte(""),
			Body:    []byte("foo"),
		})
		assert.NoError(t, err)
	}

	stopCh <- struct{}{}
}

func TestSupervisedPool_ExecTTL_TimedOut(t *testing.T) {
	var cfgExecTTL = Config{
		NumWorkers:      int64(1),
		AllocateTimeout: time.Second,
		DestroyTimeout:  time.Second,
		Supervisor: &SupervisorConfig{
			WatchTick:       1,
			TTL:             100,
			IdleTTL:         100,
			ExecTTL:         1,
			MaxWorkerMemory: 100,
		},
	}
	ctx := context.Background()
	p, err := NewPool(
		ctx,
		func() *exec.Cmd { return exec.Command("php", "tests/sleep.php", "pipes") },
		NewPipeFactory(),
		cfgExecTTL,
	)

	assert.NoError(t, err)
	assert.NotNil(t, p)
	defer p.Destroy(context.Background())

	pid := p.Workers()[0].Pid()

	resp, err := p.ExecWithContext(context.Background(), Payload{
		Context: []byte(""),
		Body:    []byte("foo"),
	})

	assert.Error(t, err)
	assert.Empty(t, resp)

	time.Sleep(time.Second * 1)
	// should be new worker with new pid
	assert.NotEqual(t, pid, p.Workers()[0].Pid())
}

func TestSupervisedPool_ExecTTL_OK(t *testing.T) {
	var cfgExecTTL = Config{
		NumWorkers:      int64(1),
		AllocateTimeout: time.Second,
		DestroyTimeout:  time.Second,
		Supervisor: &SupervisorConfig{
			WatchTick:       1,
			TTL:             100,
			IdleTTL:         100,
			ExecTTL:         4,
			MaxWorkerMemory: 100,
		},
	}
	ctx := context.Background()
	p, err := NewPool(
		ctx,
		func() *exec.Cmd { return exec.Command("php", "tests/sleep.php", "pipes") },
		NewPipeFactory(),
		cfgExecTTL,
	)

	assert.NoError(t, err)
	assert.NotNil(t, p)
	defer p.Destroy(context.Background())

	pid := p.Workers()[0].Pid()

	time.Sleep(time.Millisecond * 100)
	resp, err := p.Exec(Payload{
		Context: []byte(""),
		Body:    []byte("foo"),
	})

	assert.NoError(t, err)
	assert.Empty(t, resp)

	time.Sleep(time.Second * 1)
	// should be the same pid
	assert.Equal(t, pid, p.Workers()[0].Pid())
}