-
Notifications
You must be signed in to change notification settings - Fork 42
/
manager_test.go
101 lines (86 loc) · 2.3 KB
/
manager_test.go
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
package faktory_worker
import (
"errors"
"log"
"os"
"syscall"
"testing"
"time"
faktory "github.com/contribsys/faktory/client"
"github.com/contribsys/faktory/util"
"github.com/stretchr/testify/assert"
)
func TestManagerSetup(t *testing.T) {
clx, err := faktory.Open()
startsz := 0.0
if err == nil {
info, err := clx.Info()
if err == nil {
startsz = info["faktory"].(map[string]interface{})["tasks"].(map[string]interface{})["Workers"].(map[string]interface{})["size"].(float64)
}
}
mgr := NewManager()
err = mgr.setUpWorkerProcess()
assert.NoError(t, err)
startupCalled := false
mgr.On(Startup, func(m *Manager) error {
startupCalled = true
assert.NotNil(t, m)
return nil
})
mgr.fireEvent(Startup)
assert.True(t, startupCalled)
withServer(t, "oss", mgr, func(cl *faktory.Client) error {
info, err := cl.Info()
assert.NoError(t, err)
sz := info["faktory"].(map[string]interface{})["tasks"].(map[string]interface{})["Workers"].(map[string]interface{})["size"].(float64)
assert.EqualValues(t, startsz+1, sz)
return nil
})
assert.Equal(t, "", mgr.handleEvent("quiet"))
time.Sleep(1 * time.Millisecond)
assert.Equal(t, "quiet", mgr.handleEvent("quiet"))
devnull, err := os.OpenFile("/dev/null", os.O_WRONLY, 0)
assert.NoError(t, err)
logg := &StdLogger{
log.New(devnull, "", 0),
}
mgr.Logger = logg
assert.Equal(t, "", mgr.handleEvent("dump"))
terminateCalled := false
mgr.On(Shutdown, func(m *Manager) error {
terminateCalled = true
assert.NotNil(t, m)
return nil
})
mgr.Terminate(false)
assert.Equal(t, true, terminateCalled)
// calling terminate again should be a noop
terminateCalled = false
mgr.Terminate(false)
assert.Equal(t, false, terminateCalled)
}
func withServer(t *testing.T, lvl string, mgr *Manager, fn func(cl *faktory.Client) error) {
err := mgr.with(func(cl *faktory.Client) error {
if lvl == "oss" {
return fn(cl)
}
hash, err := cl.Info()
if err != nil {
return err
}
desc := hash["server"].(map[string]interface{})["description"].(string)
if lvl == "ent" && desc == "Faktory Enterprise" {
return fn(cl)
} else if lvl == "pro" && desc != "Faktory" {
return fn(cl)
}
return nil
})
if errors.Is(err, syscall.ECONNREFUSED) {
util.Debug("Server not running, skipping...")
return
} else {
assert.NoError(t, err)
}
}