2
2
3
3
import static cloud .eppo .helpers .AssignmentTestCase .parseTestCaseFile ;
4
4
import static cloud .eppo .helpers .AssignmentTestCase .runTestCase ;
5
+ import static cloud .eppo .helpers .TestUtils .mockHttpError ;
5
6
import static cloud .eppo .helpers .TestUtils .mockHttpResponse ;
6
7
import static cloud .eppo .helpers .TestUtils .setBaseClientHttpClientOverrideField ;
8
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
7
9
import static org .junit .jupiter .api .Assertions .assertEquals ;
8
10
import static org .junit .jupiter .api .Assertions .assertFalse ;
9
11
import static org .junit .jupiter .api .Assertions .assertThrows ;
10
12
import static org .junit .jupiter .api .Assertions .assertTrue ;
11
13
import static org .mockito .Mockito .*;
12
14
13
- import cloud .eppo .api .Attributes ;
14
- import cloud .eppo .api .Configuration ;
15
- import cloud .eppo .api .EppoValue ;
16
- import cloud .eppo .api .IAssignmentCache ;
15
+ import cloud .eppo .api .*;
17
16
import cloud .eppo .cache .LRUInMemoryAssignmentCache ;
18
17
import cloud .eppo .helpers .AssignmentTestCase ;
19
18
import cloud .eppo .logging .Assignment ;
25
24
import java .io .IOException ;
26
25
import java .util .*;
27
26
import java .util .concurrent .CompletableFuture ;
27
+ import java .util .concurrent .CompletionException ;
28
28
import java .util .stream .Stream ;
29
29
import org .apache .commons .io .FileUtils ;
30
30
import org .junit .jupiter .api .BeforeEach ;
@@ -66,7 +66,7 @@ private void initClientWithData(
66
66
new BaseEppoClient (
67
67
DUMMY_FLAG_API_KEY ,
68
68
isConfigObfuscated ? "android" : "java" ,
69
- "3.0 .0" ,
69
+ "100.1 .0" ,
70
70
TEST_HOST ,
71
71
mockAssignmentLogger ,
72
72
null ,
@@ -86,7 +86,7 @@ private void initClient(boolean isGracefulMode, boolean isConfigObfuscated) {
86
86
new BaseEppoClient (
87
87
DUMMY_FLAG_API_KEY ,
88
88
isConfigObfuscated ? "android" : "java" ,
89
- "3.0 .0" ,
89
+ "100.1 .0" ,
90
90
TEST_HOST ,
91
91
mockAssignmentLogger ,
92
92
null ,
@@ -102,14 +102,37 @@ private void initClient(boolean isGracefulMode, boolean isConfigObfuscated) {
102
102
log .info ("Test client initialized" );
103
103
}
104
104
105
+ private CompletableFuture <Void > initClientAsync (
106
+ boolean isGracefulMode , boolean isConfigObfuscated ) {
107
+ mockAssignmentLogger = mock (AssignmentLogger .class );
108
+
109
+ eppoClient =
110
+ new BaseEppoClient (
111
+ DUMMY_FLAG_API_KEY ,
112
+ isConfigObfuscated ? "android" : "java" ,
113
+ "100.1.0" ,
114
+ TEST_HOST ,
115
+ mockAssignmentLogger ,
116
+ null ,
117
+ null ,
118
+ isGracefulMode ,
119
+ isConfigObfuscated ,
120
+ true ,
121
+ null ,
122
+ null ,
123
+ null );
124
+
125
+ return eppoClient .loadConfigurationAsync ();
126
+ }
127
+
105
128
private void initClientWithAssignmentCache (IAssignmentCache cache ) {
106
129
mockAssignmentLogger = mock (AssignmentLogger .class );
107
130
108
131
eppoClient =
109
132
new BaseEppoClient (
110
133
DUMMY_FLAG_API_KEY ,
111
134
"java" ,
112
- "3.0 .0" ,
135
+ "100.1 .0" ,
113
136
TEST_HOST ,
114
137
mockAssignmentLogger ,
115
138
null ,
@@ -272,6 +295,78 @@ private CompletableFuture<Configuration> immediateConfigFuture(
272
295
Configuration .builder (config .getBytes (), isObfuscated ).build ());
273
296
}
274
297
298
+ @ Test
299
+ public void testGracefulInitializationFailure () {
300
+ // Set up bad HTTP response
301
+ mockHttpError ();
302
+
303
+ // Initialize and no exception should be thrown.
304
+ assertDoesNotThrow (() -> initClient (true , false ));
305
+ }
306
+
307
+ @ Test
308
+ public void testClientMakesDefaultAssignmentsAfterFailingToInitialize () {
309
+ // Set up bad HTTP response
310
+ mockHttpError ();
311
+
312
+ // Initialize and no exception should be thrown.
313
+ assertDoesNotThrow (() -> initClient (true , false ));
314
+
315
+ assertEquals ("default" , eppoClient .getStringAssignment ("experiment1" , "subject1" , "default" ));
316
+ }
317
+
318
+ @ Test
319
+ public void testClientMakesDefaultAssignmentsAfterFailingToInitializeNonGracefulMode () {
320
+ // Set up bad HTTP response
321
+ mockHttpError ();
322
+
323
+ // Initialize and no exception should be thrown.
324
+ try {
325
+ initClient (false , false );
326
+ } catch (RuntimeException e ) {
327
+ // Expected
328
+ assertEquals ("Intentional Error" , e .getMessage ());
329
+ } finally {
330
+ assertEquals ("default" , eppoClient .getStringAssignment ("experiment1" , "subject1" , "default" ));
331
+ }
332
+ }
333
+
334
+ @ Test
335
+ public void testNonGracefulInitializationFailure () {
336
+ // Set up bad HTTP response
337
+ mockHttpError ();
338
+
339
+ // Initialize and assert exception thrown
340
+ assertThrows (Exception .class , () -> initClient (false , false ));
341
+ }
342
+
343
+ @ Test
344
+ public void testGracefulAsyncInitializationFailure () {
345
+ // Set up bad HTTP response
346
+ mockHttpError ();
347
+
348
+ // Initialize
349
+ CompletableFuture <Void > init = initClientAsync (true , false );
350
+
351
+ // Wait for initialization; future should not complete exceptionally (equivalent of exception
352
+ // being thrown).
353
+ init .join ();
354
+ assertFalse (init .isCompletedExceptionally ());
355
+ }
356
+
357
+ @ Test
358
+ public void testNonGracefulAsyncInitializationFailure () {
359
+ // Set up bad HTTP response
360
+ mockHttpError ();
361
+
362
+ // Initialize
363
+ CompletableFuture <Void > init = initClientAsync (false , false );
364
+
365
+ // Exceptions thrown in CompletableFutures are wrapped in a CompletionException.
366
+ assertThrows (CompletionException .class , init ::join );
367
+ assertTrue (init .isCompletedExceptionally ());
368
+ }
369
+
275
370
@ Test
276
371
public void testWithInitialConfiguration () {
277
372
try {
@@ -341,7 +436,7 @@ public void testAssignmentEventCorrectlyCreated() {
341
436
Map <String , String > expectedMeta = new HashMap <>();
342
437
expectedMeta .put ("obfuscated" , "false" );
343
438
expectedMeta .put ("sdkLanguage" , "java" );
344
- expectedMeta .put ("sdkLibVersion" , "3.0 .0" );
439
+ expectedMeta .put ("sdkLibVersion" , "100.1 .0" );
345
440
346
441
assertEquals (expectedMeta , capturedAssignment .getMetaData ());
347
442
}
0 commit comments