-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFeatureFlagCachingTests.kt
More file actions
371 lines (306 loc) · 14.1 KB
/
FeatureFlagCachingTests.kt
File metadata and controls
371 lines (306 loc) · 14.1 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package com.flagsmith
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.graphics.Color
import com.flagsmith.entities.Feature
import com.flagsmith.entities.Flag
import com.flagsmith.mockResponses.MockEndpoint
import com.flagsmith.mockResponses.mockDelayFor
import com.flagsmith.mockResponses.mockFailureFor
import com.flagsmith.mockResponses.mockResponseFor
import org.awaitility.Awaitility
import org.awaitility.kotlin.await
import org.awaitility.kotlin.untilNotNull
import org.awaitility.kotlin.untilTrue
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.mockserver.integration.ClientAndServer
import java.io.File
import java.time.Duration
import java.util.concurrent.atomic.AtomicBoolean
class FeatureFlagCachingTests {
private lateinit var mockServer: ClientAndServer
private lateinit var flagsmithWithCache: Flagsmith
private lateinit var flagsmithNoCache: Flagsmith
@Mock
private lateinit var mockApplicationContext: Context
@Mock
private lateinit var mockContextResources: Resources
@Mock
private lateinit var mockSharedPreferences: SharedPreferences
@Before
fun setup() {
mockServer = ClientAndServer.startClientAndServer()
System.setProperty("mockserver.logLevel", "INFO")
Awaitility.setDefaultTimeout(Duration.ofSeconds(30));
setupMocks()
val defaultFlags = listOf(
Flag(
feature = Feature(name = "Flag 1"),
enabled = true,
featureStateValue = "Vanilla Ice"
),
Flag(
feature = Feature(name = "Flag 2"),
enabled = true,
featureStateValue = "value2"
),
)
flagsmithWithCache = Flagsmith(
environmentKey = "",
baseUrl = "http://localhost:${mockServer.localPort}",
enableAnalytics = true, // Mix up the analytics flag to test initialisation
context = mockApplicationContext,
defaultFlags = defaultFlags,
cacheConfig = FlagsmithCacheConfig(enableCache = true)
)
flagsmithNoCache = Flagsmith(
environmentKey = "",
baseUrl = "http://localhost:${mockServer.localPort}",
enableAnalytics = false,
cacheConfig = FlagsmithCacheConfig(enableCache = false),
defaultFlags = defaultFlags
)
}
private fun setupMocks() {
MockitoAnnotations.initMocks(this)
`when`(mockApplicationContext.getResources()).thenReturn(mockContextResources)
`when`(mockApplicationContext.getSharedPreferences(anyString(), anyInt())).thenReturn(
mockSharedPreferences
)
`when`(mockApplicationContext.cacheDir).thenReturn(File("cache"))
`when`(mockContextResources.getString(anyInt())).thenReturn("mocked string")
`when`(mockContextResources.getStringArray(anyInt())).thenReturn(
arrayOf(
"mocked string 1",
"mocked string 2"
)
)
`when`(mockContextResources.getColor(anyInt())).thenReturn(Color.BLACK)
`when`(mockContextResources.getBoolean(anyInt())).thenReturn(false)
`when`(mockContextResources.getDimension(anyInt())).thenReturn(100f)
`when`(mockContextResources.getIntArray(anyInt())).thenReturn(intArrayOf(1, 2, 3))
`when`(mockApplicationContext.applicationContext).thenReturn(mockApplicationContext)
}
@After
fun tearDown() {
mockServer.stop()
}
@Test
fun testThrowsExceptionWhenEnableCachingWithoutAContext() {
val exception = Assert.assertThrows(IllegalArgumentException::class.java) {
val flagsmith = Flagsmith(
environmentKey = "",
baseUrl = "http://localhost:${mockServer.localPort}",
enableAnalytics = false,
cacheConfig = FlagsmithCacheConfig(enableCache = true),
)
}
Assert.assertEquals(
"Flagsmith requires a context to use the cache feature",
exception.message
)
}
@Test
fun testGetFeatureFlagsWithIdentityUsesCachedResponseOnSecondRequestFailure() {
mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES)
mockServer.mockFailureFor(MockEndpoint.GET_IDENTITIES)
// First time around we should be successful and cache the response
var foundFromServer: Flag? = null
flagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromServer }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(756.0, foundFromServer?.featureStateValue)
// Now we mock the failure and expect the cached response to be returned
var foundFromCache: Flag? = null
flagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue(result.isSuccess)
foundFromCache =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromCache }
Assert.assertNotNull(foundFromCache)
Assert.assertEquals(756.0, foundFromCache?.featureStateValue)
}
@Test
fun testGetFeatureFlagsWithIdentityUsesCachedResponseOnSecondRequestTimeout() {
mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES)
mockServer.mockDelayFor(MockEndpoint.GET_IDENTITIES)
// First time around we should be successful and cache the response
var foundFromServer: Flag? = null
flagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(756.0, foundFromServer?.featureStateValue)
}
await untilNotNull { foundFromServer }
// Now we mock the failure and expect the cached response to be returned
var foundFromCache: Flag? = null
flagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue(result.isSuccess)
foundFromCache =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromCache }
Assert.assertNotNull(foundFromCache)
Assert.assertEquals(756.0, foundFromCache?.featureStateValue)
}
@Test
fun testGetFeatureFlagsNoIdentityUsesCachedResponseOnSecondRequestFailure() {
mockServer.mockResponseFor(MockEndpoint.GET_FLAGS)
mockServer.mockFailureFor(MockEndpoint.GET_FLAGS)
// First time around we should be successful and cache the response
var foundFromServer: Flag? = null
flagsmithWithCache.getFeatureFlags() { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromServer }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(7.0, foundFromServer?.featureStateValue)
// Now we mock the failure and expect the cached response to be returned
var foundFromCache: Flag? = null
flagsmithWithCache.getFeatureFlags() { result ->
Assert.assertTrue(result.isSuccess)
foundFromCache =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromCache }
Assert.assertNotNull(foundFromCache)
Assert.assertEquals(7.0, foundFromCache?.featureStateValue)
}
@Test
fun testGetFlagsWithFailingRequestShouldGetDefaults() {
mockServer.mockFailureFor(MockEndpoint.GET_FLAGS)
mockServer.mockResponseFor(MockEndpoint.GET_FLAGS)
// First time around we should fail and fall back to the defaults
var foundFromCache: Flag? = null
flagsmithWithCache.getFeatureFlags() { result ->
Assert.assertTrue(result.isSuccess)
foundFromCache =
result.getOrThrow().find { flag -> flag.feature.name == "Flag 1" }
}
await untilNotNull { foundFromCache }
Assert.assertNotNull(foundFromCache)
// Now we mock the server and expect the server response to be returned
var foundFromServer: Flag? = null
flagsmithWithCache.getFeatureFlags() { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromServer }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(7.0, foundFromServer?.featureStateValue)
}
@Test
fun testGetFlagsWithTimeoutRequestShouldGetDefaults() {
mockServer.mockDelayFor(MockEndpoint.GET_FLAGS)
mockServer.mockResponseFor(MockEndpoint.GET_FLAGS)
// First time around we should get the default flag values
var foundFromCache: Flag? = null
flagsmithWithCache.getFeatureFlags() { result ->
Assert.assertTrue(result.isSuccess)
foundFromCache =
result.getOrThrow().find { flag -> flag.feature.name == "Flag 1" }
}
await untilNotNull { foundFromCache }
Assert.assertNotNull(foundFromCache)
Assert.assertEquals("Vanilla Ice", foundFromCache?.featureStateValue)
// Now we mock the successful request and expect the server values
var foundFromServer: Flag? = null
flagsmithWithCache.getFeatureFlags() { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromServer }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(7.0, foundFromServer?.featureStateValue)
}
@Test
fun testGetFeatureFlagsWithNewCachedFlagsmithGetsCachedValue() {
mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES)
mockServer.mockFailureFor(MockEndpoint.GET_IDENTITIES)
// First time around we should be successful and cache the response
var foundFromServer: Flag? = null
flagsmithWithCache.clearCache()
flagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromServer }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(756.0, foundFromServer?.featureStateValue)
// Now get a new Flagsmith instance with the same cache and expect the cached response to be returned
val newFlagsmithWithCache = Flagsmith(
environmentKey = "",
baseUrl = "http://localhost:${mockServer.localPort}",
enableAnalytics = false,
context = mockApplicationContext,
cacheConfig = FlagsmithCacheConfig(enableCache = true)
)
// Now we mock the failure and expect the cached response to be returned from the new flagsmith instance
var foundFromCache: Flag? = null
newFlagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue("The request will fail but we should be successful as we fall back on the cache", result.isSuccess)
foundFromCache =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromCache }
Assert.assertNotNull(foundFromCache)
Assert.assertEquals(756.0, foundFromCache?.featureStateValue)
}
@Test
fun testGetFeatureFlagsWithNewCachedFlagsmithDoesntGetCachedValueWhenWeClearTheCache() {
mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES)
mockServer.mockFailureFor(MockEndpoint.GET_IDENTITIES)
// First time around we should be successful and cache the response
var foundFromServer: Flag? = null
flagsmithWithCache.clearCache()
flagsmithWithCache.getFeatureFlags(identity = "person") { result ->
Assert.assertTrue(result.isSuccess)
foundFromServer =
result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
}
await untilNotNull { foundFromServer }
Assert.assertNotNull(foundFromServer)
Assert.assertEquals(756.0, foundFromServer?.featureStateValue)
// Now get a new Flagsmith instance with the same cache and evict the cache straight away
val newFlagsmithWithClearedCache = Flagsmith(
environmentKey = "",
baseUrl = "http://localhost:${mockServer.localPort}",
enableAnalytics = false,
context = mockApplicationContext,
cacheConfig = FlagsmithCacheConfig(enableCache = true)
)
newFlagsmithWithClearedCache.clearCache()
// Now we mock the failure and expect the get to fail as we don't have the cache to fall back on
var foundFromCache: Flag? = null
val hasFinishedGetRequest = AtomicBoolean(false)
newFlagsmithWithClearedCache.getFeatureFlags(identity = "person") { result ->
Assert.assertFalse("This un-cached response should fail", result.isSuccess)
foundFromCache =
result.getOrNull()?.find { flag -> flag.feature.name == "with-value" }
hasFinishedGetRequest.set(true)
}
await untilTrue(hasFinishedGetRequest)
Assert.assertNull("Shouldn't get any data back as we don't have a cache", foundFromCache)
}
}