|
4 | 4 |
|
5 | 5 | import com.koushikdutta.async.AsyncServer;
|
6 | 6 | 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; |
7 | 12 | import com.koushikdutta.async.http.AsyncHttpClient;
|
8 | 13 | import com.koushikdutta.async.http.AsyncHttpGet;
|
9 | 14 | import com.koushikdutta.async.http.ResponseCacheMiddleware;
|
|
16 | 21 | import junit.framework.TestCase;
|
17 | 22 |
|
18 | 23 | import java.io.File;
|
| 24 | +import java.nio.ByteBuffer; |
19 | 25 | import java.util.Date;
|
| 26 | +import java.util.concurrent.Semaphore; |
| 27 | +import java.util.concurrent.TimeUnit; |
20 | 28 |
|
21 | 29 | /**
|
22 | 30 | * Created by koush on 6/13/13.
|
@@ -55,29 +63,37 @@ public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse re
|
55 | 63 | }
|
56 | 64 | }
|
57 | 65 |
|
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 | + } |
83 | 99 | }
|
0 commit comments