com.android.volley.Request.Priority Java Examples

The following examples show how to use com.android.volley.Request.Priority. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void compareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #2
Source File: RequestTest.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
@Test public void compareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #3
Source File: RequestTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void compareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #4
Source File: RequestTest.java    From device-database with Apache License 2.0 6 votes vote down vote up
@Test public void compareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #5
Source File: RequestTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void compareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #6
Source File: RequestTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Test public void compareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #7
Source File: RequestTest.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
public void testCompareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #8
Source File: RequestQueueTest.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) {
    Priority thisPriority = request.getPriority();
    int thisSequence = request.getSequence();

    int priorityDiff = thisPriority.compareTo(mLastPriority);

    // Should never experience a higher priority after a lower priority
    assertFalse(priorityDiff > 0);

    // If we're not transitioning to a new priority block, check sequence numbers
    if (priorityDiff == 0) {
        assertTrue(thisSequence > mLastSequence);
    }
    mLastSequence = thisSequence;
    mLastPriority = thisPriority;

    mSemaphore.release();
    return new NetworkResponse(new byte[16]);
}
 
Example #9
Source File: RequestQueueTest.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) {
    Priority thisPriority = request.getPriority();
    int thisSequence = request.getSequence();

    int priorityDiff = thisPriority.compareTo(mLastPriority);

    // Should never experience a higher priority after a lower priority
    assertFalse(priorityDiff > 0);

    // If we're not transitioning to a new priority block, check sequence numbers
    if (priorityDiff == 0) {
        assertTrue(thisSequence > mLastSequence);
    }
    mLastSequence = thisSequence;
    mLastPriority = thisPriority;

    mSemaphore.release();
    return new NetworkResponse(new byte[16]);
}
 
Example #10
Source File: RequestTest.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
public void testCompareTo() {
    int sequence = 0;
    TestRequest low = new TestRequest(Priority.LOW);
    low.setSequence(sequence++);
    TestRequest low2 = new TestRequest(Priority.LOW);
    low2.setSequence(sequence++);
    TestRequest high = new TestRequest(Priority.HIGH);
    high.setSequence(sequence++);
    TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
    immediate.setSequence(sequence++);

    // "Low" should sort higher because it's really processing order.
    assertTrue(low.compareTo(high) > 0);
    assertTrue(high.compareTo(low) < 0);
    assertTrue(low.compareTo(low2) < 0);
    assertTrue(low.compareTo(immediate) > 0);
    assertTrue(immediate.compareTo(high) < 0);
}
 
Example #11
Source File: RequestQueueTest.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
/**
 * Make a list of requests with random priorities.
 *
 * @param count Number of requests to make
 */
private List<MockRequest> makeRequests(int count) {
    Request.Priority[] allPriorities = Request.Priority.values();
    Random random = new Random();

    List<MockRequest> requests = new ArrayList<MockRequest>();
    for (int i = 0; i < count; i++) {
        MockRequest request = new MockRequest();
        Request.Priority priority = allPriorities[random.nextInt(allPriorities.length)];
        request.setCacheKey(String.valueOf(i));
        request.setPriority(priority);
        requests.add(request);
    }
    return requests;
}
 
Example #12
Source File: RequestQueueIntegrationTest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
 
Example #13
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
 
Example #14
Source File: RequestQueueIntegrationTest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
 
Example #15
Source File: RequestQueueIntegrationTest.java    From volley with Apache License 2.0 5 votes vote down vote up
@Test
public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second,
    // higher priority request takes 20ms.
    // Assert that the first request is only handled after the first one has been parsed and
    // delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    Answer<NetworkResponse> delayAnswer =
            new Answer<NetworkResponse>() {
                @Override
                public NetworkResponse answer(InvocationOnMock invocationOnMock)
                        throws Throwable {
                    Thread.sleep(20);
                    return mock(NetworkResponse.class);
                }
            };
    // delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(mMockListener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    InOrder inOrder = inOrder(mMockListener);
    // verify higherPriorityReq goes through first
    inOrder.verify(mMockListener, timeout(10000)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    inOrder.verify(mMockListener, timeout(10000)).onRequestFinished(lowerPriorityReq);

    queue.stop();
}
 
Example #16
Source File: RequestQueueTest.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Make a list of requests with random priorities.
 * @param count Number of requests to make
 */
private List<MockRequest> makeRequests(int count) {
    Request.Priority[] allPriorities = Request.Priority.values();
    Random random = new Random();

    List<MockRequest> requests = new ArrayList<MockRequest>();
    for (int i = 0; i < count; i++) {
        MockRequest request = new MockRequest();
        Request.Priority priority = allPriorities[random.nextInt(allPriorities.length)];
        request.setCacheKey(String.valueOf(i));
        request.setPriority(priority);
        requests.add(request);
    }
    return requests;
}
 
Example #17
Source File: RequestQueueIntegrationTest.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override public NetworkResponse answer(InvocationOnMock invocationOnMock)
                throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
 
Example #18
Source File: RequestQueueIntegrationTest.java    From device-database with Apache License 2.0 5 votes vote down vote up
@Test public void add_requestProcessedInCorrectOrder() throws Exception {
    // Enqueue 2 requests with different cache keys, and different priorities. The second, higher priority request
    // takes 20ms.
    // Assert that first request is only handled after the first one has been parsed and delivered.
    MockRequest lowerPriorityReq = new MockRequest();
    MockRequest higherPriorityReq = new MockRequest();
    lowerPriorityReq.setCacheKey("1");
    higherPriorityReq.setCacheKey("2");
    lowerPriorityReq.setPriority(Priority.LOW);
    higherPriorityReq.setPriority(Priority.HIGH);

    RequestFinishedListener listener = mock(RequestFinishedListener.class);
    Answer<NetworkResponse> delayAnswer = new Answer<NetworkResponse>() {
        @Override
        public NetworkResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(20);
            return mock(NetworkResponse.class);
        }
    };
    //delay only for higher request
    when(mMockNetwork.performRequest(higherPriorityReq)).thenAnswer(delayAnswer);
    when(mMockNetwork.performRequest(lowerPriorityReq)).thenReturn(mock(NetworkResponse.class));

    RequestQueue queue = new RequestQueue(new NoCache(), mMockNetwork, 1, mDelivery);
    queue.addRequestFinishedListener(listener);
    queue.add(lowerPriorityReq);
    queue.add(higherPriorityReq);
    queue.start();

    // you cannot do strict order verification in combination with timeouts with mockito 1.9.5 :(
    // as an alternative, first verify no requests have finished, while higherPriorityReq should be processing
    verifyNoMoreInteractions(listener);
    // verify higherPriorityReq goes through first
    verify(listener, timeout(100)).onRequestFinished(higherPriorityReq);
    // verify lowerPriorityReq goes last
    verify(listener, timeout(10)).onRequestFinished(lowerPriorityReq);
    queue.stop();
}
 
Example #19
Source File: RequestTest.java    From product-emm with Apache License 2.0 4 votes vote down vote up
public TestRequest(Priority priority) {
    super(Request.Method.GET, "", null);
    mPriority = priority;
}
 
Example #20
Source File: RequestTest.java    From CrossBow with Apache License 2.0 4 votes vote down vote up
@Override
public Priority getPriority() {
    return mPriority;
}
 
Example #21
Source File: RequestTest.java    From CrossBow with Apache License 2.0 4 votes vote down vote up
public TestRequest(Priority priority) {
    super(Request.Method.GET, "", null);
    mPriority = priority;
}
 
Example #22
Source File: RequestTest.java    From volley with Apache License 2.0 4 votes vote down vote up
public TestRequest(Priority priority) {
    super(Request.Method.GET, "", null);
    mPriority = priority;
}
 
Example #23
Source File: RequestTest.java    From volley with Apache License 2.0 4 votes vote down vote up
@Override
public Priority getPriority() {
    return mPriority;
}
 
Example #24
Source File: RequestTest.java    From product-emm with Apache License 2.0 4 votes vote down vote up
@Override
public Priority getPriority() {
    return mPriority;
}
 
Example #25
Source File: RequestTest.java    From product-emm with Apache License 2.0 4 votes vote down vote up
public TestRequest(Priority priority) {
    super(Request.Method.GET, "", null);
    mPriority = priority;
}
 
Example #26
Source File: RequestTest.java    From device-database with Apache License 2.0 4 votes vote down vote up
public TestRequest(Priority priority) {
    super(Request.Method.GET, "", null);
    mPriority = priority;
}
 
Example #27
Source File: RequestTest.java    From product-emm with Apache License 2.0 4 votes vote down vote up
@Override
public Priority getPriority() {
    return mPriority;
}
 
Example #28
Source File: RequestTest.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
public TestRequest(Priority priority) {
    super(Request.Method.GET, "", null);
    mPriority = priority;
}
 
Example #29
Source File: RequestTest.java    From device-database with Apache License 2.0 4 votes vote down vote up
@Override
public Priority getPriority() {
    return mPriority;
}
 
Example #30
Source File: RequestTest.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
@Override
public Priority getPriority() {
    return mPriority;
}