-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyValueStore.test.js
64 lines (47 loc) · 1.98 KB
/
keyValueStore.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { expect } = require('chai');
const { CacheConfig } = require('../config/cacheConfigs');
const { FifoStrategy } = require('../service/fifoStrategy');
const { KeyValueStore } = require('../service/keyValueStore');
const { StoreService } = require('../service/storeService');
describe('store', () => {
let service = new KeyValueStore(new StoreService(), new FifoStrategy());
describe('cache: set data', () => {
beforeEach(() => {
CacheConfig.MAXIMUM_BYTES_SIZE = 1000;
service = new KeyValueStore(new StoreService(), new FifoStrategy());
})
it('should get the same data after set', async () => {
// arrange
const updateObject = { "key": "key1", "value": "val2" };
// action
await service.set(updateObject.key, updateObject.value);
const result = await service.get(updateObject.key);
// assert
expect(result).to.equal(updateObject.value);
})
it('should remove first item from cache when over maximum size', async () => {
// arrange
CacheConfig.MAXIMUM_BYTES_SIZE = 10;
const object1 = { "key": "key1", "value": "val2" };
const object2 = { "key": "key2", "value": "val2" };
// action
await service.set(object1.key, object1.value);
await service.set(object2.key, object2.value);
const result = await service.get(object1.key);
// assert
expect(result).to.equal(object1.value);
})
it('should get correct result even set data during multi-threads', async () => {
// arrange
const object1 = { "key": "999999", "value": "2" };
const object2 = { "key": "999999", "value": "5" };
let set1 = service.set(object1.key, object1.value);
const result1 = await service.get(object1.key);
expect(result1).to.equal(object1.value);
let set2 = service.set(object2.key, result1 * object2.value);
const result2 = await service.get(object1.key);
// assert
expect(result2).to.equal(object1.value * object2.value);
})
})
})