Implement JCacheCache#putIfAbsent as atomic operation
This commit modifies putIfAbsent to use an EntryProcessor that guarantees that the operation is atomic. Closes gh-21591
This commit is contained in:
@@ -24,9 +24,12 @@ import javax.cache.spi.CachingProvider;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@@ -79,4 +82,22 @@ public class JCacheEhCacheApiTests extends AbstractValueAdaptingCacheTests<JCach
|
||||
return this.nativeCache;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPutIfAbsentNullValue() {
|
||||
JCacheCache cache = getCache(true);
|
||||
|
||||
String key = createRandomKey();
|
||||
String value = null;
|
||||
|
||||
assertThat(cache.get(key)).isNull();
|
||||
assertThat(cache.putIfAbsent(key, value)).isNull();
|
||||
assertThat(cache.get(key).get()).isEqualTo(value);
|
||||
org.springframework.cache.Cache.ValueWrapper wrapper = cache.putIfAbsent(key, "anotherValue");
|
||||
// A value is set but is 'null'
|
||||
assertThat(wrapper).isNotNull();
|
||||
assertThat(wrapper.get()).isNull();
|
||||
// not changed
|
||||
assertThat(cache.get(key).get()).isEqualTo(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user