Skip to content

Commit

Permalink
fix: using correct binding when using 'waitForResult' strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shelomentsev committed Apr 4, 2022
1 parent 5a31000 commit 1aef8cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/managers/BaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export abstract class BaseManager {
[
LockedKeyRetrieveStrategyTypes.waitForResult,
new WaitForResultLockedKeyRetrieveStrategy({
keyLockCheckFn: this.storage.keyIsLocked.bind(this),
getRecord: this.storage.get.bind(this),
keyLockCheckFn: this.storage.keyIsLocked.bind(this.storage),
getRecord: this.storage.get.bind(this.storage),
logger: this.logger,
}),
],
Expand Down Expand Up @@ -69,10 +69,7 @@ export abstract class BaseManager {
(keyLockError as Error).message
}. Running executor`
);

return runExecutor(context.executor);
}

if (!isKeySuccessfullyLocked) {
return lockedKeyRetrieveStrategy.get(context);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Redis from "ioredis";
import { v4 as uuid } from "uuid";
import Cache from "../../src/Cache";
import RedisStorageAdapter from "../../src/adapters/RedisStorageAdapter";
import MemcachedStorageAdapter from "../../src/adapters/MemcachedStorageAdapter";
import timeout from "../../src/timeout";
import Memcached from "memcached";

const logger = {
info: jest.fn(),
Expand All @@ -16,10 +18,15 @@ const redis = new Redis({
enableReadyCheck: true,
autoResendUnfulfilledCommands: false,
});
const memcached = new Memcached("localhost:11211");
const cache = new Cache({
adapter: new RedisStorageAdapter(redis, { operationTimeout: 9000 }),
logger,
});
const memcache = new Cache({
adapter: new MemcachedStorageAdapter(memcached),
logger,
});

const REDIS_OPERATION_DELAY = 1000;

Expand Down Expand Up @@ -115,4 +122,24 @@ describe("Cache", () => {

await expect(cache.get(key, executor, { tags })).resolves.toEqual(struct);
});

it("Redis concurrent test", async () => {
const tasks = [];
const executor = async () => new Date().toISOString();

for (let j = 0; j < 500; j++) {
tasks.push(cache.get("TEST", executor, { lockedKeyRetrieveStrategyType: "waitForResult" }));
}
expect(new Set(await Promise.all(tasks)).size).toEqual(1);
}, 3000);

it("Memcached concurrent test", async () => {
const tasks = [];
const executor = async () => new Date().toISOString();

for (let j = 0; j < 500; j++) {
tasks.push(memcache.get("TEST", executor, { lockedKeyRetrieveStrategyType: "waitForResult" }));
}
expect(new Set(await Promise.all(tasks)).size).toEqual(1);
}, 3000);
});

0 comments on commit 1aef8cc

Please sign in to comment.