Skip to content

Commit

Permalink
add failing test "does not return 'the same object' from a cacheGet c…
Browse files Browse the repository at this point in the history
…all"

adds hook to inject thread delays for testing purposes
  • Loading branch information
davidAtInleague committed Nov 30, 2023
1 parent 56f7f98 commit 0f7d5c5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions source/java/src/lucee/extension/io/cache/redis/RedisCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public class RedisCache extends CacheSupport implements Command {

private final Object token = new Object();

/**
* Boxed type is to support representing the null case
*/
private Integer __test__writeCommitDelay_ms = null;
public Integer get__test__writeCommitDelay_ms() {
return __test__writeCommitDelay_ms;
}

public RedisCache() {
if (async) {
// storage = new Storage(this);
Expand All @@ -101,6 +109,9 @@ public void init(Config config, Struct arguments) throws IOException {
this.cl = arguments.getClass().getClassLoader();
if (config == null) config = CFMLEngineFactory.getInstance().getThreadConfig();

__test__writeCommitDelay_ms = caster.toIntValue(arguments.get("__test__writeCommitDelay_ms", null), 0);
__test__writeCommitDelay_ms = __test__writeCommitDelay_ms <= 0 ? null : __test__writeCommitDelay_ms;

host = caster.toString(arguments.get("host", "localhost"), "localhost");
port = caster.toIntValue(arguments.get("port", null), 6379);

Expand Down Expand Up @@ -960,6 +971,10 @@ public void run() {
synchronized (tokenAddToNear) {
if (entries.isEmpty()) tokenAddToNear.wait();
}

if (cache.get__test__writeCommitDelay_ms() != null) {
Thread.sleep(cache.get__test__writeCommitDelay_ms());
}
}
catch (Throwable e) {
if (cache.log != null) cache.log.error("redis-cache", e);
Expand Down
62 changes: 62 additions & 0 deletions tests/NearCacheEntriesAreCopiedOnReadPriorToWriteCommit.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
component extends="org.lucee.cfml.test.LuceeTestCase" labels="redis" {

public void function beforeAll(){
variables.cacheName = "NearCacheEntriesAreCopiedOnReadPriorToWriteCommit"
defineCache();
}

public void function afterAll(){
application action="update" caches={};
}

private string function defineCache(){
var redis = server.getDatasource("redis");
if ( structCount(redis) eq 0 )
throw "Redis is not configured?";

admin
action="updateCacheConnection"
type="server"
password=server.SERVERADMINPASSWORD
class="lucee.extension.io.cache.redis.simple.RedisCache"
bundleName="redis.extension"
name=cacheName
custom={
"minIdle":8,
"maxTotal":40,
"maxIdle":24,
"host":redis.server,
"port":redis.port,
"socketTimeout":2000,
"liveTimeout":3600000,
"idleTimeout":60000,
"timeToLiveSeconds":0,
"testOnBorrow":true,
"rnd":1,
"__test__writeCommitDelay_ms": 5000
},
default=""
readonly=false
storage=false
remoteClients="";
}

function run() {
describe("NearCacheEntriesAreCopiedOnReadPriorToWriteCommit", () => {
it("does not return 'the same object' from a cacheGet call", () => {
var someObj = {v: 42}
var key = "redis-test/#createGuid()#";

cachePut(key = key, value = someObj, cacheName = cacheName);

var fromCache = cacheGet(key = key, cacheName = cacheName);

expect(fromCache.v).toBe(42);

fromCache.v += 1;

expect(someObj.v).toBe(42, "mutating 'fromCache' did not mutate the initial cache source 'someObj'");
})
})
}
}

0 comments on commit 0f7d5c5

Please sign in to comment.