Search in sources :

Example 11 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileLoaderTest method debugLoggedMultiThreaded.

@Test
public void debugLoggedMultiThreaded() throws IOException {
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    Cache cache = mock(Cache.class);
    datafileCache = new DatafileCache("debugLoggedMultiThreaded", 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(true);
    when(cache.exists(datafileCache.getFileName())).thenReturn(true);
    when(cache.load(datafileCache.getFileName())).thenReturn("{}");
    when(cache.save(datafileCache.getFileName(), "{}")).thenReturn(true);
    datafileLoader.getDatafile("debugLoggedMultiThreaded", datafileLoadedListener);
    Runnable r = () -> datafileLoader.getDatafile("debugLoggedMultiThreaded", datafileLoadedListener);
    new Thread(r).start();
    new Thread(r).start();
    new Thread(r).start();
    new Thread(r).start();
    try {
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail();
    }
    verify(datafileLoadedListener, atMost(5)).onDatafileLoaded("{}");
    verify(datafileLoadedListener, atLeast(1)).onDatafileLoaded("{}");
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Cache(com.optimizely.ab.android.shared.Cache) Test(org.junit.Test)

Example 12 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileLoaderTest method allowDoubleDownload.

@Test
public void allowDoubleDownload() throws IOException {
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    Cache cache = mock(Cache.class);
    datafileCache = new DatafileCache("allowDoubleDownload", cache, logger);
    DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, logger);
    // set download time to 1 second
    setTestDownloadFrequency(datafileLoader, 1000L);
    when(client.execute(any(Client.Request.class), anyInt(), anyInt())).thenReturn("{}");
    datafileLoader.getDatafile("allowDoubleDownload", datafileLoadedListener);
    try {
        executor.awaitTermination(2, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail();
    }
    datafileLoader.getDatafile("allowDoubleDownload", datafileLoadedListener);
    // reset back to normal.
    setTestDownloadFrequency(datafileLoader, 60 * 1000L);
    verify(logger, never()).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)

Example 13 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileLoaderTest method downloadAllowedNoCache.

@Test
public void downloadAllowedNoCache() throws IOException {
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    Cache cache = mock(Cache.class);
    datafileCache = new DatafileCache("downloadAllowedNoCache", 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(false);
    when(cache.exists(datafileCache.getFileName())).thenReturn(false);
    when(cache.load(datafileCache.getFileName())).thenReturn("{}");
    datafileLoader.getDatafile("downloadAllowedNoCache", datafileLoadedListener);
    datafileLoader.getDatafile("downloadAllowedNoCache", datafileLoadedListener);
    try {
        executor.awaitTermination(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail();
    }
    verify(logger, never()).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)

Example 14 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileLoaderTest method setup.

@Before
public void setup() {
    logger = mock(Logger.class);
    final Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    datafileCache = new DatafileCache("1", new Cache(targetContext, logger), logger);
    client = mock(Client.class);
    datafileClient = new DatafileClient(client, logger);
    datafileLoadedListener = mock(DatafileLoadedListener.class);
}
Also used : Context(android.content.Context) Logger(org.slf4j.Logger) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache) Before(org.junit.Before)

Example 15 with Cache

use of com.optimizely.ab.android.shared.Cache in project android-sdk by optimizely.

the class DatafileReschedulerTest method setup.

@Before
public void setup() {
    context = mock(Context.class);
    logger = mock(Logger.class);
    cache = new Cache(InstrumentationRegistry.getInstrumentation().getTargetContext(), logger);
    cache.delete(BackgroundWatchersCache.BACKGROUND_WATCHERS_FILE_NAME);
    backgroundWatchersCache = new BackgroundWatchersCache(cache, logger);
    captor = ArgumentCaptor.forClass(DatafileConfig.class);
    dispatcher = spy(new DatafileRescheduler.Dispatcher(context, backgroundWatchersCache, logger));
    doNothing().when(dispatcher).rescheduleService(any());
    datafileRescheduler = new DatafileRescheduler();
    datafileRescheduler.logger = logger;
}
Also used : Context(android.content.Context) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Logger(org.slf4j.Logger) Cache(com.optimizely.ab.android.shared.Cache) Before(org.junit.Before)

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