Author: doogie
Date: Mon Mar 15 07:21:59 2010 New Revision: 923094 URL: http://svn.apache.org/viewvc?rev=923094&view=rev Log: Reworked setObject to delay the actual setting until the next getObject call. Removes one hard to test branch. Just a single branch left to test. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLObject.java ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLObject.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLObject.java?rev=923094&r1=923093&r2=923094&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLObject.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/TTLObject.java Mon Mar 15 07:21:59 2010 @@ -80,7 +80,7 @@ public abstract class TTLObject<T> imple ExecutionPool.pulseAll(); } - public enum State { INVALID, REGEN, REGENERATING, GENERATE, GENERATING, GENERATING_INITIAL, VALID, ERROR, ERROR_INITIAL } + public enum State { INVALID, REGEN, REGENERATING, GENERATE, GENERATING, GENERATING_INITIAL, VALID, ERROR, ERROR_INITIAL, SET } private volatile ValueAndState<T> object = new StandardValueAndState<T>(this, null, null, State.INVALID, 0, null, null); private static final AtomicReferenceFieldUpdater<TTLObject, ValueAndState> objectAccessor = AtomicReferenceFieldUpdater.newUpdater(TTLObject.class, ValueAndState.class, "object"); private static final AtomicIntegerFieldUpdater<TTLObject> serialAccessor = AtomicIntegerFieldUpdater.newUpdater(TTLObject.class, "serial"); @@ -113,6 +113,10 @@ public abstract class TTLObject<T> imple return ttlObject.newValueAndState(value, null, State.VALID, serialAccessor.incrementAndGet(ttlObject), null, new Pulse(ttlObject)); } + protected ValueAndState<T> set(T value) { + return ttlObject.newValueAndState(value, null, State.SET, serialAccessor.incrementAndGet(ttlObject), null, null); + } + protected ValueAndState<T> submit(final T oldValue, State state) { return ttlObject.newValueAndState(getValue(), createTask(oldValue), state, serial, null, null); } @@ -184,6 +188,8 @@ public abstract class TTLObject<T> imple nextContainer = container.refresh(State.INVALID); } else if (container.state == State.ERROR || container.state == State.VALID) { nextContainer = container.refresh(getForeground() ? State.GENERATE : State.REGEN); + } else if (container.state == State.SET) { + nextContainer = container.refresh(getForeground() ? State.GENERATE : State.REGEN); } else { return; } @@ -201,14 +207,10 @@ public abstract class TTLObject<T> imple } protected final void setObject(T newObject) { - ValueAndState<T> container, nextContainer; - State nextState; - do { - container = getContainer(); - nextContainer = container.valid(newObject); - } while (!objectAccessor.compareAndSet(this, container, nextContainer)); + ValueAndState<T> container = getContainer(); + ValueAndState<T> nextContainer = container.set(newObject); + objectAccessor.compareAndSet(this, container, nextContainer); cancelFuture(container); - ExecutionPool.addPulse(nextContainer.pulse); } private void cancelFuture(ValueAndState<T> container) { @@ -231,6 +233,8 @@ public abstract class TTLObject<T> imple return container.getValue(); } else if (container.state == State.INVALID) { nextContainer = container.submit(getInitial(), State.GENERATING_INITIAL); + } else if (container.state == State.SET) { + nextContainer = container.valid(container.getValue()); } else if (container.state == State.REGENERATING || container.state == State.GENERATING || container.state == State.GENERATING_INITIAL) { if (container.state == State.REGENERATING && !container.future.isDone()) { return container.getValue(); Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java?rev=923094&r1=923093&r2=923094&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/AsyncTTLObjectTest.java Mon Mar 15 07:21:59 2010 @@ -69,6 +69,16 @@ public class AsyncTTLObjectTest extends assertGetObject("Refreshed with old data", "b", 3, 0, 100000000); Thread.sleep(350); assertGetObject("Refreshed with old data", "4", 5, 0, 100000000); + object.set("5"); + assertGetObject("set new data", "5", 5, 0, 100000000); + TTLObject.pulseAll(); + sleepTime = 200; + loadData = "c"; + object.set("5"); + object.refresh(); + assertGetObject("refresh after set", "5", 5, 0, 100000000); + Thread.sleep(300); + assertGetObject("refresh after set", "c", 6, 0, 100000000); } public void testSet() throws Exception { Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java?rev=923094&r1=923093&r2=923094&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/concurrent/test/SyncTTLObjectTest.java Mon Mar 15 07:21:59 2010 @@ -125,6 +125,13 @@ public class SyncTTLObjectTest extends T assertEquals("two dones", 3, doneCount.get()); object.getObject(); assertEquals("two dones", 4, doneCount.get()); + object.set("one"); + assertEquals("one", "one", object.getObject()); + assertEquals("two dones", 4, doneCount.get()); + object.set("two"); + object.refresh(); + assertEquals("two", (String) null, object.getObject()); + assertEquals("two dones", 5, doneCount.get()); } public void testGetTTL() throws Exception { |
Free forum by Nabble | Edit this page |