Skip to content

Commit

Permalink
Add atomic value tests for various configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Sep 30, 2015
1 parent 8f943b1 commit f1981db
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 65 deletions.
Expand Up @@ -24,7 +24,6 @@
import java.time.Duration; import java.time.Duration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function; import java.util.function.Function;


/** /**
Expand All @@ -34,7 +33,7 @@
*/ */
public class AtomicValueState extends StateMachine { public class AtomicValueState extends StateMachine {
private final Map<Session, Commit<AtomicValueCommands.Listen>> listeners = new HashMap<>(); private final Map<Session, Commit<AtomicValueCommands.Listen>> listeners = new HashMap<>();
private final AtomicReference<Object> value = new AtomicReference<>(); private Object value;
private Commit<? extends AtomicValueCommands.ValueCommand> current; private Commit<? extends AtomicValueCommands.ValueCommand> current;
private Scheduled timer; private Scheduled timer;


Expand Down Expand Up @@ -89,7 +88,7 @@ private void change(Object value) {
*/ */
protected Object get(Commit<AtomicValueCommands.Get> commit) { protected Object get(Commit<AtomicValueCommands.Get> commit) {
try { try {
return current != null ? value.get() : null; return current != null ? value : null;
} finally { } finally {
commit.close(); commit.close();
} }
Expand All @@ -113,28 +112,29 @@ private void cleanCurrent() {
*/ */
private void setCurrent(Commit<? extends AtomicValueCommands.ValueCommand> commit) { private void setCurrent(Commit<? extends AtomicValueCommands.ValueCommand> commit) {
timer = commit.operation().ttl() > 0 ? executor().schedule(Duration.ofMillis(commit.operation().ttl()), () -> { timer = commit.operation().ttl() > 0 ? executor().schedule(Duration.ofMillis(commit.operation().ttl()), () -> {
value.set(null); value = null;
current.clean(); current.clean();
current = null; current = null;
}) : null; }) : null;
current = commit; current = commit;
change(value.get()); change(value);
} }


/** /**
* Applies a set commit. * Applies a set commit.
*/ */
protected void set(Commit<AtomicValueCommands.Set> commit) { protected void set(Commit<AtomicValueCommands.Set> commit) {
cleanCurrent(); cleanCurrent();
value.set(commit.operation().value()); value = commit.operation().value();
setCurrent(commit); setCurrent(commit);
} }


/** /**
* Handles a compare and set commit. * Handles a compare and set commit.
*/ */
protected boolean compareAndSet(Commit<AtomicValueCommands.CompareAndSet> commit) { protected boolean compareAndSet(Commit<AtomicValueCommands.CompareAndSet> commit) {
if (value.compareAndSet(commit.operation().expect(), commit.operation().update())) { if ((value == null && commit.operation().expect() == null) || (value != null && commit.operation().expect() != null && value.equals(commit.operation().expect()))) {
value = commit.operation().update();
cleanCurrent(); cleanCurrent();
setCurrent(commit); setCurrent(commit);
return true; return true;
Expand All @@ -146,7 +146,8 @@ protected boolean compareAndSet(Commit<AtomicValueCommands.CompareAndSet> commit
* Handles a get and set commit. * Handles a get and set commit.
*/ */
protected Object getAndSet(Commit<AtomicValueCommands.GetAndSet> commit) { protected Object getAndSet(Commit<AtomicValueCommands.GetAndSet> commit) {
Object result = value.getAndSet(commit.operation().value()); Object result = value;
value = commit.operation().value();
cleanCurrent(); cleanCurrent();
setCurrent(commit); setCurrent(commit);
return result; return result;
Expand Down

0 comments on commit f1981db

Please sign in to comment.