public void testRepeatableRead()

in geode-core/src/integrationTest/java/org/apache/geode/TXJUnitTest.java [5204:5493]


  public void testRepeatableRead() throws CacheException {
    final TXManagerImpl txMgrImpl = (TXManagerImpl) txMgr;
    TXStateProxy tx;

    // try repeating a get and make sure it doesn't cause a conflict
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    assertEquals("value1", region.get("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);

    assertEquals("value1", region.get("key1"));
    txMgrImpl.commit();

    // try repeating a get and modify the entry and make sure it causes a conflict
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    assertEquals("value1", region.get("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals("value1", region.get("key1"));
    region.put("key1", "value3");
    assertEquals("value3", region.get("key1"));
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try repeating a getEntry and make sure it doesn't cause a conflict
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    region.getEntry("key1");
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);

    assertEquals("value1", region.get("key1"));
    txMgrImpl.commit();

    // try repeating a getEntry and modify the entry and make sure it causes a conflict
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    region.getEntry("key1");
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    region.put("key1", "value3");
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try RR when entry fetched using entrySet
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    region.get("key1"); // bootstrap the tx, entrySet does not
    region.entrySet(false).iterator().next();
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);

    assertEquals("value1", region.get("key1"));
    txMgrImpl.commit();

    // try RRW->CONFLICT when entry fetched using entrySet
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    region.get("key1"); // bootstrap the tx, entrySet does not
    region.entrySet(false).iterator().next();
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals("value1", region.get("key1"));
    region.put("key1", "value3");
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try containsKey
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    assertEquals(true, region.containsKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(true, region.containsKey("key1"));
    txMgrImpl.commit();
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    assertEquals(true, region.containsKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(true, region.containsKey("key1"));
    region.put("key1", "value3");
    assertEquals(true, region.containsKey("key1"));
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }
    // try containsValueForKey
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    assertEquals(true, region.containsValueForKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(true, region.containsValueForKey("key1"));
    txMgrImpl.commit();
    region.put("key1", "value1"); // non-tx
    txMgrImpl.begin();
    assertEquals(true, region.containsValueForKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(true, region.containsValueForKey("key1"));
    region.put("key1", "value3");
    assertEquals(true, region.containsValueForKey("key1"));
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // now try the same things but with no entry in committed state at
    // the time of the first read
    // try repeating a get and make sure it doesn't cause a conflict
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(null, region.get("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);

    assertEquals(null, region.get("key1"));
    txMgrImpl.commit();

    // try repeating a get and modify the entry and make sure it causes a conflict
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(null, region.get("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(null, region.get("key1"));
    region.put("key1", "value3");
    assertEquals("value3", region.get("key1"));
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try repeating a getEntry and make sure it doesn't cause a conflict
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(null, region.getEntry("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);

    assertEquals(null, region.getEntry("key1"));
    txMgrImpl.commit();

    // try repeating a getEntry and modify the entry and make sure it causes a conflict
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(null, region.getEntry("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(null, region.getEntry("key1"));
    region.put("key1", "value3");
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try containsKey
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(false, region.containsKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(false, region.containsKey("key1"));
    txMgrImpl.commit();
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(false, region.containsKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(false, region.containsKey("key1"));
    region.put("key1", "value3");
    assertEquals(true, region.containsKey("key1"));
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try containsValueForKey
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(false, region.containsValueForKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(false, region.containsValueForKey("key1"));
    txMgrImpl.commit();
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    assertEquals(false, region.containsValueForKey("key1"));
    tx = txMgrImpl.pauseTransaction();
    region.put("key1", "value2"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(false, region.containsValueForKey("key1"));
    region.put("key1", "value3");
    assertEquals(true, region.containsValueForKey("key1"));
    try {
      txMgrImpl.commit();
      fail("expected CommitConflictException");
    } catch (CommitConflictException ignored) {
    }

    // try an invalidate of an already invalid entry
    region.remove("key1"); // non-tx
    region.create("key1", null); // non-tx
    txMgrImpl.begin();
    region.get("key1");
    region.localInvalidate("key1"); // should be a noop since it is already invalid
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    txMgrImpl.commit();
    assertEquals(false, region.containsKey("key1"));

    // make sure a noop invalidate is repeatable read
    region.remove("key1"); // non-tx
    region.create("key1", null); // non-tx
    txMgrImpl.begin();
    region.localInvalidate("key1"); // should be a noop since it is already invalid
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(true, region.containsKey("key1"));
    assertEquals(false, region.containsValueForKey("key1"));
    txMgrImpl.commit();
    assertEquals(false, region.containsKey("key1"));

    // make sure a destroy that throws entryNotFound is repeatable read
    region.remove("key1"); // non-tx
    txMgrImpl.begin();
    try {
      region.localDestroy("key1");
      fail("expected EntryNotFoundException");
    } catch (EntryNotFoundException ignored) {
    }
    tx = txMgrImpl.pauseTransaction();
    region.create("key1", "value1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(false, region.containsKey("key1"));
    txMgrImpl.commit();
    assertEquals(true, region.containsKey("key1"));
    region.remove("key1"); // non-tx

    // make sure a create that throws entryExists is repeatable read
    region.create("key1", "non-tx-value1"); // non-tx
    txMgrImpl.begin();
    try {
      region.create("key1", "value1");
      fail("expected EntryExistsException");
    } catch (EntryExistsException ignored) {
    }
    tx = txMgrImpl.pauseTransaction();
    region.remove("key1"); // non-tx
    txMgrImpl.unpauseTransaction(tx);
    assertEquals(true, region.containsKey("key1"));
    txMgrImpl.commit();
    assertEquals(false, region.containsKey("key1"));
  }