Example #1
0
  public void testGetWithManyClients() throws InterruptedException, ExecutionException {
    // Check initial state
    Assert.assertFalse(fTestCache.isValid());

    // 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();
      qList.add(q);
    }
    // Wait until the cache starts data retrieval.
    waitForRetrieveRm();

    // Check state while waiting for data
    Assert.assertFalse(fTestCache.isValid());

    // Set the data to the callback
    Protocol.invokeLater(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();
          }
        });

    for (Query<Integer> q : qList) {
      Assert.assertEquals(1, (int) q.get());
    }

    // Check final state
    assertCacheValidWithData(1);
  }
Example #2
0
  public void commitTransaction(IObjectScope scope, ICmdbTransaction cmdbTx) {
    idCache.clear();
    aliasCache.clear();

    Session session = null;
    Transaction tx = null;
    Profiler.start("commitTx()");
    try {
      session = getSession(cmdbTx.getSession());
      tx = session.beginTransaction();
      for (ICi item : scope.getDestroyedICis()) {
        session.delete(item);
      }
      for (ICi item : scope.getNewICis()) {
        session.save(item);
      }
      for (ICi item : scope.getModifiedICis()) {
        session.update(item);
      }
      cmdbTx.setEndTs(new Date());
      storeTx(session, cmdbTx); // session.update(cmdbTx);
      tx.commit();

    } catch (HibernateException he) {
      if (tx != null) {
        tx.rollback();
      }
      throw he;
    } finally {
      closeSession(session);

      Profiler.stop("commitTx()");
    }
  }
Example #3
0
  public void testGetWithTwoClients() throws InterruptedException, ExecutionException {
    // Check initial state
    Assert.assertFalse(fTestCache.isValid());

    // Request data from cache
    Query<Integer> q1 = new TestQuery();
    q1.invoke();

    // Request data from cache again
    Query<Integer> q2 = new TestQuery();
    q2.invoke();

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

    // Check state while waiting for data
    Assert.assertFalse(fTestCache.isValid());

    // Set the data to the callback
    Protocol.invokeLater(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();
          }
        });

    Assert.assertEquals(1, (int) q1.get());
    Assert.assertEquals(1, (int) q2.get());

    // Check final state
    assertCacheValidWithData(1);
  }
Example #4
0
  public void testGet() throws InterruptedException, ExecutionException {
    // Request data from cache
    Query<Integer> q = new TestQuery();

    // Check initial state
    Assert.assertFalse(fTestCache.isValid());

    q.invoke();

    // Wait until the cache requests the data.
    waitForRetrieveRm();

    // Check state while waiting for data
    Assert.assertFalse(fTestCache.isValid());

    // Complete the cache's retrieve data request.
    Protocol.invokeAndWait(
        new Runnable() {
          public void run() {
            fRetrieveRm.setData(1);
            fRetrieveRm.done();

            // Check that the data is available in the cache immediately
            // (in the same dispatch cycle).
            Assert.assertEquals(1, (int) fTestCache.getData());
            Assert.assertTrue(fTestCache.isValid());
          }
        });

    Assert.assertEquals(1, (int) q.get());

    // Re-check final state
    assertCacheValidWithData(1);
  }
Example #5
0
 private void assertCacheResetWithoutData() {
   Assert.assertFalse(fTestCache.isValid());
   try {
     fTestCache.getData();
     Assert.fail("Expected an IllegalStateException");
   } catch (IllegalStateException e) {
   }
   try {
     fTestCache.getError();
     Assert.fail("Expected an IllegalStateException");
   } catch (IllegalStateException e) {
   }
 }
Example #6
0
 private void assertCacheInvalidAndWithCanceledRM() {
   Assert.assertFalse(fTestCache.isValid());
   try {
     fTestCache.getData();
     Assert.fail("Expected an IllegalStateException");
   } catch (IllegalStateException e) {
   }
   try {
     fTestCache.getError();
     Assert.fail("Expected an IllegalStateException");
   } catch (IllegalStateException e) {
   }
   Assert.assertTrue(fRetrieveRm.isCanceled());
 }
Example #7
0
  public void rejectTransaction(ICmdbTransaction cmdbTx) {
    idCache.clear();
    aliasCache.clear();

    Session session = getSession(cmdbTx.getSession());
    Transaction tx = null;

    try {
      tx = session.beginTransaction();
      cmdbTx.setEndTs(new Date());
      storeTx(session, cmdbTx);
      tx.commit();
    } catch (HibernateException he) {
      if (tx != null) {
        tx.rollback();
      }
      throw he;
    } finally {
      closeSession(session);
    }
  }
Example #8
0
 private void assertCacheValidWithData(Object data) {
   Assert.assertTrue(fTestCache.isValid());
   Assert.assertEquals(data, fTestCache.getData());
   Assert.assertNull(fTestCache.getError());
 }
Example #9
0
 private void assertCacheValidWithoutData() {
   Assert.assertTrue(fTestCache.isValid());
   Assert.assertEquals(null, fTestCache.getData());
   Assert.assertNotNull(fTestCache.getError());
   Assert.assertEquals(fTestCache.getError(), ERROR_TARGET_RUNNING);
 }