Skip to content

Commit

Permalink
notify CacheListener after cache is completed with original file
Browse files Browse the repository at this point in the history
  • Loading branch information
danikula committed Aug 26, 2016
1 parent 71c6301 commit 0f522f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/src/main/java/com/danikula/videocache/ProxyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private void readSource() {
notifyNewCacheDataAvailable(offset, sourceAvailable);
}
tryComplete();
onSourceRead();
} catch (Throwable e) {
readSourceErrorsCount.incrementAndGet();
onError(e);
Expand All @@ -143,6 +144,12 @@ private void readSource() {
}
}

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()) {
Expand Down
13 changes: 13 additions & 0 deletions test/src/test/java/com/danikula/videocache/HttpProxyCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ public void testLoadEmptyFile() throws Exception {
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();
Expand Down

3 comments on commit 0f522f2

@Antoha895
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@

@Antoha895
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

,

@Antoha895
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.