-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEppoClientTest.java
More file actions
344 lines (289 loc) · 12.3 KB
/
EppoClientTest.java
File metadata and controls
344 lines (289 loc) · 12.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
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
package cloud.eppo;
import static cloud.eppo.helpers.AssignmentTestCase.parseTestCaseFile;
import static cloud.eppo.helpers.AssignmentTestCase.runTestCase;
import static cloud.eppo.helpers.BanditTestCase.parseBanditTestCaseFile;
import static cloud.eppo.helpers.BanditTestCase.runBanditTestCase;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import cloud.eppo.api.Attributes;
import cloud.eppo.api.BanditActions;
import cloud.eppo.api.BanditResult;
import cloud.eppo.helpers.AssignmentTestCase;
import cloud.eppo.helpers.BanditTestCase;
import cloud.eppo.helpers.TestUtils;
import cloud.eppo.logging.Assignment;
import cloud.eppo.logging.AssignmentLogger;
import cloud.eppo.logging.BanditAssignment;
import cloud.eppo.logging.BanditLogger;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import java.io.File;
import java.lang.reflect.Field;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
@ExtendWith(WireMockExtension.class)
public class EppoClientTest {
private static final int TEST_PORT = 4001;
private static final String TEST_HOST = "http://localhost:" + TEST_PORT;
private static WireMockServer mockServer;
private static final String DUMMY_FLAG_API_KEY = "dummy-flags-api-key"; // Will load flags-v1
private static final String DUMMY_BANDIT_API_KEY =
"dummy-bandits-api-key"; // Will load bandit-flags-v1
private AssignmentLogger mockAssignmentLogger;
private BanditLogger mockBanditLogger;
@BeforeAll
public static void initMockServer() {
mockServer = new WireMockServer(TEST_PORT);
mockServer.start();
// If we get the dummy flag API key, return flags-v1.json
String ufcFlagsResponseJson = readConfig("src/test/resources/shared/ufc/flags-v1.json");
mockServer.stubFor(
WireMock.get(
WireMock.urlMatching(
".*flag-config/v1/config\\?.*apiKey=" + DUMMY_FLAG_API_KEY + ".*"))
.willReturn(WireMock.okJson(ufcFlagsResponseJson)));
// If we get the dummy bandit API key, return bandit-flags-v1.json
String banditFlagsResponseJson =
readConfig("src/test/resources/shared/ufc/bandit-flags-v1.json");
mockServer.stubFor(
WireMock.get(
WireMock.urlMatching(
".*flag-config/v1/config\\?.*apiKey=" + DUMMY_BANDIT_API_KEY + ".*"))
.willReturn(WireMock.okJson(banditFlagsResponseJson)));
// Return bandit models (no need to switch on API key)
String banditModelsResponseJson =
readConfig("src/test/resources/shared/ufc/bandit-models-v1.json");
mockServer.stubFor(
WireMock.get(WireMock.urlMatching(".*flag-config/v1/bandits\\?.*"))
.willReturn(WireMock.okJson(banditModelsResponseJson)));
}
private static String readConfig(String jsonToReturnFilePath) {
File mockResponseFile = new File(jsonToReturnFilePath);
try {
return FileUtils.readFileToString(mockResponseFile, "UTF8");
} catch (Exception e) {
throw new RuntimeException("Error reading mock data: " + e.getMessage(), e);
}
}
@AfterEach
public void cleanUp() {
TestUtils.setBaseClientHttpClientOverrideField(null);
EppoClient.stopPolling();
}
@AfterAll
public static void tearDown() {
if (mockServer != null) {
mockServer.stop();
}
}
@ParameterizedTest
@MethodSource("getAssignmentTestData")
public void testUnobfuscatedAssignments(File testFile) {
AssignmentTestCase testCase = parseTestCaseFile(testFile);
EppoClient eppoClient = initClient(DUMMY_FLAG_API_KEY);
runTestCase(testCase, eppoClient);
}
private static Stream<Arguments> getAssignmentTestData() {
return AssignmentTestCase.getAssignmentTestData();
}
@ParameterizedTest
@MethodSource("getBanditTestData")
public void testUnobfuscatedBanditAssignments(File testFile) {
BanditTestCase testCase = parseBanditTestCaseFile(testFile);
EppoClient eppoClient = initClient(DUMMY_BANDIT_API_KEY);
runBanditTestCase(testCase, eppoClient);
}
private static Stream<Arguments> getBanditTestData() {
return BanditTestCase.getBanditTestData();
}
@SuppressWarnings("ExtractMethodRecommender")
@Test
public void testLoggers() {
EppoClient eppoClient = initClient(DUMMY_BANDIT_API_KEY);
String flagKey = "banner_bandit_flag";
String subjectKey = "bob";
Attributes subjectAttributes = new Attributes();
subjectAttributes.put("age", 25);
subjectAttributes.put("country", "USA");
subjectAttributes.put("gender_identity", "female");
BanditActions actions = new BanditActions();
Attributes nikeAttributes = new Attributes();
nikeAttributes.put("brand_affinity", 1.5);
nikeAttributes.put("loyalty_tier", "silver");
actions.put("nike", nikeAttributes);
Attributes adidasAttributes = new Attributes();
adidasAttributes.put("brand_affinity", -1.0);
adidasAttributes.put("loyalty_tier", "bronze");
actions.put("adidas", adidasAttributes);
Attributes rebookAttributes = new Attributes();
rebookAttributes.put("brand_affinity", 0.5);
rebookAttributes.put("loyalty_tier", "gold");
actions.put("reebok", rebookAttributes);
BanditResult banditResult =
eppoClient.getBanditAction(flagKey, subjectKey, subjectAttributes, actions, "control");
// Verify assignment
assertEquals("banner_bandit", banditResult.getVariation());
assertEquals("adidas", banditResult.getAction());
// Verify experiment assignment logger called
ArgumentCaptor<Assignment> assignmentLogCaptor = ArgumentCaptor.forClass(Assignment.class);
verify(mockAssignmentLogger, times(1)).logAssignment(assignmentLogCaptor.capture());
// Verify bandit logger called
ArgumentCaptor<BanditAssignment> banditLogCaptor =
ArgumentCaptor.forClass(BanditAssignment.class);
verify(mockBanditLogger, times(1)).logBanditAssignment(banditLogCaptor.capture());
}
@Test
public void getInstanceWhenUninitialized() {
uninitClient();
assertThrows(RuntimeException.class, EppoClient::getInstance);
}
@Test
public void testErrorGracefulModeOn() {
initBuggyClient();
EppoClient.getInstance().setIsGracefulFailureMode(true);
assertEquals(
1.234, EppoClient.getInstance().getDoubleAssignment("numeric_flag", "subject1", 1.234));
}
@Test
public void testErrorGracefulModeOff() {
initBuggyClient();
EppoClient.getInstance().setIsGracefulFailureMode(false);
assertThrows(
Exception.class,
() -> EppoClient.getInstance().getDoubleAssignment("numeric_flag", "subject1", 1.234));
}
@Test
public void testReinitializeWithoutForcing() {
EppoClient firstInstance = initClient(DUMMY_FLAG_API_KEY);
EppoClient secondInstance = new EppoClient.Builder().apiKey(DUMMY_FLAG_API_KEY).buildAndInit();
assertSame(firstInstance, secondInstance);
}
@Test
public void testReinitializeWitForcing() {
EppoClient firstInstance = initClient(DUMMY_FLAG_API_KEY);
EppoClient secondInstance =
new EppoClient.Builder().apiKey(DUMMY_FLAG_API_KEY).forceReinitialize(true).buildAndInit();
assertNotSame(firstInstance, secondInstance);
}
@Test
public void testPolling() {
EppoHttpClient httpClient = new EppoHttpClient(TEST_HOST, DUMMY_FLAG_API_KEY, "java", "3.0.0");
EppoHttpClient httpClientSpy = spy(httpClient);
TestUtils.setBaseClientHttpClientOverrideField(httpClientSpy);
new EppoClient.Builder()
.apiKey(DUMMY_FLAG_API_KEY)
.pollingIntervalMs(20)
.forceReinitialize(true)
.buildAndInit();
// Method will be called immediately on init
verify(httpClientSpy, times(1)).get(anyString());
// Sleep for 25 ms to allow another polling cycle to complete
sleepUninterruptedly(25);
// Now, the method should have been called twice
verify(httpClientSpy, times(2)).get(anyString());
EppoClient.stopPolling();
sleepUninterruptedly(25);
// No more calls since stopped
verify(httpClientSpy, times(2)).get(anyString());
}
// NOTE: Graceful mode during init is intrinsically true since the call is non-blocking and
// exceptions are caught without rethrowing in `FetchConfigurationsTask`
@Test
public void testClientMakesDefaultAssignmentsAfterFailingToInitialize() {
// Set up bad HTTP response
mockHttpError();
// Initialize and no exception should be thrown.
try {
EppoClient eppoClient = initFailingGracefulClient(true);
Thread.sleep(25); // Sleep to allow the async config fetch call to happen (and fail)
assertEquals("default", eppoClient.getStringAssignment("experiment1", "subject1", "default"));
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
}
public static void mockHttpError() {
// Create a mock instance of EppoHttpClient
EppoHttpClient mockHttpClient = mock(EppoHttpClient.class);
// Mock sync get
when(mockHttpClient.get(anyString())).thenThrow(new RuntimeException("Intentional Error"));
// Mock async get
CompletableFuture<byte[]> mockAsyncResponse = new CompletableFuture<>();
when(mockHttpClient.getAsync(anyString())).thenReturn(mockAsyncResponse);
mockAsyncResponse.completeExceptionally(new RuntimeException("Intentional Error"));
setBaseClientHttpClientOverrideField(mockHttpClient);
}
@SuppressWarnings("SameParameterValue")
private void sleepUninterruptedly(long sleepMs) {
try {
Thread.sleep(sleepMs);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private EppoClient initClient(String apiKey) {
mockAssignmentLogger = mock(AssignmentLogger.class);
mockBanditLogger = mock(BanditLogger.class);
return new EppoClient.Builder()
.apiKey(apiKey)
.apiBaseUrl(Constants.appendApiPathToHost(TEST_HOST))
.assignmentLogger(mockAssignmentLogger)
.banditLogger(mockBanditLogger)
.isGracefulMode(false)
.forceReinitialize(true) // Useful for tests
.buildAndInit();
}
private EppoClient initFailingGracefulClient(boolean isGracefulMode) {
mockAssignmentLogger = mock(AssignmentLogger.class);
mockBanditLogger = mock(BanditLogger.class);
return new EppoClient.Builder()
.apiKey(DUMMY_FLAG_API_KEY)
.apiBaseUrl("blag")
.assignmentLogger(mockAssignmentLogger)
.banditLogger(mockBanditLogger)
.isGracefulMode(isGracefulMode)
.forceReinitialize(true) // Useful for tests
.buildAndInit();
}
private void uninitClient() {
try {
Field httpClientOverrideField = EppoClient.class.getDeclaredField("instance");
httpClientOverrideField.setAccessible(true);
httpClientOverrideField.set(null, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private void initBuggyClient() {
try {
EppoClient eppoClient = initClient(DUMMY_FLAG_API_KEY);
Field configurationStoreField = BaseEppoClient.class.getDeclaredField("configurationStore");
configurationStoreField.setAccessible(true);
configurationStoreField.set(eppoClient, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static void setBaseClientHttpClientOverrideField(EppoHttpClient httpClient) {
// Uses reflection to set a static override field used for tests (e.g., httpClientOverride)
try {
Field httpClientOverrideField = BaseEppoClient.class.getDeclaredField("httpClientOverride");
httpClientOverrideField.setAccessible(true);
httpClientOverrideField.set(null, httpClient);
httpClientOverrideField.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}