@autofleet/matmon 2.3.1-beta-4 → 2.3.1-beta-5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cache.js +1 -1
- package/lib/cache.test.js +61 -65
- package/lib/promise-utils.d.ts +1 -0
- package/lib/promise-utils.js +19 -0
- package/lib/redis/index.js +3 -9
- package/lib/redis/index.test.js +22 -25
- package/package.json +7 -10
package/lib/cache.js
CHANGED
|
@@ -113,7 +113,7 @@ const getMultipleWithCache = ({ getFromCache, multiGetterFromCache, setInCache,
|
|
|
113
113
|
const valuesFromGetter = await (multiGetter?.([...queriesMap.values()]) || // Use multiGetter if it's provided
|
|
114
114
|
Promise.all([...queriesMap.values()].map(id => getter(id))) // Otherwise, iterate over the queries with getter
|
|
115
115
|
);
|
|
116
|
-
if (setMultiInCache) {
|
|
116
|
+
if (setMultiInCache && valuesFromGetter.length > 0) {
|
|
117
117
|
const setCacheObject = valuesFromGetter.reduce((acc, value) => {
|
|
118
118
|
acc[getIdField(value, idField)] = value;
|
|
119
119
|
return acc;
|
package/lib/cache.test.js
CHANGED
|
@@ -1,36 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
3
4
|
const cache_1 = require("./cache");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
wrapWithMutex: jest.fn((mutex, fn) => fn()),
|
|
8
|
-
}));
|
|
9
|
-
describe('Cache', () => {
|
|
10
|
-
describe('getNewLRU', () => {
|
|
11
|
-
it('should create a new LRU cache with default size', () => {
|
|
5
|
+
(0, vitest_1.describe)('Cache', () => {
|
|
6
|
+
(0, vitest_1.describe)('getNewLRU', () => {
|
|
7
|
+
(0, vitest_1.it)('should create a new LRU cache with default size', () => {
|
|
12
8
|
const cache = (0, cache_1.getNewLRU)(60);
|
|
13
|
-
expect(cache.max).toBe(300);
|
|
9
|
+
(0, vitest_1.expect)(cache.max).toBe(300);
|
|
14
10
|
});
|
|
15
|
-
it('should create a new LRU cache with specified size', () => {
|
|
11
|
+
(0, vitest_1.it)('should create a new LRU cache with specified size', () => {
|
|
16
12
|
const cache = (0, cache_1.getNewLRU)(60, 500);
|
|
17
|
-
expect(cache.max).toBe(500);
|
|
13
|
+
(0, vitest_1.expect)(cache.max).toBe(500);
|
|
18
14
|
});
|
|
19
|
-
it('should not exceed the maximum size', () => {
|
|
15
|
+
(0, vitest_1.it)('should not exceed the maximum size', () => {
|
|
20
16
|
const cache = (0, cache_1.getNewLRU)(60, 200000);
|
|
21
|
-
expect(cache.max).toBe(100000);
|
|
17
|
+
(0, vitest_1.expect)(cache.max).toBe(100000);
|
|
22
18
|
});
|
|
23
19
|
});
|
|
24
|
-
describe('getWithCacheSupport', () => {
|
|
20
|
+
(0, vitest_1.describe)('getWithCacheSupport', () => {
|
|
25
21
|
const cacheKey = 'testKey';
|
|
26
22
|
const createMocks = () => {
|
|
27
|
-
const cacheGet =
|
|
28
|
-
const cacheSet =
|
|
29
|
-
const fetching =
|
|
30
|
-
const getFromCache =
|
|
31
|
-
const setInCache =
|
|
32
|
-
const multiGetterFromCache =
|
|
33
|
-
const setMultiInCache =
|
|
23
|
+
const cacheGet = vitest_1.vi.fn();
|
|
24
|
+
const cacheSet = vitest_1.vi.fn();
|
|
25
|
+
const fetching = vitest_1.vi.fn();
|
|
26
|
+
const getFromCache = vitest_1.vi.fn();
|
|
27
|
+
const setInCache = vitest_1.vi.fn();
|
|
28
|
+
const multiGetterFromCache = vitest_1.vi.fn();
|
|
29
|
+
const setMultiInCache = vitest_1.vi.fn();
|
|
34
30
|
return {
|
|
35
31
|
cacheGet,
|
|
36
32
|
cacheSet,
|
|
@@ -41,56 +37,56 @@ describe('Cache', () => {
|
|
|
41
37
|
setMultiInCache
|
|
42
38
|
};
|
|
43
39
|
};
|
|
44
|
-
beforeEach(() => {
|
|
45
|
-
|
|
40
|
+
(0, vitest_1.beforeEach)(() => {
|
|
41
|
+
vitest_1.vi.clearAllMocks();
|
|
46
42
|
});
|
|
47
|
-
it('should fetch and set value if skipCache is true', async () => {
|
|
43
|
+
(0, vitest_1.it)('should fetch and set value if skipCache is true', async () => {
|
|
48
44
|
const { cacheGet, cacheSet, fetching } = createMocks();
|
|
49
45
|
fetching.mockResolvedValue('fetchedValue');
|
|
50
46
|
await (0, cache_1.getWithCacheSupport)({ cacheKey, cacheGet, cacheSet, fetching, skipCache: true });
|
|
51
|
-
expect(fetching).toHaveBeenCalled();
|
|
52
|
-
expect(cacheSet).toHaveBeenCalledWith('fetchedValue');
|
|
47
|
+
(0, vitest_1.expect)(fetching).toHaveBeenCalled();
|
|
48
|
+
(0, vitest_1.expect)(cacheSet).toHaveBeenCalledWith('fetchedValue');
|
|
53
49
|
});
|
|
54
|
-
it('should fetch and set value if cache is empty', async () => {
|
|
50
|
+
(0, vitest_1.it)('should fetch and set value if cache is empty', async () => {
|
|
55
51
|
const { cacheGet, cacheSet, fetching } = createMocks();
|
|
56
52
|
cacheGet.mockResolvedValue(null);
|
|
57
53
|
fetching.mockResolvedValue('fetchedValue');
|
|
58
54
|
await (0, cache_1.getWithCacheSupport)({ cacheKey, cacheGet, cacheSet, fetching });
|
|
59
|
-
expect(fetching).toHaveBeenCalled();
|
|
60
|
-
expect(cacheSet).toHaveBeenCalledWith('fetchedValue');
|
|
55
|
+
(0, vitest_1.expect)(fetching).toHaveBeenCalled();
|
|
56
|
+
(0, vitest_1.expect)(cacheSet).toHaveBeenCalledWith('fetchedValue');
|
|
61
57
|
});
|
|
62
|
-
it('should return cached value if available', async () => {
|
|
58
|
+
(0, vitest_1.it)('should return cached value if available', async () => {
|
|
63
59
|
const { cacheGet, cacheSet, fetching } = createMocks();
|
|
64
60
|
cacheGet.mockResolvedValue('cachedValue');
|
|
65
61
|
const result = await (0, cache_1.getWithCacheSupport)({ cacheKey, cacheGet, cacheSet, fetching });
|
|
66
|
-
expect(result).toBe('cachedValue');
|
|
67
|
-
expect(fetching).not.toHaveBeenCalled();
|
|
62
|
+
(0, vitest_1.expect)(result).toBe('cachedValue');
|
|
63
|
+
(0, vitest_1.expect)(fetching).not.toHaveBeenCalled();
|
|
68
64
|
});
|
|
69
|
-
it('should retry fetching if an error occurs', async () => {
|
|
65
|
+
(0, vitest_1.it)('should retry fetching if an error occurs', async () => {
|
|
70
66
|
const { cacheGet, cacheSet, fetching } = createMocks();
|
|
71
67
|
cacheGet.mockRejectedValue(new Error('Cache error'));
|
|
72
68
|
fetching.mockResolvedValue('fetchedValue');
|
|
73
69
|
const result = await (0, cache_1.getWithCacheSupport)({ cacheKey, cacheGet, cacheSet, fetching });
|
|
74
|
-
expect(result).toBe('fetchedValue');
|
|
75
|
-
expect(fetching).toHaveBeenCalled();
|
|
76
|
-
expect(cacheSet).toHaveBeenCalledWith('fetchedValue');
|
|
70
|
+
(0, vitest_1.expect)(result).toBe('fetchedValue');
|
|
71
|
+
(0, vitest_1.expect)(fetching).toHaveBeenCalled();
|
|
72
|
+
(0, vitest_1.expect)(cacheSet).toHaveBeenCalledWith('fetchedValue');
|
|
77
73
|
});
|
|
78
|
-
it('should not throw an error if cacheSet fails', async () => {
|
|
74
|
+
(0, vitest_1.it)('should not throw an error if cacheSet fails', async () => {
|
|
79
75
|
const { cacheGet, cacheSet, fetching } = createMocks();
|
|
80
76
|
cacheGet.mockResolvedValue(null);
|
|
81
77
|
cacheSet.mockRejectedValue(new Error('Cache set error'));
|
|
82
78
|
fetching.mockResolvedValue('fetchedValue');
|
|
83
|
-
await expect((0, cache_1.getWithCacheSupport)({ cacheKey, cacheGet, cacheSet, fetching })).resolves.not.toThrow();
|
|
79
|
+
await (0, vitest_1.expect)((0, cache_1.getWithCacheSupport)({ cacheKey, cacheGet, cacheSet, fetching })).resolves.not.toThrow();
|
|
84
80
|
});
|
|
85
81
|
});
|
|
86
|
-
describe('getMultipleWithCache', () => {
|
|
82
|
+
(0, vitest_1.describe)('getMultipleWithCache', () => {
|
|
87
83
|
const buildMocks = () => {
|
|
88
|
-
const getFromCache =
|
|
89
|
-
const multiGetterFromCache =
|
|
90
|
-
const setInCache =
|
|
91
|
-
const setMultiInCache =
|
|
92
|
-
const getter =
|
|
93
|
-
const multiGetter =
|
|
84
|
+
const getFromCache = vitest_1.vi.fn();
|
|
85
|
+
const multiGetterFromCache = vitest_1.vi.fn();
|
|
86
|
+
const setInCache = vitest_1.vi.fn();
|
|
87
|
+
const setMultiInCache = vitest_1.vi.fn();
|
|
88
|
+
const getter = vitest_1.vi.fn();
|
|
89
|
+
const multiGetter = vitest_1.vi.fn();
|
|
94
90
|
return {
|
|
95
91
|
getFromCache,
|
|
96
92
|
multiGetterFromCache,
|
|
@@ -100,73 +96,73 @@ describe('Cache', () => {
|
|
|
100
96
|
multiGetter,
|
|
101
97
|
};
|
|
102
98
|
};
|
|
103
|
-
beforeEach(() => {
|
|
104
|
-
|
|
99
|
+
(0, vitest_1.beforeEach)(() => {
|
|
100
|
+
vitest_1.vi.clearAllMocks();
|
|
105
101
|
});
|
|
106
|
-
it('should return values from cache if available', async () => {
|
|
102
|
+
(0, vitest_1.it)('should return values from cache if available', async () => {
|
|
107
103
|
const { getFromCache, setInCache, getter } = buildMocks();
|
|
108
104
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
109
105
|
getFromCache.mockResolvedValueOnce({ id: 1, value: 'cachedValue1' }).mockResolvedValueOnce({ id: 2, value: 'cachedValue2' });
|
|
110
106
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ getFromCache, setInCache, getter });
|
|
111
107
|
const result = await getMultiple(queries);
|
|
112
|
-
expect(result.map(({ value }) => value)).toEqual(['cachedValue1', 'cachedValue2']);
|
|
108
|
+
(0, vitest_1.expect)(result.map(({ value }) => value)).toEqual(['cachedValue1', 'cachedValue2']);
|
|
113
109
|
});
|
|
114
|
-
it('should fetch and cache values if not in cache', async () => {
|
|
110
|
+
(0, vitest_1.it)('should fetch and cache values if not in cache', async () => {
|
|
115
111
|
const { getFromCache, setInCache, getter } = buildMocks();
|
|
116
112
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
117
113
|
getFromCache.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
|
|
118
114
|
getter.mockResolvedValueOnce({ id: 1, value: 'fetchedValue1' }).mockResolvedValueOnce({ id: 2, value: 'fetchedValue2' });
|
|
119
115
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ getFromCache, setInCache, getter });
|
|
120
116
|
const result = await getMultiple(queries);
|
|
121
|
-
expect(result.map(({ value }) => value)).toEqual(['fetchedValue1', 'fetchedValue2']);
|
|
122
|
-
expect(setInCache).toHaveBeenCalledWith(1, { id: 1, value: 'fetchedValue1' });
|
|
123
|
-
expect(setInCache).toHaveBeenCalledWith(2, { id: 2, value: 'fetchedValue2' });
|
|
117
|
+
(0, vitest_1.expect)(result.map(({ value }) => value)).toEqual(['fetchedValue1', 'fetchedValue2']);
|
|
118
|
+
(0, vitest_1.expect)(setInCache).toHaveBeenCalledWith(1, { id: 1, value: 'fetchedValue1' });
|
|
119
|
+
(0, vitest_1.expect)(setInCache).toHaveBeenCalledWith(2, { id: 2, value: 'fetchedValue2' });
|
|
124
120
|
});
|
|
125
|
-
it('should use multiGetterFromCache if provided', async () => {
|
|
121
|
+
(0, vitest_1.it)('should use multiGetterFromCache if provided', async () => {
|
|
126
122
|
const { multiGetterFromCache, setInCache, getter } = buildMocks();
|
|
127
123
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
128
124
|
multiGetterFromCache.mockResolvedValue([{ id: 1, value: 'cachedValue1' }, { id: 2, value: 'cachedValue2' }]);
|
|
129
125
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ multiGetterFromCache, setInCache, getter });
|
|
130
126
|
const result = await getMultiple(queries);
|
|
131
|
-
expect(result.map(({ value }) => value)).toEqual(['cachedValue1', 'cachedValue2']);
|
|
127
|
+
(0, vitest_1.expect)(result.map(({ value }) => value)).toEqual(['cachedValue1', 'cachedValue2']);
|
|
132
128
|
});
|
|
133
|
-
it('should use multiGetter if provided', async () => {
|
|
129
|
+
(0, vitest_1.it)('should use multiGetter if provided', async () => {
|
|
134
130
|
const { getFromCache, setInCache, multiGetter } = buildMocks();
|
|
135
131
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
136
132
|
getFromCache.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
|
|
137
133
|
multiGetter.mockResolvedValue([{ id: 1, value: 'fetchedValue1' }, { id: 2, value: 'fetchedValue2' }]);
|
|
138
134
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ getFromCache, setInCache, multiGetter });
|
|
139
135
|
const result = await getMultiple(queries);
|
|
140
|
-
expect(result.map(({ value }) => value)).toEqual(['fetchedValue1', 'fetchedValue2']);
|
|
141
|
-
expect(setInCache).toHaveBeenCalledWith(1, { id: 1, value: 'fetchedValue1' });
|
|
142
|
-
expect(setInCache).toHaveBeenCalledWith(2, { id: 2, value: 'fetchedValue2' });
|
|
136
|
+
(0, vitest_1.expect)(result.map(({ value }) => value)).toEqual(['fetchedValue1', 'fetchedValue2']);
|
|
137
|
+
(0, vitest_1.expect)(setInCache).toHaveBeenCalledWith(1, { id: 1, value: 'fetchedValue1' });
|
|
138
|
+
(0, vitest_1.expect)(setInCache).toHaveBeenCalledWith(2, { id: 2, value: 'fetchedValue2' });
|
|
143
139
|
});
|
|
144
|
-
it('should use setMultiInCache if provided', async () => {
|
|
140
|
+
(0, vitest_1.it)('should use setMultiInCache if provided', async () => {
|
|
145
141
|
const { getFromCache, setInCache, multiGetter, setMultiInCache } = buildMocks();
|
|
146
142
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
147
143
|
getFromCache.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
|
|
148
144
|
multiGetter.mockResolvedValue([{ id: 1, value: 'fetchedValue1' }, { id: 2, value: 'fetchedValue2' }]);
|
|
149
145
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ getFromCache, setInCache, multiGetter, setMultiInCache });
|
|
150
146
|
await getMultiple(queries);
|
|
151
|
-
expect(setMultiInCache).toHaveBeenCalledWith({ 1: { id: 1, value: 'fetchedValue1' }, 2: { id: 2, value: 'fetchedValue2' } });
|
|
147
|
+
(0, vitest_1.expect)(setMultiInCache).toHaveBeenCalledWith({ 1: { id: 1, value: 'fetchedValue1' }, 2: { id: 2, value: 'fetchedValue2' } });
|
|
152
148
|
});
|
|
153
|
-
it('Should not throw and error if setMultiInCache throws an error', async () => {
|
|
149
|
+
(0, vitest_1.it)('Should not throw and error if setMultiInCache throws an error', async () => {
|
|
154
150
|
const { getFromCache, setInCache, multiGetter, setMultiInCache } = buildMocks();
|
|
155
151
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
156
152
|
getFromCache.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
|
|
157
153
|
multiGetter.mockResolvedValue([{ id: 1, value: 'fetchedValue1' }, { id: 2, value: 'fetchedValue2' }]);
|
|
158
154
|
setMultiInCache.mockRejectedValue(new Error('Error setting cache'));
|
|
159
155
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ getFromCache, setInCache, multiGetter, setMultiInCache });
|
|
160
|
-
await expect(getMultiple(queries)).resolves.not.toThrow();
|
|
156
|
+
await (0, vitest_1.expect)(getMultiple(queries)).resolves.not.toThrow();
|
|
161
157
|
});
|
|
162
|
-
it('should not throw an error if setInCache fails', async () => {
|
|
158
|
+
(0, vitest_1.it)('should not throw an error if setInCache fails', async () => {
|
|
163
159
|
const { getFromCache, setInCache, getter } = buildMocks();
|
|
164
160
|
const queries = [{ id: 1 }, { id: 2 }];
|
|
165
161
|
getFromCache.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
|
|
166
162
|
getter.mockResolvedValueOnce({ id: 1, value: 'fetchedValue1' }).mockResolvedValueOnce({ id: 2, value: 'fetchedValue2' });
|
|
167
163
|
setInCache.mockRejectedValue(new Error('Error setting cache'));
|
|
168
164
|
const getMultiple = (0, cache_1.getMultipleWithCache)({ getFromCache, setInCache, getter });
|
|
169
|
-
await expect(getMultiple(queries)).resolves.not.toThrow();
|
|
165
|
+
await (0, vitest_1.expect)(getMultiple(queries)).resolves.not.toThrow();
|
|
170
166
|
});
|
|
171
167
|
});
|
|
172
168
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const promisifyAll: (obj: any) => void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promisifyAll = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
5
|
+
const promisifyAll = (obj) => {
|
|
6
|
+
Object.keys(obj).forEach((key) => {
|
|
7
|
+
if (typeof obj[key] === 'function') {
|
|
8
|
+
obj[`${key}Async`] = (0, util_1.promisify)(obj[key]);
|
|
9
|
+
// To support the `isPromisified` check in `bluebird`, we need to set the `__isPromisified__` property to `true`.
|
|
10
|
+
Object.defineProperty(obj[`${key}Async`], '__isPromisified__', {
|
|
11
|
+
value: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
enumerable: false,
|
|
14
|
+
writable: true
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
exports.promisifyAll = promisifyAll;
|
package/lib/redis/index.js
CHANGED
|
@@ -8,15 +8,9 @@ const redis_lock_1 = __importDefault(require("redis-lock"));
|
|
|
8
8
|
const util_1 = require("util");
|
|
9
9
|
const errors_1 = require("./errors");
|
|
10
10
|
const logger_1 = __importDefault(require("../logger"));
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
obj[`${key}Async`] = (0, util_1.promisify)(obj[key]);
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
promisifyAll(redis_1.default.RedisClient.prototype);
|
|
19
|
-
promisifyAll(redis_1.default.Multi.prototype);
|
|
11
|
+
const promise_utils_1 = require("../promise-utils");
|
|
12
|
+
(0, promise_utils_1.promisifyAll)(redis_1.default.RedisClient.prototype);
|
|
13
|
+
(0, promise_utils_1.promisifyAll)(redis_1.default.Multi.prototype);
|
|
20
14
|
const { env } = process;
|
|
21
15
|
const HOST = env.REDIS_HOST_NAME || '127.0.0.1';
|
|
22
16
|
const PORT = env.REDIS_HOST_PORT || 6379;
|
package/lib/redis/index.test.js
CHANGED
|
@@ -4,62 +4,59 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const redis_1 = __importDefault(require("redis"));
|
|
7
|
+
const vitest_1 = require("vitest");
|
|
7
8
|
const index_1 = __importDefault(require("./index"));
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (typeof obj[key] === 'function') {
|
|
12
|
-
obj[`${key}Async`] = (0, util_1.promisify)(obj[key]);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
promisifyAll(redis_1.default.RedisClient.prototype);
|
|
17
|
-
describe('RedisCache', () => {
|
|
9
|
+
const promise_utils_1 = require("../promise-utils");
|
|
10
|
+
(0, promise_utils_1.promisifyAll)(redis_1.default.RedisClient.prototype);
|
|
11
|
+
(0, vitest_1.describe)('RedisCache', () => {
|
|
18
12
|
let redisCache;
|
|
19
13
|
const testClient = redis_1.default.createClient();
|
|
20
|
-
beforeEach(() => {
|
|
14
|
+
(0, vitest_1.beforeEach)(() => {
|
|
21
15
|
redisCache = new index_1.default({});
|
|
22
16
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
describe('setMultiple', () => {
|
|
27
|
-
it('should set multiple key-value pairs in redis', async () => {
|
|
17
|
+
(0, vitest_1.describe)('setMultiple', () => {
|
|
18
|
+
(0, vitest_1.it)('should set multiple key-value pairs in redis', async () => {
|
|
28
19
|
const keyValues = { testKey1: 'testValue1', testKey2: 'testValue2' };
|
|
29
20
|
const keyValuesArray = Object.entries(keyValues);
|
|
30
21
|
await redisCache.setMultiple(keyValues);
|
|
31
22
|
const expectedKeyValues = keyValuesArray.map(([key, value]) => [`${redisCache.keyPrefix}${key}`, JSON.stringify(value)]);
|
|
32
23
|
await Promise.all(expectedKeyValues.map(async ([key, value]) => {
|
|
33
24
|
const valueInRedis = await testClient.getAsync(key);
|
|
34
|
-
expect(valueInRedis).toBe(value);
|
|
25
|
+
(0, vitest_1.expect)(valueInRedis).toBe(value);
|
|
35
26
|
}));
|
|
36
27
|
// Check that the keys expire after a random amount of time.
|
|
37
28
|
const currentTTL = await testClient.ttlAsync(expectedKeyValues[0][0]);
|
|
38
|
-
expect(currentTTL).toBeGreaterThanOrEqual(0);
|
|
29
|
+
(0, vitest_1.expect)(currentTTL).toBeGreaterThanOrEqual(0);
|
|
30
|
+
});
|
|
31
|
+
(0, vitest_1.it)('should not set any key-value pairs if keyValues is an empty object', async () => {
|
|
32
|
+
const currentKeys = await testClient.keysAsync(`${redisCache.keyPrefix}*`);
|
|
33
|
+
await redisCache.setMultiple({});
|
|
34
|
+
const keys = await testClient.keysAsync(`${redisCache.keyPrefix}*`);
|
|
35
|
+
(0, vitest_1.expect)(keys).toEqual(currentKeys);
|
|
39
36
|
});
|
|
40
37
|
});
|
|
41
|
-
describe('set', () => {
|
|
42
|
-
it('should set a key-value pair in redis', async () => {
|
|
38
|
+
(0, vitest_1.describe)('set', () => {
|
|
39
|
+
(0, vitest_1.it)('should set a key-value pair in redis', async () => {
|
|
43
40
|
const key = 'testKey';
|
|
44
41
|
const value = 'testValue';
|
|
45
42
|
await redisCache.set(key, value);
|
|
46
43
|
const keyWithPrefix = `${redisCache.keyPrefix}${key}`;
|
|
47
44
|
const valueInRedis = await testClient.getAsync(keyWithPrefix);
|
|
48
|
-
expect(valueInRedis).toBe(JSON.stringify(value));
|
|
45
|
+
(0, vitest_1.expect)(valueInRedis).toBe(JSON.stringify(value));
|
|
49
46
|
// Check that the key expires after a random amount of time.
|
|
50
47
|
const currentTTL = await testClient.ttlAsync(keyWithPrefix);
|
|
51
|
-
expect(currentTTL).toBeGreaterThanOrEqual(0);
|
|
48
|
+
(0, vitest_1.expect)(currentTTL).toBeGreaterThanOrEqual(0);
|
|
52
49
|
});
|
|
53
|
-
it('should release the lock after setting a key-value pair', async () => {
|
|
50
|
+
(0, vitest_1.it)('should release the lock after setting a key-value pair', async () => {
|
|
54
51
|
const key = 'testKeyWithLock';
|
|
55
52
|
const value = 'testValueWithLock';
|
|
56
53
|
redisCache.useLock = true;
|
|
57
54
|
await redisCache.set(key, value);
|
|
58
55
|
const keyWithPrefix = `${redisCache.keyPrefix}${key}`;
|
|
59
56
|
const valueInRedis = await testClient.getAsync(keyWithPrefix);
|
|
60
|
-
expect(valueInRedis).toBe(JSON.stringify(value));
|
|
57
|
+
(0, vitest_1.expect)(valueInRedis).toBe(JSON.stringify(value));
|
|
61
58
|
// Check that the lock is released
|
|
62
|
-
expect(redisCache.locks[keyWithPrefix]).toBeUndefined();
|
|
59
|
+
(0, vitest_1.expect)(redisCache.locks[keyWithPrefix]).toBeUndefined();
|
|
63
60
|
});
|
|
64
61
|
});
|
|
65
62
|
});
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/matmon",
|
|
3
|
-
"version": "2.3.1-beta-
|
|
3
|
+
"version": "2.3.1-beta-5",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rm -rf lib && tsc",
|
|
9
9
|
"prepublish": "npm run build",
|
|
10
|
-
"coverage": "
|
|
11
|
-
"test": "
|
|
12
|
-
"test-auto": "
|
|
10
|
+
"coverage": "vitest --coverage",
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test-auto": "vitest run --watch",
|
|
13
13
|
"linter": "./node_modules/.bin/eslint ."
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
"homepage": "https://github.com/Autofleet/matmon",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@autofleet/logger": "^4",
|
|
27
|
-
"@types/node": "^14.14.20",
|
|
28
27
|
"async-mutex": "^0.2.6",
|
|
29
28
|
"dotenv": "^8.2.0",
|
|
30
29
|
"lru-cache": "^6.0.0",
|
|
@@ -35,13 +34,11 @@
|
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
37
36
|
"@autofleet/logger": "^4.0.3",
|
|
38
|
-
"@types/jest": "^29.5.14",
|
|
39
37
|
"@types/lru-cache": "^5.1.1",
|
|
40
|
-
"@types/node": "^
|
|
38
|
+
"@types/node": "^20.17.10",
|
|
41
39
|
"@types/redis": "^4.0.11",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"typescript": "^5.5.2"
|
|
40
|
+
"typescript": "^5.5.2",
|
|
41
|
+
"vitest": "^2.1.8"
|
|
45
42
|
},
|
|
46
43
|
"peerDependencies": {
|
|
47
44
|
"@autofleet/logger": "^4"
|