File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
library/src/main/java/com/danikula/videocache
test/src/test/java/com/danikula/videocache Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff 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 ()) {
Original file line number Diff line number Diff 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 ();
You can’t perform that action at this time.
0 commit comments