-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash-storage.test.js
184 lines (146 loc) · 4.29 KB
/
hash-storage.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict'
const test = require('node:test')
const assert = require('node:assert')
const { setTimeout: wait } = require('node:timers/promises')
const ClusteredStorage = require('.')
test('simple init/shutdown test', async () => {
assert.doesNotThrow(() => {
const storage = ClusteredStorage({
type: 'object',
TTL: 1000,
norm: 0,
concurrency: 4
})
storage.shutdown()
})
})
test('storage not wanishing data too early due to TTL', async () => {
const storage = ClusteredStorage({
type: 'object',
TTL: 1000,
norm: 0,
concurrency: 4
})
const data = { locker: true, user: '__admin__' }
await storage.hset('CRM#1', '1001', data)
const res1 = await storage.hget('CRM#1', '1001')
assert.deepEqual(res1, data)
await wait(500)
const res2 = await storage.hget('CRM#1', '1001')
storage.shutdown()
assert.deepEqual(res2, data)
})
test('not crashing on many insert & many retrieve operations', async () => {
const storage = ClusteredStorage({
type: 'object',
TTL: 1000,
norm: 1,
concurrency: 1
})
await assert.doesNotReject(async () => {
const promises1 = []
for (let i = 0; i < 10000; i++) {
const info = { locked: true, user: '__system__' }
promises1.push(storage.hset('CRM#1', i, info))
}
await Promise.all(promises1)
const promises2 = []
for (let i = 0; i < 10000; i++) {
promises2.push(storage.hget('CRM#1', i))
}
await Promise.all(promises2)
})
storage.shutdown()
})
test('should return null after deletion', async () => {
const storage = ClusteredStorage({
type: 'object',
TTL: 0,
norm: 0,
concurrency: 1
})
const data = { locker: true, user: '__admin__' }
await storage.hset('CRM#1', '1001', data)
const res1 = await storage.hget('CRM#1', '1001')
assert.deepEqual(res1, data)
await storage.hdel('CRM#1', '1001')
const res2 = await storage.hget('CRM#1', '1001')
storage.shutdown()
const missingValue = null
assert.equal(res2, missingValue)
})
test('separate vs same thread perf', async () => {
const workerStorage = ClusteredStorage({
type: 'object',
TTL: 1000,
norm: 1,
concurrency: 1
})
const noWorkerStorage = ClusteredStorage({
type: 'object',
TTL: 1000,
norm: 1,
concurrency: 0
})
const workerTime = { insert: Infinity, retrieve: Infinity }
const sameThreadTime = { insert: Infinity, retrieve: Infinity }
{
workerTime.insert = -process.hrtime.bigint()
const promises1 = []
for (let i = 0; i < 10000; i++) {
const info = { locked: true, user: '__system__' }
promises1.push(workerStorage.hset('CRM#1', i, info))
}
await Promise.all(promises1)
workerTime.insert += process.hrtime.bigint()
workerTime.retrieve = -process.hrtime.bigint()
const promises2 = []
for (let i = 0; i < 10000; i++) {
promises2.push(workerStorage.hget('CRM#1', i))
}
await Promise.all(promises2)
workerTime.retrieve += process.hrtime.bigint()
}
{
sameThreadTime.insert = -process.hrtime.bigint()
const promises1 = []
for (let i = 0; i < 10000; i++) {
const info = { locked: true, user: '__system__' }
promises1.push(noWorkerStorage.hset('CRM#1', i, info))
}
await Promise.all(promises1)
sameThreadTime.insert += process.hrtime.bigint()
sameThreadTime.retrieve = -process.hrtime.bigint()
const promises2 = []
for (let i = 0; i < 10000; i++) {
promises2.push(noWorkerStorage.hget('CRM#1', i))
}
await Promise.all(promises2)
sameThreadTime.retrieve += process.hrtime.bigint()
}
workerStorage.shutdown()
noWorkerStorage.shutdown()
assert.ok(
workerTime.insert > sameThreadTime.insert,
'Somehow separate thread inserts faster'
)
assert.ok(
workerTime.retrieve > sameThreadTime.retrieve,
'Somehow separate thread retrieves faster'
)
})
test('retrieving all data correctess', async () => {
const storage = ClusteredStorage({
type: 'object',
TTL: 0,
norm: 0,
concurrency: 1
})
const data = { locker: true, user: '__admin__' }
await storage.hset('CRM#1', '1001', data)
await storage.hset('CRM#1', '1002', data)
const allData = await storage.hgetall('CRM#1')
storage.shutdown()
const expected = { 1001: data, 1002: data }
assert.deepEqual(allData, expected)
})