use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.
the class UserProfileCacheTest method setup.
@Before
public void setup() throws JSONException {
logger = mock(Logger.class);
projectId = "1";
cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger);
diskCache = new UserProfileCache.DiskCache(cache, executor, logger, projectId);
legacyDiskCache = new UserProfileCache.LegacyDiskCache(cache, executor, logger, projectId);
memoryCache = new ConcurrentHashMap<>();
userProfileCache = new UserProfileCache(diskCache, logger, memoryCache, legacyDiskCache);
// Test data.
userId1 = "user_1";
Map<String, Map<String, String>> experimentBucketMap1 = new ConcurrentHashMap<>();
Map<String, String> decisionMap1 = new ConcurrentHashMap<>();
decisionMap1.put("variation_id", "var_1");
experimentBucketMap1.put("exp_1", decisionMap1);
Map<String, String> decisionMap2 = new ConcurrentHashMap<>();
decisionMap2.put("variation_id", "var_2");
experimentBucketMap1.put("exp_2", decisionMap2);
userProfileMap1 = new ConcurrentHashMap<>();
userProfileMap1.put("user_id", userId1);
userProfileMap1.put("experiment_bucket_map", experimentBucketMap1);
userId2 = "user_2";
Map<String, Map<String, String>> experimentBucketMap2 = new ConcurrentHashMap<>();
Map<String, String> decisionMap3 = new ConcurrentHashMap<>();
decisionMap3.put("variation_id", "var_3");
experimentBucketMap2.put("exp_1", decisionMap3);
Map<String, String> decisionMap4 = new ConcurrentHashMap<>();
decisionMap4.put("variation_id", "var_4");
experimentBucketMap2.put("exp_2", decisionMap4);
userProfileMap2 = new ConcurrentHashMap<>();
userProfileMap2.put("user_id", userId2);
userProfileMap2.put("experiment_bucket_map", experimentBucketMap2);
}
use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.
the class BackgroundWatchersCacheTest method testLoadFileNotFound.
@Test
public void testLoadFileNotFound() {
Cache cache = mock(Cache.class);
BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
// Cause a JSONException to be thrown
when(cache.load(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME)).thenReturn(null);
assertFalse(backgroundWatchersCache.setIsWatching(new DatafileConfig("1", null), true));
verify(logger).info("Creating background watchers file {}.", BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME);
}
use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.
the class DatafileCacheTest method loadJsonException.
@Test
public void loadJsonException() throws IOException {
Cache cache = mock(Cache.class);
DatafileCache datafileCache = new DatafileCache("1", cache, logger);
when(cache.load(datafileCache.getFileName())).thenReturn("{");
assertNull(datafileCache.load());
verify(logger).error(contains("Unable to parse data file"), any(JSONException.class));
}
use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.
the class DatafileLoaderTest method warningsAreLogged.
@Test
// flacky with lower API
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void warningsAreLogged() throws IOException {
final ExecutorService executor = Executors.newSingleThreadExecutor();
Cache cache = mock(Cache.class);
datafileCache = new DatafileCache("warningsAreLogged", cache, logger);
DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, logger);
when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}");
when(cache.exists(datafileCache.getFileName())).thenReturn(true);
when(cache.delete(datafileCache.getFileName())).thenReturn(false);
when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(false);
datafileLoader.getDatafile("warningsAreLogged", datafileLoadedListener);
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail();
}
verify(logger).warn("Unable to delete old datafile");
verify(logger).warn("Unable to save new datafile");
verify(datafileLoadedListener, atMost(1)).onDatafileLoaded("{}");
}
use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.
the class DatafileLoaderTest method debugLogged.
@Test
// flacky with lower API
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void debugLogged() throws IOException {
final ExecutorService executor = Executors.newSingleThreadExecutor();
Cache cache = mock(Cache.class);
datafileCache = new DatafileCache("debugLogged", cache, logger);
DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, logger);
when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}");
when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(true);
when(cache.exists(datafileCache.getFileName())).thenReturn(true);
when(cache.load(datafileCache.getFileName())).thenReturn("{}");
datafileLoader.getDatafile("debugLogged", datafileLoadedListener);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
datafileLoader.getDatafile("debugLogged", datafileLoadedListener);
try {
executor.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail();
}
verify(logger).debug("Last download happened under 1 minute ago. Throttled to be at least 1 minute apart.");
verify(datafileLoadedListener, atMost(2)).onDatafileLoaded("{}");
verify(datafileLoadedListener, atLeast(1)).onDatafileLoaded("{}");
}
Aggregations