-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
107 lines (90 loc) · 2.26 KB
/
main_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
102
103
104
105
106
107
package main
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"syscall"
"testing"
"time"
)
func assertErrUserPrint(expectedPrint map[LogLevel]string) error {
memLog := bytes.Buffer{}
log.SetOutput(&memLog)
defer log.SetOutput(os.Stderr) // os.Stderr is the default output
for l, p := range expectedPrint {
errUserPrint(fmt.Errorf(p))
if !strings.Contains(memLog.String(), p) {
return fmt.Errorf("%s log level assertion failed", l)
}
}
return nil
}
func TestErrUserPrint(t *testing.T) {
expectedPrint := map[LogLevel]string{
logCritical: "Testing err user print",
logInfo: lobbySettings.supportMsg,
}
if err := assertErrUserPrint(expectedPrint); err != nil {
t.Errorf("errUserPrint test failed with failed assertion: %v", err)
}
}
func TestSIGHUP(t *testing.T) {
config := `lb:
`
testConfigPath := "/tmp/lobby_test_conf.yaml"
lobbySettings.configFilePath = testConfigPath
os.WriteFile(testConfigPath, []byte(config), 0400)
var err error
lbs, err := lbInit()
if err != nil {
t.Errorf("lbInit returned an unexpected error: '%v'", err)
}
wt := 2
time.Sleep(time.Duration(wt) * time.Second)
os.Remove(testConfigPath)
config = `lb:
- engine: testEngine
targets:
- name: testReconfig
protocol: tcp
port: 8082
upstream_group:
name: ug
distribution: round-robin
upstreams:
- name: testUpstream
host: 1.1.1.1
port: 80
`
os.WriteFile(testConfigPath, []byte(config), 0400)
signalHandler(syscall.SIGHUP, sSig, &lbs)
time.Sleep(time.Duration(wt) * time.Second)
if len(lbs) == 0 {
t.Errorf("SIGHUP reconfig failed")
}
os.Remove(testConfigPath)
config = `lb:
- engine: turbo
targets:
- name: testReconfig
protocol: tcp
port: 8082
upstream_group:
name: ug
distribution: round-robin
upstreams:
- name: testUpstream
host: 1.1.1.1
port: 80
`
os.WriteFile(testConfigPath, []byte(config), 0400)
signalHandler(syscall.SIGHUP, sSig, &lbs)
time.Sleep(time.Duration(wt) * time.Second)
if lbs[0].et.String() != "testEngine" {
t.Errorf("Expected engine type was 'testEngine', but got %v", lbs[0].et.String())
}
os.Remove(testConfigPath)
lbs[0].stop()
}