예제 #1
0
 public static void searchTweet(Context context, String pUrl, NetworkCallback callback) {
   StringBuilder url = new StringBuilder(pUrl);
   url.append("&count=" + DEFAULT_PAGE_SIZE);
   Request<String> volleyTypeRequest = null;
   volleyTypeRequest =
       bundleToVolleyRequestWithSoftTtl(
           context, Request.Method.GET, null, url.toString(), callback);
   volleyTypeRequest.setTag(callback);
   volleyTypeRequest.setShouldCache(true);
   dispatchToQueue(volleyTypeRequest, context);
 }
예제 #2
0
 public static <T> void addRequest(RequestQueue requestQueue, Request<T> request, Object tag) {
   if (tag != null) {
     request.setTag(tag);
   }
   request.setShouldCache(false);
   request.setRetryPolicy(
       new DefaultRetryPolicy(
           TIME_OUT,
           DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
           DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
   requestQueue.add(request);
 }
  @Test
  public void testRefresh() throws VolleyError {
    when(cacheEntry.isExpired()).thenReturn(false);
    when(cacheEntry.refreshNeeded()).thenReturn(true);

    // mock non expired entry for return
    Cache.Entry networkEntry = mock(Cache.Entry.class);
    when(networkEntry.isExpired()).thenReturn(false);

    Response response = Response.success(null, networkEntry);
    when(request.parseNetworkResponse(any(NetworkResponse.class))).thenReturn(response);
    request.setShouldCache(true);

    dispatcher.processRequest(request);
    verify(cache).put("test-cache-key", networkEntry);
    verify(mockNetwork).performRequest(request);
  }
  @Test
  public void testCachePut() throws VolleyError {
    // Given
    when(cacheEntry.isExpired()).thenReturn(true);

    // mock non expired entry for return
    Cache.Entry networkEntry = new Cache.Entry();
    networkEntry.ttl = System.currentTimeMillis() + 100000;

    NetworkResponse networkResponse = mock(NetworkResponse.class);
    when(mockNetwork.performRequest(any(Request.class))).thenReturn(networkResponse);

    Response response = Response.success(new Object(), networkEntry);
    when(request.parseNetworkResponse(any(NetworkResponse.class))).thenReturn(response);
    request.setShouldCache(true);

    // When
    dispatcher.processRequest(request);

    // Then
    verify(cache).put("test-cache-key", networkEntry);
  }