notify CacheListener after cache is completed with original file

This commit is contained in:
Alexey Danilov
2016-08-26 13:10:19 +03:00
parent 71c6301eb4
commit 0f522f200d
2 changed files with 20 additions and 0 deletions

View File

@@ -134,6 +134,7 @@ class ProxyCache {
notifyNewCacheDataAvailable(offset, sourceAvailable);
}
tryComplete();
onSourceRead();
} catch (Throwable e) {
readSourceErrorsCount.incrementAndGet();
onError(e);
@@ -143,6 +144,12 @@ class ProxyCache {
}
}
private void onSourceRead() {
// guaranteed notify listeners after source read and cache completed
percentsAvailable = 100;
onCachePercentsAvailableChanged(percentsAvailable);
}
private void tryComplete() throws ProxyCacheException {
synchronized (stopLock) {
if (!isStopped() && cache.available() == source.length()) {

View File

@@ -166,6 +166,19 @@ public class HttpProxyCacheTest {
assertThat(response.data).isEmpty();
}
@Test
public void testCacheListenerCalledAtTheEnd() throws Exception {
File file = ProxyCacheTestUtils.newCacheFile();
File tempFile = ProxyCacheTestUtils.getTempFile(file);
HttpProxyCache proxyCache = new HttpProxyCache(new HttpUrlSource(HTTP_DATA_URL), new FileCache(file));
CacheListener listener = Mockito.mock(CacheListener.class);
proxyCache.registerCacheListener(listener);
processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
Mockito.verify(listener).onCacheAvailable(tempFile, HTTP_DATA_URL, 100); // must be called for temp file ...
Mockito.verify(listener).onCacheAvailable(file, HTTP_DATA_URL, 100); // .. and for original file too
}
@Test(expected = ProxyCacheException.class)
public void testTouchSourceForAbsentSourceInfoAndCache() throws Exception {
SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newEmptySourceInfoStorage();