• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.volley;
18 
19 import android.test.suitebuilder.annotation.MediumTest;
20 
21 import com.android.volley.mock.MockCache;
22 import com.android.volley.mock.MockRequest;
23 import com.android.volley.mock.MockResponseDelivery;
24 import com.android.volley.mock.WaitableQueue;
25 import com.android.volley.utils.CacheTestUtils;
26 
27 import junit.framework.TestCase;
28 
29 @MediumTest
30 @SuppressWarnings("rawtypes")
31 public class CacheDispatcherTest extends TestCase {
32     private CacheDispatcher mDispatcher;
33     private WaitableQueue mCacheQueue;
34     private WaitableQueue mNetworkQueue;
35     private MockCache mCache;
36     private MockResponseDelivery mDelivery;
37     private MockRequest mRequest;
38 
39     private static final long TIMEOUT_MILLIS = 5000;
40 
41     @Override
setUp()42     protected void setUp() throws Exception {
43         super.setUp();
44 
45         mCacheQueue = new WaitableQueue();
46         mNetworkQueue = new WaitableQueue();
47         mCache = new MockCache();
48         mDelivery = new MockResponseDelivery();
49 
50         mRequest = new MockRequest();
51 
52         mDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
53         mDispatcher.start();
54     }
55 
56     @Override
tearDown()57     protected void tearDown() throws Exception {
58         super.tearDown();
59         mDispatcher.quit();
60         mDispatcher.join();
61     }
62 
63     // A cancelled request should not be processed at all.
testCancelledRequest()64     public void testCancelledRequest() throws Exception {
65         mRequest.cancel();
66         mCacheQueue.add(mRequest);
67         mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
68         assertFalse(mCache.getCalled);
69         assertFalse(mDelivery.wasEitherResponseCalled());
70     }
71 
72     // A cache miss does not post a response and puts the request on the network queue.
testCacheMiss()73     public void testCacheMiss() throws Exception {
74         mCacheQueue.add(mRequest);
75         mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
76         assertFalse(mDelivery.wasEitherResponseCalled());
77         assertTrue(mNetworkQueue.size() > 0);
78         Request request = mNetworkQueue.take();
79         assertNull(request.getCacheEntry());
80     }
81 
82     // A non-expired cache hit posts a response and does not queue to the network.
testNonExpiredCacheHit()83     public void testNonExpiredCacheHit() throws Exception {
84         Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
85         mCache.setEntryToReturn(entry);
86         mCacheQueue.add(mRequest);
87         mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
88         assertTrue(mDelivery.postResponse_called);
89         assertFalse(mDelivery.postError_called);
90     }
91 
92     // A soft-expired cache hit posts a response and queues to the network.
testSoftExpiredCacheHit()93     public void testSoftExpiredCacheHit() throws Exception {
94         Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, true);
95         mCache.setEntryToReturn(entry);
96         mCacheQueue.add(mRequest);
97         mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
98         assertTrue(mDelivery.postResponse_called);
99         assertFalse(mDelivery.postError_called);
100         assertTrue(mNetworkQueue.size() > 0);
101         Request request = mNetworkQueue.take();
102         assertSame(entry, request.getCacheEntry());
103     }
104 
105     // An expired cache hit does not post a response and queues to the network.
testExpiredCacheHit()106     public void testExpiredCacheHit() throws Exception {
107         Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, true, true);
108         mCache.setEntryToReturn(entry);
109         mCacheQueue.add(mRequest);
110         mCacheQueue.waitUntilEmpty(TIMEOUT_MILLIS);
111         assertFalse(mDelivery.wasEitherResponseCalled());
112         assertTrue(mNetworkQueue.size() > 0);
113         Request request = mNetworkQueue.take();
114         assertSame(entry, request.getCacheEntry());
115     }
116 }
117