Search in sources :

Example 1 with Cache

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);
}
Also used : Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) Cache(com.optimizely.ab.android.shared.Cache) Before(org.junit.Before)

Example 2 with Cache

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);
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 3 with Cache

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));
}
Also used : JSONException(org.json.JSONException) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 4 with Cache

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("{}");
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 5 with Cache

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("{}");
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test) SdkSuppress(androidx.test.filters.SdkSuppress)

Aggregations

Cache (com.optimizely.ab.android.shared.Cache)28 Test (org.junit.Test)10 Logger (org.slf4j.Logger)9 Before (org.junit.Before)8 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)6 Client (com.optimizely.ab.android.shared.Client)5 ExecutorService (java.util.concurrent.ExecutorService)5 Context (android.content.Context)3 OptlyStorage (com.optimizely.ab.android.shared.OptlyStorage)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 JSONObject (org.json.JSONObject)3 SdkSuppress (androidx.test.filters.SdkSuppress)2 JSONException (org.json.JSONException)2 Intent (android.content.Intent)1 FileObserver (android.os.FileObserver)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1 ListenableWorker (androidx.work.ListenableWorker)1 File (java.io.File)1