-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
176 lines (155 loc) · 4.3 KB
/
config_test.go
File metadata and controls
176 lines (155 loc) · 4.3 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package parallel
import (
"bytes"
"os"
"strings"
"testing"
)
func TestConfigSet(t *testing.T) {
cfg := &config{}
out := &bytes.Buffer{}
WithStdout(out).apply(cfg)
if cfg.stdout != out {
t.Error("stdout io.Writer not set")
}
err := &bytes.Buffer{}
WithStderr(err).apply(cfg)
if cfg.stderr != err {
t.Error("stderr io.Writer not set")
}
sep := "====\n"
WithStdoutSeparator(sep).apply(cfg)
if string(cfg.outSep) != sep {
t.Error("Separator not set", string(cfg.outSep), sep)
}
WithStderrSeparator(sep).apply(cfg)
if string(cfg.errSep) != sep {
t.Error("Separator not set", string(cfg.errSep), sep)
}
LimitMemoryPerRunner(1024).apply(cfg)
if cfg.limitMemory != 1024 {
t.Error("limitMemory not set", cfg.limitMemory)
}
LimitActiveRunners(15).apply(cfg)
if cfg.limitRunners != 15 {
t.Error("limitRunners not set", cfg.limitRunners)
}
OrderStderr(true).apply(cfg)
if cfg.orderStderr != true {
t.Error("orderStderr not set")
}
OrderRunners(true).apply(cfg)
if cfg.orderRunners != true {
t.Error("orderRunners not set")
}
Passthru(true).apply(cfg)
if cfg.passthru != true {
t.Error("passthru not set")
}
}
func TestConfigForeground(t *testing.T) {
grp, err := NewGroup()
if err != nil {
t.Fatal("Unexpected error", err)
}
if !grp.foregroundAllowed() {
t.Error("foregroundAllowed should be the default setting of NewGroup()")
}
grp, err = NewGroup(OrderStderr(true))
if err != nil {
t.Fatal("Unexpected error", err)
}
if grp.foregroundAllowed() {
t.Error("foregroundAllowed should be false with OrderStderr(true)")
}
grp, err = NewGroup(OrderRunners(false))
if err != nil {
t.Fatal("Unexpected error", err)
}
if grp.foregroundAllowed() {
t.Error("foregroundAllowed should be false with OrderRunners(false)")
}
}
func TestConfigValidCombos(t *testing.T) {
grp, err := NewGroup()
if err != nil {
t.Fatal("Unexpected error", err)
}
grp, err = NewGroup(LimitMemoryPerRunner(100), LimitActiveRunners(1))
if err != nil {
t.Fatal("Unexpected error", err)
}
if grp.limitMemory != 100 {
t.Error("Default Group should allow a memory quota")
}
grp, err = NewGroup(LimitActiveRunners(10))
if err != nil {
t.Fatal("Unexpected error", err)
}
if grp.limitRunners != 10 {
t.Error("LimitActiveRunners(10) did not stick", grp.limitRunners)
}
}
// Test illegal config combinations
func TestConfigConflicts(t *testing.T) {
type testCase struct {
limitMemory uint64
limitRunners uint
orderRunners bool
orderStderr bool
passthru bool
error string
}
testCases := []testCase{
/* 0 */ {0, 0, false, false, false, ""},
/* 1 */ {100, 0, false, false, false, "Must set LimitActiveRunners"},
/* 2 */ {100, 1, false, false, false, "LimitMemoryPerRunner with OrderRunners"},
/* 3 */ {100, 1, true, false, false, ""},
/* 4 */ {100, 1, true, true, false, "LimitMemoryPerRunner with OrderStderr"},
/* 5 */ {100, 1, false, true, false, "LimitMemoryPerRunner with OrderRunners"},
/* 6 */ {100, 1, true, false, true, "LimitMemoryPerRunner with Passthru"},
/* 7 */ {100, 1, true, false, true, "LimitMemoryPerRunner with Passthru"},
/* 8 */ {0, 0, true, false, true, "OrderRunners with Passthru"},
/* 9 */ {0, 0, false, true, true, "OrderStderr with Passthru"},
}
for ix, tc := range testCases {
_, err := NewGroup(LimitMemoryPerRunner(tc.limitMemory),
LimitActiveRunners(tc.limitRunners),
OrderRunners(tc.orderRunners),
OrderStderr(tc.orderStderr),
Passthru(tc.passthru))
if err == nil {
if len(tc.error) != 0 {
t.Errorf("Case %d: Missing error. Expected '%s'", ix, tc.error)
}
continue
}
if len(tc.error) == 0 {
t.Errorf("Case %d: Unexpected error '%s'", ix, err.Error())
continue
}
if !strings.Contains(err.Error(), tc.error) {
t.Errorf("Case %d: Wrong error. Expected '%s', got '%s'",
ix, tc.error, err.Error())
}
}
}
func TestConfigNilWriters(t *testing.T) {
cfg := &config{}
err := WithStdout(os.Stdout).apply(cfg)
if err != nil {
t.Error("Unexpected error setting WithStdout", err)
}
err = WithStderr(os.Stderr).apply(cfg)
if err != nil {
t.Error("Unexpected error setting WithStderr", err)
}
err = WithStdout(nil).apply(cfg)
if err == nil {
t.Error("Expected error setting WithStdout(nil)")
}
err = WithStderr(nil).apply(cfg)
if err == nil {
t.Error("Expected error setting WithStderr(nil)", err)
}
}