Skip to content

Commit

Permalink
fix: uninitialized object
Browse files Browse the repository at this point in the history
  • Loading branch information
SaurusXI committed Nov 17, 2023
1 parent 05d97da commit e47f435
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class MultilevelCache {
const redisCacheResults = await this.redisCache.batchGet(redisQueryKeys);

let redisIdx = 0;
let out: any[];
const out: any[] = [];
inMemoryResults.forEach((val, i) => {
if (val === null && redisIdx < redisCacheResults.length) {
out[i] = redisCacheResults[redisIdx];
Expand Down
5 changes: 3 additions & 2 deletions tests/batched.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Redis from "ioredis"
import SugarCache from "../lib/main";
import { logger } from './index.test';

describe('Batched operations', () => {
const redis = new Redis({
Expand Down Expand Up @@ -58,8 +59,8 @@ describe('Batched operations', () => {
const cachedVals = await cache.mget(keys);
expect(cachedVals).toStrictEqual([null, null]);

const v0 = cache.get(keys[0]);
const v1 = cache.get(keys[1]);
const v0 = await cache.get(keys[0]);
const v1 = await cache.get(keys[1]);

expect(v0).toBeNull();
expect(v1).toBeNull();
Expand Down
7 changes: 7 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import Redis from 'ioredis';
import SugarCache from '../lib/main';

export const logger = {
info: (message: string, ...args: any[]) => console.log(message),
debug: (message: string, ...args: any[]) => console.log(message),
warn: (message: string, ...args: any[]) => console.log(message),
error: (message: string, ...args: any[]) => console.log(message),
}

describe('Functional tests', () => {
const redis = new Redis({
port: 6379,
Expand Down
12 changes: 2 additions & 10 deletions tests/multilevel.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import Redis from "ioredis";
import SugarCache from "../lib/main";
import { logger } from './index.test';

const resourceId = 'resource-UUID';

const logger = {
info: (message: string, ...args: any[]) => console.log(message),
debug: (message: string, ...args: any[]) => console.log(message),
warn: (message: string, ...args: any[]) => console.log(message),
error: (message: string, ...args: any[]) => console.log(message),
}
const redis = new Redis({
port: 6379,
host: '127.0.0.1',
Expand All @@ -17,7 +12,7 @@ const redis = new Redis({
const mockRedisReturnVal = 'REDIS_VAL';

describe('Multilevel caching', () => {
const cacheWithMockedRedis = new SugarCache(redis, { namespace: 'multilevel', inMemoryCache: { enable: true, memoryThresholdPercentage: 0.9} }, logger);
const cacheWithMockedRedis = new SugarCache(redis, { namespace: 'multilevel', inMemoryCache: { enable: true, memoryThresholdPercentage: 0.9} });

class Controller {
static mockLatency = 500;
Expand All @@ -33,7 +28,6 @@ describe('Multilevel caching', () => {
ttl: 100
})
async get_fixedTTL(resourceId: string) {
console.log('executing function');
await new Promise((resolve) => setTimeout(resolve, Controller.mockLatency));
return Controller.returnVal;
}
Expand Down Expand Up @@ -74,8 +68,6 @@ describe('Multilevel caching', () => {
const result = await controller.get_fixedTTL(resourceId);
// Value is returned from memory, so should be the actual value being returned by controller
expect(result).toStrictEqual(Controller.returnVal);

console.log('Test should complete here');
},
// Since there are two controller calls and the second one is cached, upper bound is 2*mockLatency
Controller.mockLatency * 2)
Expand Down

0 comments on commit e47f435

Please sign in to comment.