Skip to content

Commit 5012c00

Browse files
committed
Fix crash bug when cancelling requests being cached.
koush/ion#252 (comment)
1 parent bc94814 commit 5012c00

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

AndroidAsync/src/com/koushikdutta/async/FilteredDataEmitter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public void setDataTracker(DataTracker tracker) {
4545
int totalRead;
4646
@Override
4747
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
48+
if (closed) {
49+
// this emitter was closed but for some reason data is still being spewed...
50+
// eat it, nom nom.
51+
bb.recycle();
52+
return;
53+
}
4854
if (bb != null)
4955
totalRead += bb.remaining();
5056
Util.emitAllData(this, bb);
@@ -81,9 +87,12 @@ public AsyncServer getServer() {
8187
return mEmitter.getServer();
8288
}
8389

90+
boolean closed;
8491
@Override
8592
public void close() {
86-
mEmitter.close();
93+
closed = true;
94+
if (mEmitter != null)
95+
mEmitter.close();
8796
}
8897

8998
@Override

AndroidAsync/src/com/koushikdutta/async/http/ResponseCacheMiddleware.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ protected void report(Exception e) {
325325
@Override
326326
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
327327
if (cached != null) {
328-
com.koushikdutta.async.Util.emitAllData(this, cached);
328+
super.onDataAvailable(emitter, cached);
329329
// couldn't emit it all, so just wait for another day...
330330
if (cached.remaining() > 0)
331331
return;
@@ -369,6 +369,12 @@ public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
369369
}
370370
}
371371

372+
@Override
373+
public void close() {
374+
abort();
375+
super.close();
376+
}
377+
372378
public void abort() {
373379
if (editor != null) {
374380
editor.abort();

AndroidAsync/test/src/com/koushikdutta/async/test/CacheTests.java

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
import com.koushikdutta.async.AsyncServer;
66
import com.koushikdutta.async.AsyncServerSocket;
7+
import com.koushikdutta.async.ByteBufferList;
8+
import com.koushikdutta.async.DataEmitter;
9+
import com.koushikdutta.async.FilteredDataEmitter;
10+
import com.koushikdutta.async.Util;
11+
import com.koushikdutta.async.callback.DataCallback;
712
import com.koushikdutta.async.http.AsyncHttpClient;
813
import com.koushikdutta.async.http.AsyncHttpGet;
914
import com.koushikdutta.async.http.ResponseCacheMiddleware;
@@ -16,7 +21,10 @@
1621
import junit.framework.TestCase;
1722

1823
import java.io.File;
24+
import java.nio.ByteBuffer;
1925
import java.util.Date;
26+
import java.util.concurrent.Semaphore;
27+
import java.util.concurrent.TimeUnit;
2028

2129
/**
2230
* Created by koush on 6/13/13.
@@ -55,29 +63,37 @@ public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse re
5563
}
5664
}
5765

58-
// static public boolean deleteDirectory(File path) {
59-
// if (path.exists()) {
60-
// File[] files = path.listFiles();
61-
// if (files != null) {
62-
// for (int i = 0; i < files.length; i++) {
63-
// if (files[i].isDirectory()) {
64-
// deleteDirectory(files[i]);
65-
// } else {
66-
// files[i].delete();
67-
// }
68-
// }
69-
// }
70-
// }
71-
// return (path.delete());
72-
// }
73-
74-
// public void testDiskLruCache() throws Exception {
75-
// File dir = new File(Environment.getExternalStorageDirectory(), "AndroidAsyncTest/cache-test");
76-
// deleteDirectory(dir);
77-
// DiskLruCache cache = DiskLruCache.open(dir, 0, 1000, 10000000);
78-
// DiskLruCache.Editor editor = cache.edit("stuff");
79-
//
80-
// DiskLruCache cache2 = DiskLruCache.open(dir, 0, 2, 10000000);
81-
// DiskLruCache.Snapshot snapshot = cache2.get("stuff");
82-
// }
66+
private static final long TIMEOUT = 1000L;
67+
public void testFilteredDataEmitter() throws Exception {
68+
final Semaphore semaphore = new Semaphore(0);
69+
70+
FilteredDataEmitter f = new FilteredDataEmitter() {
71+
@Override
72+
public boolean isPaused() {
73+
return false;
74+
}
75+
};
76+
77+
f.setDataCallback(new DataCallback() {
78+
@Override
79+
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
80+
assertEquals(bb.readString(), "hello");
81+
bb.recycle();
82+
semaphore.release();
83+
}
84+
});
85+
86+
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
87+
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
88+
89+
f.setDataCallback(new DataCallback() {
90+
@Override
91+
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
92+
fail();
93+
}
94+
});
95+
f.close();
96+
97+
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
98+
}
8399
}

0 commit comments

Comments
 (0)