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:
Stéphane Nicoll
2023-09-18 09:09:47 +02:00
parent 57a4628934
commit f79bc7b14d
2 changed files with 40 additions and 3 deletions

View File

@@ -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);
}
}