-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
164 lines (130 loc) · 3.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
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
package main
import (
"bytes"
"os"
"strings"
"syscall"
"testing"
)
func TestBadEnv(t *testing.T) {
buff := new(bytes.Buffer)
stderr = buff
code := 0
exit = func(c int) {
code = c
}
main()
if code != 1 {
t.Error("expected exit status 1")
}
if !strings.Contains(buff.String(), "COFFEE_SHOP_CLOSE_TIME") {
t.Error("Didn't provide close time, but didn't error")
}
code = 0
os.Setenv("COFFEE_SHOP_CLOSE_TIME", "1")
main()
if code != 1 {
t.Error("expected exit status 1")
}
if !strings.Contains(buff.String(), "COFFEE_SHOP_SHUTDOWN") {
t.Error("Didn't provide shutdown time, but didn't error")
}
code = 0
os.Setenv("COFFEE_SHOP_SHUTDOWN", "1")
main()
if code != 1 {
t.Error("expected exit status 1")
}
if !strings.Contains(buff.String(), "COFFEE_SHOP_CUSTOMERS") {
t.Error("Didn't provide number of customers, but didn't error")
}
code = 0
os.Setenv("COFFEE_SHOP_CUSTOMERS", "1")
main()
if code != 1 {
t.Error("expected exit status 1")
}
if !strings.Contains(buff.String(), "COFFEE_SHOP_BARISTAS") {
t.Error("Didn't provide number of baristas, but didn't error")
}
}
func TestStore(t *testing.T) {
buff := new(bytes.Buffer)
stderr = buff
code := -1
exit = func(c int) {
code = c
}
os.Setenv("COFFEE_SHOP_CLOSE_TIME", "1")
os.Setenv("COFFEE_SHOP_SHUTDOWN", "0")
os.Setenv("COFFEE_SHOP_CUSTOMERS", "1")
os.Setenv("COFFEE_SHOP_BARISTAS", "1")
main()
if code != 0 {
t.Error("Expected 0 exit status")
}
if s := buff.String(); !strings.Contains(s, "Store is closing") ||
!strings.Contains(s, "Customer 1 says Yum and thanks to Barista 1") {
t.Errorf("Output was not expected got %v", s)
}
}
func TestSignalStore(t *testing.T) {
buff := new(bytes.Buffer)
stderr = buff
code := -1
exit = func(c int) {
code = c
}
stop = make(chan os.Signal, 1)
os.Setenv("COFFEE_SHOP_CLOSE_TIME", "100")
os.Setenv("COFFEE_SHOP_SHUTDOWN", "90")
os.Setenv("COFFEE_SHOP_CUSTOMERS", "1")
os.Setenv("COFFEE_SHOP_BARISTAS", "1")
stop <- syscall.SIGINT
main()
if code != 0 {
t.Error("Expected 0 exit code")
}
if s := buff.String(); !strings.Contains(s, "I received a signal to close the store") ||
!strings.Contains(s, "Store closed") {
t.Error("Expected a store to close with all customers served")
}
}
func TestSignalStoreShutdownTimeout(t *testing.T) {
buff := new(bytes.Buffer)
stderr = buff
code := -1
exit = func(c int) {
code = c
}
stop = make(chan os.Signal, 1)
os.Setenv("COFFEE_SHOP_CLOSE_TIME", "100")
os.Setenv("COFFEE_SHOP_SHUTDOWN", "0")
os.Setenv("COFFEE_SHOP_CUSTOMERS", "1")
os.Setenv("COFFEE_SHOP_BARISTAS", "1")
stop <- syscall.SIGINT
main()
if code != 0 {
t.Error("Expected 0 exit code")
}
if s := buff.String(); !strings.Contains(s, "I received a signal to close the store") ||
!strings.Contains(s, "Shutdown time reached") {
t.Error("Expected a store to close with all customers served")
}
}
func TestCantOpenStore(t *testing.T) {
buff := new(bytes.Buffer)
stderr = buff
exitCode := 0
exit = func(code int) {
exitCode = code
}
os.Setenv("COFFEE_SHOP_CLOSE_TIME", "0")
os.Setenv("COFFEE_SHOP_SHUTDOWN", "0")
os.Setenv("COFFEE_SHOP_CUSTOMERS", "0")
os.Setenv("COFFEE_SHOP_BARISTAS", "0")
main()
if exitCode != 1 {
t.Errorf("Expected an exitCode of 1, but got %v", exitCode)
}
}