0z2i6v3u5t 1.0.0

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.
Files changed (211) hide show
  1. package/.devcontainer/devcontainer.json +4 -0
  2. package/.devcontainer/setup.sh +11 -0
  3. package/.dockerignore +2 -0
  4. package/.github/CONTRIBUTING.md +52 -0
  5. package/.github/FUNDING.yml +3 -0
  6. package/.github/ISSUE_TEMPLATE/bug_report.yml +59 -0
  7. package/.github/ISSUE_TEMPLATE/config.yml +5 -0
  8. package/.github/ISSUE_TEMPLATE/feature_request.yml +43 -0
  9. package/.github/dependabot.yml +17 -0
  10. package/.github/workflows/codeql.yml +76 -0
  11. package/.github/workflows/publish_docs.yml +25 -0
  12. package/.github/workflows/test.yml +78 -0
  13. package/.nvmrc +1 -0
  14. package/.prettierignore +1 -0
  15. package/.prettierrc +1 -0
  16. package/.vscode/launch.json +42 -0
  17. package/CODE_OF_CONDUCT.md +76 -0
  18. package/Dockerfile +17 -0
  19. package/LICENSE +21 -0
  20. package/README.md +3 -0
  21. package/SECURITY.md +5 -0
  22. package/__tests__/actions/cacheTest.ts +58 -0
  23. package/__tests__/actions/randomNumber.ts +26 -0
  24. package/__tests__/actions/recursiveAction.ts +16 -0
  25. package/__tests__/actions/sleepTest.ts +24 -0
  26. package/__tests__/actions/status.ts +17 -0
  27. package/__tests__/actions/swagger.ts +76 -0
  28. package/__tests__/actions/validationTest.ts +63 -0
  29. package/__tests__/cli/cli.ts +126 -0
  30. package/__tests__/core/api.ts +632 -0
  31. package/__tests__/core/cache.ts +400 -0
  32. package/__tests__/core/chatRoom.ts +589 -0
  33. package/__tests__/core/cli.ts +349 -0
  34. package/__tests__/core/cluster.ts +132 -0
  35. package/__tests__/core/config.ts +78 -0
  36. package/__tests__/core/errors.ts +112 -0
  37. package/__tests__/core/log.ts +23 -0
  38. package/__tests__/core/middleware.ts +427 -0
  39. package/__tests__/core/plugins/partialPlugin.ts +94 -0
  40. package/__tests__/core/plugins/withPlugin.ts +88 -0
  41. package/__tests__/core/plugins/withoutPlugin.ts +81 -0
  42. package/__tests__/core/process.ts +42 -0
  43. package/__tests__/core/specHelper.ts +330 -0
  44. package/__tests__/core/staticFile/compression.ts +99 -0
  45. package/__tests__/core/staticFile/staticFile.ts +180 -0
  46. package/__tests__/core/tasks/customQueueFunction.ts +67 -0
  47. package/__tests__/core/tasks/fullWorkerFlow.ts +199 -0
  48. package/__tests__/core/tasks/tasks.ts +605 -0
  49. package/__tests__/integration/browser.ts +133 -0
  50. package/__tests__/integration/ioredis-mock.ts +194 -0
  51. package/__tests__/integration/sendBuffer.ts +97 -0
  52. package/__tests__/integration/sendFile.ts +24 -0
  53. package/__tests__/integration/sharedFingerprint.ts +82 -0
  54. package/__tests__/integration/taskFlow.ts +110 -0
  55. package/__tests__/jest.ts +5 -0
  56. package/__tests__/modules/action.ts +103 -0
  57. package/__tests__/modules/config.ts +19 -0
  58. package/__tests__/modules/utils/ensureNoTsHeaderOrSpecFiles.ts +24 -0
  59. package/__tests__/servers/web/allowedRequestHosts.ts +88 -0
  60. package/__tests__/servers/web/enableMultiples.ts +83 -0
  61. package/__tests__/servers/web/fileUpload.ts +79 -0
  62. package/__tests__/servers/web/jsonp.ts +57 -0
  63. package/__tests__/servers/web/nonMultiples.ts +83 -0
  64. package/__tests__/servers/web/rawBody.ts +208 -0
  65. package/__tests__/servers/web/returnErrorCodes.ts +55 -0
  66. package/__tests__/servers/web/routes/deepRoutes.ts +96 -0
  67. package/__tests__/servers/web/routes/routes.ts +579 -0
  68. package/__tests__/servers/web/routes/veryDeepRoutes.ts +92 -0
  69. package/__tests__/servers/web/web.ts +1031 -0
  70. package/__tests__/servers/websocket.ts +795 -0
  71. package/__tests__/tasks/runAction.ts +37 -0
  72. package/__tests__/template.ts.example +20 -0
  73. package/__tests__/testCliCommands/hello.ts +44 -0
  74. package/__tests__/testPlugin/public/plugin.html +1 -0
  75. package/__tests__/testPlugin/src/actions/pluginAction.ts +14 -0
  76. package/__tests__/testPlugin/src/bin/hello.ts +22 -0
  77. package/__tests__/testPlugin/src/initializers/pluginInitializer.ts +17 -0
  78. package/__tests__/testPlugin/src/tasks/pluginTask.ts +15 -0
  79. package/__tests__/testPlugin/tsconfig.json +10 -0
  80. package/__tests__/utils/utils.ts +492 -0
  81. package/app.json +23 -0
  82. package/bin/deploy-docs +39 -0
  83. package/client/ActionheroWebsocketClient.js +277 -0
  84. package/docker-compose.yml +73 -0
  85. package/package.json +24 -0
  86. package/public/chat.html +194 -0
  87. package/public/css/cosmo.css +12 -0
  88. package/public/favicon.ico +0 -0
  89. package/public/index.html +115 -0
  90. package/public/javascript/.gitkeep +0 -0
  91. package/public/linkedSession.html +80 -0
  92. package/public/logo/actionhero-small.png +0 -0
  93. package/public/logo/actionhero.png +0 -0
  94. package/public/pixel.gif +0 -0
  95. package/public/simple.html +2 -0
  96. package/public/swagger.html +32 -0
  97. package/public/websocketLoadTest.html +322 -0
  98. package/src/actions/cacheTest.ts +58 -0
  99. package/src/actions/createChatRoom.ts +20 -0
  100. package/src/actions/randomNumber.ts +17 -0
  101. package/src/actions/recursiveAction.ts +13 -0
  102. package/src/actions/sendFile.ts +12 -0
  103. package/src/actions/sleepTest.ts +40 -0
  104. package/src/actions/status.ts +73 -0
  105. package/src/actions/swagger.ts +155 -0
  106. package/src/actions/validationTest.ts +36 -0
  107. package/src/bin/actionhero.ts +225 -0
  108. package/src/bin/methods/actions/list.ts +30 -0
  109. package/src/bin/methods/console.ts +26 -0
  110. package/src/bin/methods/generate/action.ts +58 -0
  111. package/src/bin/methods/generate/cli.ts +51 -0
  112. package/src/bin/methods/generate/initializer.ts +54 -0
  113. package/src/bin/methods/generate/plugin.ts +57 -0
  114. package/src/bin/methods/generate/server.ts +38 -0
  115. package/src/bin/methods/generate/task.ts +68 -0
  116. package/src/bin/methods/generate.ts +176 -0
  117. package/src/bin/methods/task/enqueue.ts +35 -0
  118. package/src/classes/action.ts +98 -0
  119. package/src/classes/actionProcessor.ts +463 -0
  120. package/src/classes/api.ts +51 -0
  121. package/src/classes/cli.ts +67 -0
  122. package/src/classes/config.ts +15 -0
  123. package/src/classes/connection.ts +321 -0
  124. package/src/classes/exceptionReporter.ts +9 -0
  125. package/src/classes/initializer.ts +59 -0
  126. package/src/classes/initializers.ts +5 -0
  127. package/src/classes/input.ts +9 -0
  128. package/src/classes/inputs.ts +34 -0
  129. package/src/classes/process/actionheroVersion.ts +15 -0
  130. package/src/classes/process/env.ts +16 -0
  131. package/src/classes/process/id.ts +34 -0
  132. package/src/classes/process/pid.ts +32 -0
  133. package/src/classes/process/projectRoot.ts +16 -0
  134. package/src/classes/process/typescript.ts +47 -0
  135. package/src/classes/process.ts +479 -0
  136. package/src/classes/server.ts +251 -0
  137. package/src/classes/task.ts +87 -0
  138. package/src/config/api.ts +107 -0
  139. package/src/config/errors.ts +162 -0
  140. package/src/config/logger.ts +113 -0
  141. package/src/config/plugins.ts +37 -0
  142. package/src/config/redis.ts +78 -0
  143. package/src/config/routes.ts +44 -0
  144. package/src/config/tasks.ts +84 -0
  145. package/src/config/web.ts +136 -0
  146. package/src/config/websocket.ts +62 -0
  147. package/src/index.ts +46 -0
  148. package/src/initializers/actions.ts +125 -0
  149. package/src/initializers/chatRoom.ts +214 -0
  150. package/src/initializers/connections.ts +124 -0
  151. package/src/initializers/exceptions.ts +155 -0
  152. package/src/initializers/params.ts +52 -0
  153. package/src/initializers/redis.ts +191 -0
  154. package/src/initializers/resque.ts +248 -0
  155. package/src/initializers/routes.ts +229 -0
  156. package/src/initializers/servers.ts +134 -0
  157. package/src/initializers/specHelper.ts +195 -0
  158. package/src/initializers/staticFile.ts +253 -0
  159. package/src/initializers/tasks.ts +188 -0
  160. package/src/modules/action.ts +89 -0
  161. package/src/modules/cache.ts +326 -0
  162. package/src/modules/chatRoom.ts +321 -0
  163. package/src/modules/config.ts +246 -0
  164. package/src/modules/log.ts +62 -0
  165. package/src/modules/redis.ts +93 -0
  166. package/src/modules/route.ts +59 -0
  167. package/src/modules/specHelper.ts +182 -0
  168. package/src/modules/task.ts +527 -0
  169. package/src/modules/utils/argv.ts +3 -0
  170. package/src/modules/utils/arrayStartingMatch.ts +21 -0
  171. package/src/modules/utils/arrayUnique.ts +15 -0
  172. package/src/modules/utils/collapseObjectToArray.ts +33 -0
  173. package/src/modules/utils/deepCopy.ts +3 -0
  174. package/src/modules/utils/ensureNoTsHeaderOrSpecFiles.ts +19 -0
  175. package/src/modules/utils/eventLoopDelay.ts +34 -0
  176. package/src/modules/utils/fileUtils.ts +119 -0
  177. package/src/modules/utils/filterObjectForLogging.ts +51 -0
  178. package/src/modules/utils/filterResponseForLogging.ts +53 -0
  179. package/src/modules/utils/getExternalIPAddress.ts +17 -0
  180. package/src/modules/utils/hashMerge.ts +63 -0
  181. package/src/modules/utils/isPlainObject.ts +45 -0
  182. package/src/modules/utils/isRunning.ts +7 -0
  183. package/src/modules/utils/parseCookies.ts +20 -0
  184. package/src/modules/utils/parseHeadersForClientAddress.ts +53 -0
  185. package/src/modules/utils/parseIPv6URI.ts +24 -0
  186. package/src/modules/utils/replaceDistWithSrc.ts +9 -0
  187. package/src/modules/utils/safeGlob.ts +6 -0
  188. package/src/modules/utils/sleep.ts +8 -0
  189. package/src/modules/utils/sortGlobalMiddleware.ts +17 -0
  190. package/src/modules/utils/sourceRelativeLinkPath.ts +29 -0
  191. package/src/modules/utils.ts +66 -0
  192. package/src/server.ts +20 -0
  193. package/src/servers/web.ts +894 -0
  194. package/src/servers/websocket.ts +304 -0
  195. package/src/tasks/runAction.ts +29 -0
  196. package/tea.yaml +9 -0
  197. package/templates/README.md.template +17 -0
  198. package/templates/action.ts.template +15 -0
  199. package/templates/boot.js.template +9 -0
  200. package/templates/cli.ts.template +15 -0
  201. package/templates/gitignore.template +23 -0
  202. package/templates/initializer.ts.template +17 -0
  203. package/templates/package-plugin.json.template +12 -0
  204. package/templates/package.json.template +45 -0
  205. package/templates/projectMap.txt +39 -0
  206. package/templates/projectServer.ts.template +20 -0
  207. package/templates/server.ts.template +37 -0
  208. package/templates/task.ts.template +16 -0
  209. package/templates/test/action.ts.template +13 -0
  210. package/templates/test/task.ts.template +20 -0
  211. package/tsconfig.json +11 -0
@@ -0,0 +1,400 @@
1
+ import * as path from "path";
2
+ import * as fs from "fs";
3
+ import * as os from "os";
4
+ import { api, Process, cache, utils, id } from "./../../src";
5
+
6
+ const actionhero = new Process();
7
+
8
+ describe("Core", () => {
9
+ describe("cache", () => {
10
+ beforeAll(async () => {
11
+ await actionhero.start();
12
+ });
13
+ afterAll(async () => {
14
+ await actionhero.stop();
15
+ });
16
+
17
+ beforeAll(async () => {
18
+ await api.redis.clients.client.flushdb();
19
+ });
20
+
21
+ test("cache methods should exist", () => {
22
+ expect(cache.save).toBeInstanceOf(Function);
23
+ expect(cache.load).toBeInstanceOf(Function);
24
+ expect(cache.destroy).toBeInstanceOf(Function);
25
+ });
26
+
27
+ test("cache.save", async () => {
28
+ const resp = await cache.save("testKey", "abc123");
29
+ expect(resp).toEqual(true);
30
+ });
31
+
32
+ test("cache.load", async () => {
33
+ await cache.save("testKey", "abc123");
34
+ const { value } = await cache.load("testKey");
35
+ expect(value).toEqual("abc123");
36
+ });
37
+
38
+ test("cache.getKeys", async () => {
39
+ await cache.client().set("act:other:namespace:k", 2);
40
+ const keys = await cache.getKeys("act*");
41
+ expect(keys.sort()).toEqual([
42
+ "act:other:namespace:k",
43
+ "actionhero:cache:testKey",
44
+ ]);
45
+ });
46
+
47
+ test("cache.keys (no prefix)", async () => {
48
+ await cache.save("otherKey", "abc123");
49
+ const keys = await cache.keys();
50
+ expect(keys.sort()).toEqual([
51
+ "actionhero:cache:otherKey",
52
+ "actionhero:cache:testKey",
53
+ ]);
54
+
55
+ await cache.client().del("actionhero:cache:otherKey");
56
+ });
57
+
58
+ test("cache.keys (with prefix)", async () => {
59
+ await cache.save("otherKey", "abc123");
60
+ const keys = await cache.keys("test");
61
+ expect(keys.sort()).toEqual(["actionhero:cache:testKey"]);
62
+
63
+ await cache.client().del("actionhero:cache:otherKey");
64
+ });
65
+
66
+ test("cache.load failures", async () => {
67
+ try {
68
+ await cache.load("something else");
69
+ throw new Error("should not get here");
70
+ } catch (error) {
71
+ expect(String(error)).toEqual("Error: Object not found");
72
+ }
73
+ });
74
+
75
+ test("cache.destroy", async () => {
76
+ const resp = await cache.destroy("testKey");
77
+ expect(resp).toEqual(true);
78
+ });
79
+
80
+ test("cache.destroy failure", async () => {
81
+ const resp = await cache.destroy("testKey");
82
+ expect(resp).toEqual(false);
83
+ });
84
+
85
+ test("cache.save with expire time", async () => {
86
+ const resp = await cache.save("testKey", "abc123", 10);
87
+ expect(resp).toEqual(true);
88
+ });
89
+
90
+ test("cache.load with expired items should not return them", async () => {
91
+ const saveResp = await cache.save("testKey_slow", "abc123", 10);
92
+ expect(saveResp).toEqual(true);
93
+ await utils.sleep(20);
94
+ try {
95
+ await cache.load("testKey_slow");
96
+ throw new Error("should not get here");
97
+ } catch (error) {
98
+ expect(String(error)).toEqual("Error: Object not found");
99
+ }
100
+ });
101
+
102
+ test("cache.load with negative expire times will never load", async () => {
103
+ const saveResp = await cache.save("testKeyInThePast", "abc123", -1);
104
+ expect(saveResp).toEqual(true);
105
+ try {
106
+ await cache.load("testKeyInThePast");
107
+ throw new Error("should not get here");
108
+ } catch (error) {
109
+ expect(String(error)).toMatch(/Error: Object/);
110
+ }
111
+ });
112
+
113
+ test("cache.save does not need to pass expireTime", async () => {
114
+ const saveResp = await cache.save("testKeyForNullExpireTime", "abc123");
115
+ expect(saveResp).toEqual(true);
116
+ const { value } = await cache.load("testKeyForNullExpireTime");
117
+ expect(value).toEqual("abc123");
118
+ });
119
+
120
+ test("cache.load without changing the expireTime will re-apply the redis expire", async () => {
121
+ const key = "testKey";
122
+ await cache.save(key, "val", 1000);
123
+ const loadResp = await cache.load(key);
124
+ expect(loadResp.value).toEqual("val");
125
+ await utils.sleep(1001);
126
+ try {
127
+ await cache.load(key);
128
+ throw new Error("should not get here");
129
+ } catch (error) {
130
+ expect(String(error)).toMatch(/Error: Object not found/);
131
+ }
132
+ });
133
+
134
+ test("cache.load with options that extending expireTime should return cached item", async () => {
135
+ const expireTime = 400;
136
+ const timeout = 200;
137
+
138
+ // save the initial key
139
+ const saveResp = await cache.save("testKey_slow", "abc123", expireTime);
140
+ expect(saveResp).toEqual(true);
141
+
142
+ // wait for `timeout` and try to load the key
143
+ await utils.sleep(timeout);
144
+
145
+ let loadResp = await cache.load("testKey_slow", {
146
+ expireTimeMS: expireTime,
147
+ });
148
+ expect(loadResp.value).toEqual("abc123");
149
+
150
+ // wait another `timeout` and load the key again within the extended expire time
151
+ await utils.sleep(timeout);
152
+
153
+ loadResp = await cache.load("testKey_slow");
154
+ expect(loadResp.value).toEqual("abc123");
155
+
156
+ // wait another `timeout` and the key load should fail without the extension
157
+ await utils.sleep(timeout);
158
+
159
+ try {
160
+ loadResp = await cache.load("testKey_slow");
161
+ throw new Error("should not get here");
162
+ } catch (error) {
163
+ expect(String(error)).toEqual("Error: Object not found");
164
+ }
165
+ });
166
+
167
+ test("cache.save works with arrays", async () => {
168
+ const saveResp = await cache.save("array_key", [1, 2, 3]);
169
+ expect(saveResp).toEqual(true);
170
+ const { value } = await cache.load("array_key");
171
+ expect(value[0]).toEqual(1);
172
+ expect(value[1]).toEqual(2);
173
+ expect(value[2]).toEqual(3);
174
+ });
175
+
176
+ test("cache.save works with objects", async () => {
177
+ const data: {
178
+ [key: string]: any;
179
+ } = {};
180
+
181
+ data.thing = "stuff";
182
+ data.otherThing = [1, 2, 3];
183
+ const saveResp = await cache.save("obj_key", data);
184
+ expect(saveResp).toEqual(true);
185
+ const { value } = await cache.load("obj_key");
186
+ expect(value.thing).toEqual("stuff");
187
+ expect(value.otherThing[0]).toEqual(1);
188
+ expect(value.otherThing[1]).toEqual(2);
189
+ expect(value.otherThing[2]).toEqual(3);
190
+ });
191
+
192
+ test("can read the cache size", async () => {
193
+ await cache.save("thingA", {});
194
+ const count = await cache.size();
195
+ expect(count > 0).toEqual(true);
196
+ });
197
+
198
+ test("can clear the cache entirely", async () => {
199
+ await cache.save("thingA", {});
200
+ let count = await cache.size();
201
+ expect(count > 0).toEqual(true);
202
+ await cache.clear();
203
+ count = await cache.size();
204
+ expect(count).toEqual(0);
205
+ });
206
+
207
+ describe("lists", () => {
208
+ test("can push and pop from an array", async () => {
209
+ await cache.push("testListKey", "a string");
210
+ await cache.push("testListKey", ["an array"]);
211
+ await cache.push("testListKey", { what: "an object" });
212
+
213
+ let data;
214
+ data = await cache.pop("testListKey");
215
+ expect(data).toEqual("a string");
216
+ data = await cache.pop("testListKey");
217
+ expect(data).toEqual(["an array"]);
218
+ data = await cache.pop("testListKey");
219
+ expect(data).toEqual({ what: "an object" });
220
+ data = await cache.pop("testListKey");
221
+ expect(data).toBeNull();
222
+ });
223
+
224
+ test("will return undefined if the list is empty", async () => {
225
+ const data = await cache.pop("emptyListKey");
226
+ expect(data).toBeNull();
227
+ });
228
+
229
+ test("can get the length of an array when full", async () => {
230
+ await cache.push("testListKey2", "a string");
231
+ const length = await cache.listLength("testListKey2");
232
+ expect(length).toEqual(1);
233
+ });
234
+
235
+ test("will return 0 length when the key does not exist", async () => {
236
+ const length = await cache.listLength("testListKey3");
237
+ expect(length).toEqual(0);
238
+ });
239
+ });
240
+
241
+ describe("locks", () => {
242
+ const key = "testKey";
243
+
244
+ afterEach(async () => {
245
+ cache.overrideLockName(id);
246
+ await cache.unlock(key);
247
+ });
248
+
249
+ test("things can be locked, checked, and unlocked arbitrarily", async () => {
250
+ let lockOk;
251
+ lockOk = await cache.lock(key, 100);
252
+ expect(lockOk).toEqual(true);
253
+ lockOk = await cache.checkLock(key);
254
+ expect(lockOk).toEqual(true);
255
+ lockOk = await cache.unlock(key);
256
+ expect(lockOk).toEqual(true);
257
+ lockOk = await cache.checkLock(key);
258
+ expect(lockOk).toEqual(true);
259
+ });
260
+
261
+ test("cache.locks", async () => {
262
+ let locks = await cache.locks();
263
+ expect(locks).toEqual([]);
264
+
265
+ await cache.lock(key, 100);
266
+ locks = await cache.locks();
267
+ expect(locks).toEqual(["actionhero:lock:testKey"]);
268
+
269
+ await cache.unlock(key);
270
+ locks = await cache.locks();
271
+ expect(locks).toEqual([]);
272
+ });
273
+
274
+ test("locks have a TTL and the default will be assumed from config", async () => {
275
+ const lockOk = await cache.lock(key, undefined);
276
+ expect(lockOk).toEqual(true);
277
+ const ttl = await api.redis.clients.client.ttl(cache.lockPrefix + key);
278
+ expect(ttl).toBeGreaterThanOrEqual(9);
279
+ expect(ttl).toBeLessThanOrEqual(10);
280
+ });
281
+
282
+ test("you can save an item if you do hold the lock", async () => {
283
+ const lockOk = await cache.lock(key);
284
+ expect(lockOk).toEqual(true);
285
+ const success = await cache.save(key, "value");
286
+ expect(success).toEqual(true);
287
+ });
288
+
289
+ test("you cannot save a locked item if you do not hold the lock", async () => {
290
+ const lockOk = await cache.lock(key);
291
+ expect(lockOk).toEqual(true);
292
+ // cache.lockName = "otherId";
293
+ cache.overrideLockName("otherId");
294
+ try {
295
+ await cache.save(key, "value");
296
+ throw new Error("should not get here");
297
+ } catch (error) {
298
+ expect(String(error)).toEqual("Error: Object locked");
299
+ }
300
+ });
301
+
302
+ test("you cannot destroy a locked item if you do not hold the lock", async () => {
303
+ const lockOk = await cache.lock(key);
304
+ expect(lockOk).toEqual(true);
305
+ cache.overrideLockName("otherId");
306
+ try {
307
+ await cache.destroy(key);
308
+ throw new Error("should not get here");
309
+ } catch (error) {
310
+ expect(String(error)).toEqual("Error: Object locked");
311
+ }
312
+ });
313
+
314
+ test("you can opt to retry to obtain a lock if a lock is held (READ)", async () => {
315
+ const success = await cache.save(key, "value");
316
+ expect(success).toEqual(true);
317
+ let lockOk = await cache.lock(key, 1); // will be rounded up to 1s
318
+ expect(lockOk).toEqual(true);
319
+
320
+ cache.overrideLockName("otherId");
321
+ lockOk = await cache.checkLock(key);
322
+ expect(lockOk).toEqual(false);
323
+
324
+ const start = new Date().getTime();
325
+ const { value } = await cache.load(key, { retry: 2000 });
326
+ expect(value).toEqual("value");
327
+ const delta = new Date().getTime() - start;
328
+ expect(delta >= 1000).toEqual(true);
329
+ });
330
+
331
+ describe("locks are actually blocking", () => {
332
+ let originalLockName: () => string;
333
+
334
+ beforeAll(() => {
335
+ originalLockName = cache.lockName;
336
+ });
337
+ afterAll(() => {
338
+ cache.lockName = originalLockName;
339
+ });
340
+
341
+ test("locks are actually blocking", async () => {
342
+ const key = "test";
343
+ let lockOk;
344
+
345
+ cache.overrideLockName(`test-name-pass-${1}`);
346
+ lockOk = await cache.checkLock(key);
347
+ expect(lockOk).toEqual(true);
348
+ await cache.lock(key, 1000 * 60);
349
+
350
+ cache.overrideLockName(`test-name-pass-${2}`);
351
+ lockOk = await cache.checkLock(key);
352
+ expect(lockOk).toEqual(false);
353
+
354
+ cache.overrideLockName(`test-name-pass-${3}`);
355
+ lockOk = await cache.checkLock(key);
356
+ expect(lockOk).toEqual(false);
357
+ });
358
+
359
+ test("locks are actually blocking (using setnx value)", async () => {
360
+ const key = "test-setnx";
361
+ let lockOk;
362
+
363
+ cache.overrideLockName(`test-setnx-name-pass-${1}`);
364
+ lockOk = await cache.lock(key, 1000 * 60);
365
+ expect(lockOk).toEqual(true);
366
+
367
+ cache.overrideLockName(`test-setnx-name-pass-${2}`);
368
+ lockOk = await cache.lock(key, 1000 * 60);
369
+ expect(lockOk).toEqual(false);
370
+
371
+ cache.overrideLockName(`test-setnx-name-pass-${3}`);
372
+ lockOk = await cache.lock(key, 1000 * 60);
373
+ expect(lockOk).toEqual(false);
374
+ });
375
+ });
376
+ });
377
+
378
+ describe("cache dump files", () => {
379
+ const file = os.tmpdir() + path.sep + "cacheDump";
380
+
381
+ test("can read write the cache to a dump file", async () => {
382
+ await cache.clear();
383
+ await cache.save("thingA", 123);
384
+ const count = await cache.dumpWrite(file);
385
+ expect(count).toEqual(1);
386
+ const body = JSON.parse(String(fs.readFileSync(file)));
387
+ const content = JSON.parse(body["actionhero:cache:thingA"]);
388
+ expect(content.value).toEqual(123);
389
+ });
390
+
391
+ test("can load the cache from a dump file", async () => {
392
+ await cache.clear();
393
+ const count = await cache.dumpRead(file);
394
+ expect(count).toEqual(1);
395
+ const { value } = await cache.load("thingA");
396
+ expect(value).toEqual(123);
397
+ });
398
+ });
399
+ });
400
+ });