Skip to content

Commit 0f522f2

Browse files
committed
notify CacheListener after cache is completed with original file
1 parent 71c6301 commit 0f522f2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

library/src/main/java/com/danikula/videocache/ProxyCache.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private void readSource() {
134134
notifyNewCacheDataAvailable(offset, sourceAvailable);
135135
}
136136
tryComplete();
137+
onSourceRead();
137138
} catch (Throwable e) {
138139
readSourceErrorsCount.incrementAndGet();
139140
onError(e);
@@ -143,6 +144,12 @@ private void readSource() {
143144
}
144145
}
145146

147+
private void onSourceRead() {
148+
// guaranteed notify listeners after source read and cache completed
149+
percentsAvailable = 100;
150+
onCachePercentsAvailableChanged(percentsAvailable);
151+
}
152+
146153
private void tryComplete() throws ProxyCacheException {
147154
synchronized (stopLock) {
148155
if (!isStopped() && cache.available() == source.length()) {

test/src/test/java/com/danikula/videocache/HttpProxyCacheTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ public void testLoadEmptyFile() throws Exception {
166166
assertThat(response.data).isEmpty();
167167
}
168168

169+
@Test
170+
public void testCacheListenerCalledAtTheEnd() throws Exception {
171+
File file = ProxyCacheTestUtils.newCacheFile();
172+
File tempFile = ProxyCacheTestUtils.getTempFile(file);
173+
HttpProxyCache proxyCache = new HttpProxyCache(new HttpUrlSource(HTTP_DATA_URL), new FileCache(file));
174+
CacheListener listener = Mockito.mock(CacheListener.class);
175+
proxyCache.registerCacheListener(listener);
176+
processRequest(proxyCache, "GET /" + HTTP_DATA_URL + " HTTP/1.1");
177+
178+
Mockito.verify(listener).onCacheAvailable(tempFile, HTTP_DATA_URL, 100); // must be called for temp file ...
179+
Mockito.verify(listener).onCacheAvailable(file, HTTP_DATA_URL, 100); // .. and for original file too
180+
}
181+
169182
@Test(expected = ProxyCacheException.class)
170183
public void testTouchSourceForAbsentSourceInfoAndCache() throws Exception {
171184
SourceInfoStorage sourceInfoStorage = SourceInfoStorageFactory.newEmptySourceInfoStorage();

0 commit comments

Comments
 (0)