Example #1
0
  public void testCancelWhilePendingWithTwoClients()
      throws InterruptedException, ExecutionException {

    // Request data from cache. Use an additional invokeAndWait to
    // ensure both update requests are initiated before we wait
    // for retrieval to start
    Query<Integer> q1 = new TestQuery();
    q1.invoke();
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {}
        });

    // Request data from cache again
    Query<Integer> q2 = new TestQuery();
    q2.invoke();
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {}
        });

    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Cancel the first client request
    q1.cancel(true);
    try {
      q1.get();
      Assert.fail("Expected a cancellation exception");
    } catch (CancellationException e) {
    } // Expected exception;
    assertCacheWaiting();

    // Cancel the second request
    q2.cancel(true);
    try {
      q2.get();
      Assert.fail("Expected a cancellation exception");
    } catch (CancellationException e) {
    } // Expected exception;

    assertCacheInvalidAndWithCanceledRM();

    // Completed the retrieve RM
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();
          }
        });

    // Validate that cache didn't accept the result after its RM was canceled
    assertCacheInvalidAndWithCanceledRM();
  }
Example #2
0
  public void testCancelWhilePending3() throws InterruptedException, ExecutionException {
    // Request data from cache
    Query<Integer> q = new TestQuery();
    q.invoke();

    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Cancel the client request
    q.cancel(true);
    try {
      q.get();
      Assert.fail("Expected a cancellation exception");
    } catch (CancellationException e) {
    } // Expected exception;

    assertCacheInvalidAndWithCanceledRM();

    // Simulate retrieval logic that is regularly checking the RM's cancel
    // status and has discovered that the request has been canceled. It
    // aborts its processing, sets STATUS.CANCEL_STATUS in the RM and
    // completes it.
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.setError(new CancellationException());
            fRetrieveRm.done();
          }
        });

    // Validate that cache didn't accept the result after its RM was canceled
    assertCacheInvalidAndWithCanceledRM();
  }
Example #3
0
  public void testCancelWhilePending2() throws InterruptedException, ExecutionException {
    // Request data from cache
    Query<Integer> q = new TestQuery();
    q.invoke();

    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Cancel the client request
    q.cancel(true);
    try {
      q.get();
      Assert.fail("Expected a cancellation exception");
    } catch (CancellationException e) {
    } // Expected exception;

    assertCacheInvalidAndWithCanceledRM();

    // Simulate retrieval logic that is regularly checking the RM's cancel
    // status and has discovered that the request has been canceled. It
    // technically does not need to explicitly set a cancel status object in
    // the RM, thanks to RequestMonitor.getStatus() automatically returning
    // Status.CANCEL_STATUS when its in the cancel state. So here we
    // simulate the retrieval logic just aborting its operations and
    // completing the RM. Note that it hasn't provided the data to the
    // cache.
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.done();
          }
        });

    assertCacheInvalidAndWithCanceledRM();
  }
Example #4
0
  public void testCancelWhilePending() throws InterruptedException, ExecutionException {
    // Request data from cache
    Query<Integer> q = new TestQuery();
    q.invoke();

    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Cancel the client request
    q.cancel(true);
    try {
      q.get();
      Assert.fail("Expected a cancellation exception");
    } catch (CancellationException e) {
    } // Expected exception;

    assertCacheInvalidAndWithCanceledRM();

    // Simulate the retrieval completing successfully despite the cancel
    // request. Perhaps the retrieval logic isn't checking the RM status.
    // Even if it is checking, it may have gotten passed its last checkpoint
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();
          }
        });

    // Validate that cache didn't accept the result after its RM was canceled
    assertCacheInvalidAndWithCanceledRM();
  }
Example #5
0
  public void testCancelWhilePendingWithManyClients()
      throws InterruptedException, ExecutionException {
    // Request data from cache
    List<Query<Integer>> qList = new ArrayList<Query<Integer>>();
    for (int i = 0; i < 10; i++) {
      Query<Integer> q = new TestQuery();
      q.invoke();
      Protocol.invokeAndWait(
          new Runnable() {
            public void run() {}
          });
      qList.add(q);
    }

    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Cancel some client requests
    int[] toCancel = new int[] {0, 2, 5, 9};
    for (int i = 0; i < toCancel.length; i++) {

      // Cancel request and verify that its canceled
      Query<Integer> q = qList.get(toCancel[i]);
      q.cancel(true);
      try {
        q.get();
        Assert.fail("Expected a cancellation exception");
      } catch (CancellationException e) {
      } // Expected exception;
      qList.set(toCancel[i], null);

      assertCacheWaiting();
    }

    // Replace canceled requests with new ones
    for (int i = 0; i < toCancel.length; i++) {
      Query<Integer> q = new TestQuery();
      q.invoke();
      Protocol.invokeAndWait(
          new Runnable() {
            public void run() {}
          });
      qList.set(toCancel[i], q);
      assertCacheWaiting();
    }

    // Now cancel all requests
    for (int i = 0; i < (qList.size() - 1); i++) {
      // Validate that cache is still waiting and is not canceled
      assertCacheWaiting();
      qList.get(i).cancel(true);
    }
    qList.get(qList.size() - 1).cancel(true);
    assertCacheInvalidAndWithCanceledRM();

    // Completed the retrieve RM
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();
          }
        });

    // Validate that cache didn't accept the result after its RM was canceled
    assertCacheInvalidAndWithCanceledRM();
  }
Example #6
0
  public void testCancelWhilePendingWithoutClientNotification()
      throws InterruptedException, ExecutionException {
    final boolean canceledCalled[] = new boolean[] {false};

    fTestCache =
        new TestCache() {
          protected synchronized void canceled() {
            canceledCalled[0] = true;
          };
        };

    // Request data from cache
    Query<Integer> q =
        new Query<Integer>() {
          @Override
          protected void execute(final DataCallback<Integer> rm) {

            fTestCache.wait(
                new Callback(rm) {
                  @Override
                  public synchronized void addCancelListener(ICanceledListener listener) {
                    // Do not add the cancel listener so that the cancel request is not
                    // propagated to the cache.
                  }

                  @Override
                  protected void handleSuccess() {
                    rm.setData(fTestCache.getData());
                    rm.done();
                  }
                });
          }
        };
    q.invoke();

    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Cancel the client request
    q.cancel(true);

    assertCacheInvalidAndWithCanceledRM();

    // AbstractCache.canceled() should be called after isCanceled()
    // discovers that the client has canceled its request.  The canceled() method is
    // called in a separate dispatch cycle, so we have to wait one cycle of the executor
    // after is canceled is called.
    fRetrieveRm.isCanceled();
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {}
        });
    Assert.assertTrue(canceledCalled[0]);

    try {
      q.get();
      Assert.fail("Expected a cancellation exception");
    } catch (CancellationException e) {
    } // Expected exception;

    // Completed the retrieve RM
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();
          }
        });

    // Validate that cache didn't accept the result after its RM was canceled
    assertCacheInvalidAndWithCanceledRM();
  }