Skip to content

Commit

Permalink
Issue ehcache#37 Make sure we update if MutableEntry.setValue was inv…
Browse files Browse the repository at this point in the history
…oked
  • Loading branch information
alexsnaps committed Dec 9, 2014
1 parent 9521e2f commit c334085
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ehcache-jcache/src/main/java/org/ehcache/jcache/JCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ private static class JMutableEntry<K, V> implements MutableEntry<K, V> {
private final boolean fromLoader;
private final V initialValue;
private volatile V newValue;
private volatile boolean updated;
private volatile boolean deleted;
private volatile boolean skipDelete;

Expand All @@ -852,6 +853,7 @@ public void remove() {
skipDelete = initialValue == null && newValue != null;
newValue = null;
deleted = true;
updated = false;
}

@Override
Expand All @@ -860,6 +862,7 @@ public void setValue(final V value) {
throw new EntryProcessorException();
}
deleted = false;
updated = true;
newValue = value;
}

Expand Down Expand Up @@ -889,7 +892,7 @@ void apply(final JCache<K, V> jCache) {
if(deleted && !skipDelete) {
jCache.remove(key);
}
if(newValue != initialValue && newValue != null) {
if(updated && newValue != null) {
jCache.put(key, newValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ModifiedExpiryPolicy;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.MutableEntry;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
Expand Down Expand Up @@ -62,4 +67,25 @@ public void nullCacheWhenNoCacheExists() {
assertThat(jcache, nullValue());
}

@Test
public void testUpdatesCacheWhenSettingMutableEntryValue() {
final CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
Cache<String, AtomicBoolean> cache = cacheManager.createCache("testUpdatesCacheWhenSettingMutableEntryValue", new MutableConfiguration<String, AtomicBoolean>());
try {
cache.put("key", new AtomicBoolean());
assertThat(cache.invoke("key", new EntryProcessor<String, AtomicBoolean, Boolean>() {
@Override
public Boolean process(final MutableEntry<String, AtomicBoolean> entry, final Object... arguments) throws EntryProcessorException {
final AtomicBoolean value = entry.getValue();
final boolean previous = value.getAndSet(true);
entry.setValue(value);
return previous;
}
}), is(false));
assertThat(cache.get("key").get(), is(true));
} finally {
cacheManager.destroyCache(cache.getName());
}
}

}

0 comments on commit c334085

Please sign in to comment.