-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
272 lines (219 loc) · 11 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"fmt"
"reflect"
"runtime"
)
const dashes string = "------------------------------------------------------------------------------"
type CacheUsage int
const (
CacheDisabled CacheUsage = iota
CacheEnabled
NotSet
)
type algorithmToTest func(bool, CacheUsage, CacheUsage, CacheUsage) bool
// Helper function to get the name of a function
func getFunctionName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func main() {
var algorithm algorithmToTest
algorithm = isCacheEnabled
runAllTestsUsingAlgorithm(algorithm)
algorithm = isCacheEnabled_UsingOnlyBooleans
runAllTestsUsingAlgorithm(algorithm)
}
func runAllTestsUsingAlgorithm(algorithm algorithmToTest) {
fmt.Printf("\n%s\n%s\nrunning all tests using algorithm: %s\n%s\n%s\n", dashes, dashes, getFunctionName(algorithm), dashes, dashes)
testDescription := "server supports caching, but it was not enabled at any level"
serverLevelCachingSupported := true
runLevel := NotSet
componentLevel := NotSet
pipelineLevel := NotSet
expected := false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server set all caching unsupported, random settings at other levels"
serverLevelCachingSupported = false
runLevel = NotSet
componentLevel = NotSet
pipelineLevel = NotSet
expected = false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server set all caching unsupported, random settings at other levels 2"
serverLevelCachingSupported = false
runLevel = CacheEnabled
componentLevel = NotSet
pipelineLevel = CacheEnabled
expected = false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server set all caching unsupported, random settings at other levels 3"
serverLevelCachingSupported = false
runLevel = NotSet
componentLevel = CacheDisabled
pipelineLevel = CacheEnabled
expected = false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
// the following block of tests check for when only one level is set to CacheEnabled
testDescription = "server supports caching, enabled only at run level"
serverLevelCachingSupported = true
runLevel = CacheEnabled
componentLevel = NotSet
pipelineLevel = NotSet
expected = true
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server supports caching, enabled only at component level"
serverLevelCachingSupported = true
runLevel = NotSet
componentLevel = CacheEnabled
pipelineLevel = NotSet
expected = true
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server supports caching, enabled only at pipeline level"
serverLevelCachingSupported = true
runLevel = NotSet
componentLevel = NotSet
pipelineLevel = CacheEnabled
expected = true
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
// the following block of tests check for when a lower precedence level is set to CacheDisabled, but a higher precedence level is set to CacheEnabled.
// the higher precedence level should win
testDescription = "server supports caching, disabled at pipeline level, but enabled at run level (run level should take precedence)"
serverLevelCachingSupported = true
runLevel = CacheEnabled
componentLevel = NotSet
pipelineLevel = CacheDisabled
expected = true
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server supports caching, disabled at component level, but enabled at run level (run level should take precedence)"
serverLevelCachingSupported = true
runLevel = CacheEnabled
componentLevel = CacheDisabled
pipelineLevel = NotSet
expected = true
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server supports caching, disabled at pipeline level, but enabled at component level (component level should take precedence)"
serverLevelCachingSupported = true
runLevel = NotSet
componentLevel = CacheEnabled
pipelineLevel = CacheDisabled
expected = true
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
// the following block of tests is the same as above, but the requests are just flipped.
// so, check for when a lower precedence level is set to CacheEnabled, but a higher precedence level is set to CacheDisabled.
// the higher precedence level should win
testDescription = "server supports caching, enabled at pipeline level, but disabled at run level (run level should take precedence)"
serverLevelCachingSupported = true
runLevel = CacheDisabled
componentLevel = NotSet
pipelineLevel = CacheEnabled
expected = false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server supports caching, enabled at component level, but disabled at run level (run level should take precedence)"
serverLevelCachingSupported = true
runLevel = CacheDisabled
componentLevel = CacheEnabled
pipelineLevel = NotSet
expected = false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
testDescription = "server supports caching, enabled at pipeline level, but disabled at component level (component level should take precedence)"
serverLevelCachingSupported = true
runLevel = NotSet
componentLevel = CacheDisabled
pipelineLevel = CacheEnabled
expected = false
runTestAndPrintResults(algorithm, testDescription, serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel, expected)
// Notice that the previous block all work in `isCacheEnabled` but not in `isCacheEnabled_UsingOnlyBooleans` !!
// This is because the latter does not have the concept of NotSet.
//
// What happens is that any time a higher precendence level says "I definitely don't want cache",
// the lower precedence levels are winning when they are true!
// This happens because basically isCacheEnabled_UsingOnlyBooleans is a giant boolean OR !!
// It will say "yes I want cache" if ANY of the levels say they want cache, which is not what we want.
// The precedence is not respected when using only booleans.
}
func runTestAndPrintResults(algorithm algorithmToTest, testDescription string, serverLevelCachingSupported bool, runLevel CacheUsage, componentLevel CacheUsage, pipelineLevel CacheUsage, expected bool) {
fmt.Printf("\n\nrunning test case [%s]...\n%s\n", testDescription, dashes)
result := algorithm(serverLevelCachingSupported, runLevel, componentLevel, pipelineLevel)
fmt.Printf("isCacheEnabled: %v \t expected: %v\n", result, expected)
if result != expected {
fmt.Println("!!!!!!!!!!!!!!!! TEST FAILED !!!!!!!!!!!!!!!!")
} else {
fmt.Println("TEST PASSED")
}
}
///////////////////////////////////////////
// below are the various algorithms to test
///////////////////////////////////////////
// this algorithm uses switches with 3 values - CacheEnabled, CacheDisabled, NotSet
func isCacheEnabled(serverLevelCachingSupported bool, runLevel CacheUsage, componentLevel CacheUsage, pipelineLevel CacheUsage) bool {
if !serverLevelCachingSupported {
fmt.Println("caching is disabled for this component (will NOT try to use cache) because it was set to unsupported (globally disabled) at the server level")
return false
}
// if we're here, serverLevelCachingSupported was true
fmt.Println("the server-wide caching supported setting is true. Caching is not globally unsupported. Will proceed to check the other levels to see if caching is enabled in any of those")
if runLevel == CacheEnabled {
fmt.Println("will try to use cache because it was enabled at Run time")
return true
}
if runLevel == CacheDisabled {
fmt.Println("will NOT try to use cache because it was disabled at Run time")
return false
}
// if we're here, run level was NotSet
fmt.Println("no caching setting was found at the run level. Will now check the component level")
if componentLevel == CacheEnabled {
fmt.Println("will try to use cache because it was enabled at the component level")
return true
}
if componentLevel == CacheDisabled {
fmt.Println("will NOT try to use cache because it was disabled at the component level")
return false
}
// if we're here, run level was NotSet
fmt.Println("no caching setting was found at the component level. Will now check the pipeline level")
if pipelineLevel == CacheEnabled {
fmt.Println("will try to use cache because it was enabled at the pipeline level")
return true
}
if pipelineLevel == CacheDisabled {
fmt.Println("will NOT try to use cache because it was disabled at the pipeline level")
return false
}
// if we're here, pipeline level was NotSet
fmt.Println("no caching setting was found at the pipeline level")
// overall default case is here. Default is no cache!
fmt.Println("caching is disabled for this component (will NOT try to use cache) because it wasn't enabled at any level")
return false
}
// this algorithm uses booleans - no concept of NotSet
func isCacheEnabled_UsingOnlyBooleans(serverLevelCachingSupported bool, runLevel CacheUsage, componentLevel CacheUsage, pipelineLevel CacheUsage) bool {
if !serverLevelCachingSupported {
fmt.Println("caching is disabled for this component (will NOT try to use cache) because it was set to unsupported (globally disabled) at the server level")
return false
}
// if we're here, serverLevelCachingSupported was true
fmt.Println("the server-wide caching supported setting is true. Caching is not globally unsupported. Will proceed to check the other levels to see if caching is enabled in any of those")
// make the booleans
runLevelBoolean := runLevel == CacheEnabled
componentLevelBoolean := componentLevel == CacheEnabled
pipelineLevelBoolean := pipelineLevel == CacheEnabled
if runLevelBoolean {
fmt.Println("will try to use cache because it was enabled at Run time")
return true
}
if componentLevelBoolean {
fmt.Println("will try to use cache because it was enabled at the component level")
return true
}
if pipelineLevelBoolean {
fmt.Println("will try to use cache because it was enabled at the pipeline level")
return true
}
// notice that the above 3 ifs could have been written as a single boolean OR:
// if runLevelBoolean || componentLevelBoolean || pipelineLevelBoolean ... return true
// not what we want!
// overall default case is here. Default is no cache!
fmt.Println("caching is disabled for this component (will NOT try to use cache) because it wasn't enabled at any level")
return false
}