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("{}");
}
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("{}");
}
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("{}");
}
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);
}
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;
}
Aggregations