📄 abstractentityregionaccessstrategytestcase.java
字号:
assertNull("node1 starts clean", localAccessStrategy.get(KEY, txTimestamp)); writeLatch1.await(); if (useMinimalAPI) { localAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true); } else { localAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1)); } localAccessStrategy.update(KEY, VALUE2, new Integer(2), new Integer(1)); BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node1 caught exception", e); node1Exception = e; rollback(); } catch (AssertionFailedError e) { node1Failure = e; rollback(); } finally { // Let node2 write writeLatch2.countDown(); completionLatch.countDown(); } } }; Thread node2 = new Thread() { public void run() { try { long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin(); assertNull("node1 starts clean", remoteAccessStrategy.get(KEY, txTimestamp)); // Let node1 write writeLatch1.countDown(); // Wait for node1 to finish writeLatch2.await(); if (useMinimalAPI) { remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true); } else { remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1)); } BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node2 caught exception", e); node2Exception = e; rollback(); } catch (AssertionFailedError e) { node2Failure = e; rollback(); } finally { completionLatch.countDown(); } } }; node1.setDaemon(true); node2.setDaemon(true); node1.start(); node2.start(); assertTrue("Threads completed", completionLatch.await(2, TimeUnit.SECONDS)); assertThreadsRanCleanly(); long txTimestamp = System.currentTimeMillis(); assertEquals("Correct node1 value", VALUE2, localAccessStrategy.get(KEY, txTimestamp)); if (isUsingInvalidation()) { if (isUsingOptimisticLocking()) // The node is "deleted" but it's ghost data version prevents the stale node2 PFER assertEquals("Correct node2 value", null, remoteAccessStrategy.get(KEY, txTimestamp)); else { // no data version to prevent the PFER; we count on db locks preventing this assertEquals("Expected node2 value", VALUE1, remoteAccessStrategy.get(KEY, txTimestamp)); } } else { // The node1 update is replicated, preventing the node2 PFER assertEquals("Correct node2 value", VALUE2, remoteAccessStrategy.get(KEY, txTimestamp)); } } /** * Test method for {@link TransactionalAccess#insert(java.lang.Object, java.lang.Object, java.lang.Object)}. */ public void testInsert() throws Exception { final String KEY = KEY_BASE + testCount++; final CountDownLatch readLatch = new CountDownLatch(1); final CountDownLatch commitLatch = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch(2); Thread inserter = new Thread() { public void run() { try { long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin(); assertNull("Correct initial value", localAccessStrategy.get(KEY, txTimestamp)); localAccessStrategy.insert(KEY, VALUE1, new Integer(1)); readLatch.countDown(); commitLatch.await(); BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node1 caught exception", e); node1Exception = e; rollback(); } catch (AssertionFailedError e) { node1Failure = e; rollback(); } finally { completionLatch.countDown(); } } }; Thread reader = new Thread() { public void run() { try { long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin(); readLatch.await(); Object expected = isUsingOptimisticLocking() ? null : VALUE1; assertEquals("Correct initial value", expected, localAccessStrategy.get(KEY, txTimestamp)); BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node1 caught exception", e); node1Exception = e; rollback(); } catch (AssertionFailedError e) { node1Failure = e; rollback(); } finally { commitLatch.countDown(); completionLatch.countDown(); } } }; inserter.setDaemon(true); reader.setDaemon(true); inserter.start(); reader.start(); if (isUsingOptimisticLocking()) assertTrue("Threads completed", completionLatch.await(1, TimeUnit.SECONDS)); else { // Reader should be blocking for lock assertFalse("Threads completed", completionLatch.await(250, TimeUnit.MILLISECONDS)); commitLatch.countDown(); assertTrue("Threads completed", completionLatch.await(1, TimeUnit.SECONDS)); } assertThreadsRanCleanly(); long txTimestamp = System.currentTimeMillis(); assertEquals("Correct node1 value", VALUE1, localAccessStrategy.get(KEY, txTimestamp)); Object expected = isUsingInvalidation() ? null : VALUE1; assertEquals("Correct node2 value", expected, remoteAccessStrategy.get(KEY, txTimestamp)); } /** * Test method for {@link TransactionalAccess#update(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object)}. */ public void testUpdate() throws Exception { final String KEY = KEY_BASE + testCount++; // Set up initial state localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); // Let the async put propagate sleep(250); final CountDownLatch readLatch = new CountDownLatch(1); final CountDownLatch commitLatch = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch(2); Thread updater = new Thread() { public void run() { try { long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin(); assertEquals("Correct initial value", VALUE1, localAccessStrategy.get(KEY, txTimestamp)); localAccessStrategy.update(KEY, VALUE2, new Integer(2), new Integer(1)); readLatch.countDown(); commitLatch.await(); BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node1 caught exception", e); node1Exception = e; rollback(); } catch (AssertionFailedError e) { node1Failure = e; rollback(); } finally { completionLatch.countDown(); } } }; Thread reader = new Thread() { public void run() { try { long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin(); readLatch.await(); // This will block w/ pessimistic locking and then // read the new value; w/ optimistic it shouldn't // block and will read the old value Object expected = isUsingOptimisticLocking() ? VALUE1 : VALUE2; assertEquals("Correct value", expected, localAccessStrategy.get(KEY, txTimestamp)); BatchModeTransactionManager.getInstance().commit(); } catch (Exception e) { log.error("node1 caught exception", e); node1Exception = e; rollback(); } catch (AssertionFailedError e) { node1Failure = e; rollback();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -