### Question:
Kelly { public T get(String key) throws KellyException { CacheEntry<T> cacheEntry = null; try { cacheEntry = cacheProvider.get(key); } catch (CacheProviderException e) { throw new KellyException(e); } if (cacheEntry != null){ if(cacheEntryExpired(cacheEntry)){ if(putRequestInFlight(cacheEntry)) try { reloadCacheEntry(cacheEntry); } catch (ExecutionException e) { throw new KellyException(e); } catch (InterruptedException e) { throw new KellyException(e); } } return cacheEntry.getValue(); } else { return null; } } @Deprecated  Kelly(CacheProvider<T> cacheProvider, CacheLoader<T> cacheLoader, int executorPoolSize);  Kelly(CacheProvider<T> cacheProvider, CacheLoader<T> cacheLoader, int threadPoolSize,
                 int queueSize); T get(String key); boolean put(String key, CacheEntry<T> value); void expire(String key);  }### Answer:
@Test public void noCacheEntryTest() throws CacheProviderException, KellyException { CacheProvider<String> cacheProvider = Mockito.mock(CacheProvider.class); CacheLoader<String> cacheLoader = Mockito.mock(CacheLoader.class); Mockito.when(cacheProvider.get("dude")).thenReturn(null); Kelly kelly = new Kelly(cacheProvider, cacheLoader, 1); assert kelly.get("dude") == null; }
@Test public void ttlZeroTest() throws CacheLoaderException, CacheProviderException, InterruptedException, KellyException { CacheProvider<String> cacheProvider = Mockito.mock(CacheProvider.class); CacheLoader<String> cacheLoader = Mockito.mock(CacheLoader.class); CacheEntry<String> cacheEntry = new CacheEntry<String>("dude", "suman karthik"); Kelly<String> kelly = new Kelly<String>(cacheProvider, cacheLoader, 1); Mockito.when(cacheProvider.get("dude")).thenReturn(cacheEntry); assert kelly.get("dude").equals("suman karthik"); Thread.sleep(1000); assert kelly.get("dude").equals("suman karthik"); Mockito.verify(cacheLoader, Mockito.never()).reload("dude","suman karthik"); }
@Test public void kellyGetTest() throws CacheProviderException, KellyException { CacheProvider<String> cacheProvider = Mockito.mock(CacheProvider.class); CacheLoader<String> cacheLoader = Mockito.mock(CacheLoader.class); CacheEntry<String> cacheEntry = new CacheEntry<String>("dude","suman",1); Mockito.when(cacheProvider.get("dude")).thenReturn(cacheEntry); Kelly<String> kelly = new Kelly<String>(cacheProvider,cacheLoader,1); assert kelly.get("dude").equals("suman"); }
@Test(singleThreaded = true) public void requestsInFlightTest() throws InterruptedException, CacheProviderException, KellyException { CacheProvider<String> cacheProvider = Mockito.mock(CacheProvider.class); CacheLoader<String> cacheLoader = new DummyCacheLoader(); CacheEntry<String> cacheEntry = new CacheEntry<String>("dude","suman",1); Kelly<String> kelly = new Kelly<String>(cacheProvider,cacheLoader,10); Thread.sleep(2000); Mockito.when(cacheProvider.get("dude")).thenReturn(cacheEntry); kelly.get("dude"); kelly.get("dude"); Thread.sleep(1000); assert DummyCacheLoader.invocations==1; }