| 1 |
import gensync from "gensync"; |
| 2 |
import { makeStrongCacheSync, makeStrongCache } from "../lib/config/caching.js"; |
| 3 |
import { waitFor } from "../lib/gensync-utils/async.js"; |
| 4 |
|
| 5 |
describe("caching API", () => { |
| 6 |
it("should allow permacaching with .forever()", () => { |
| 7 |
let count = 0; |
| 8 |
|
| 9 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 10 |
cache.forever(); |
| 11 |
return { arg, count: count++ }; |
| 12 |
}); |
| 13 |
|
| 14 |
expect(fn("one")).toEqual({ arg: "one", count: 0 }); |
| 15 |
expect(fn("one")).toBe(fn("one")); |
| 16 |
|
| 17 |
expect(fn("two")).toEqual({ arg: "two", count: 1 }); |
| 18 |
expect(fn("two")).toBe(fn("two")); |
| 19 |
|
| 20 |
expect(fn("one")).not.toEqual(fn("two")); |
| 21 |
}); |
| 22 |
|
| 23 |
it("should allow disabling caching with .never()", () => { |
| 24 |
let count = 0; |
| 25 |
|
| 26 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 27 |
cache.never(); |
| 28 |
return { arg, count: count++ }; |
| 29 |
}); |
| 30 |
|
| 31 |
expect(fn("one")).toEqual({ arg: "one", count: 0 }); |
| 32 |
expect(fn("one")).toEqual({ arg: "one", count: 1 }); |
| 33 |
expect(fn("one")).not.toEqual(fn("one")); |
| 34 |
|
| 35 |
expect(fn("two")).toEqual({ arg: "two", count: 4 }); |
| 36 |
expect(fn("two")).toEqual({ arg: "two", count: 5 }); |
| 37 |
expect(fn("two")).not.toEqual(fn("two")); |
| 38 |
|
| 39 |
expect(fn("one")).not.toEqual(fn("two")); |
| 40 |
}); |
| 41 |
|
| 42 |
it("should allow caching based on a value with .using(fn)", () => { |
| 43 |
let count = 0; |
| 44 |
let other = "default"; |
| 45 |
|
| 46 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 47 |
const val = cache.using(() => other); |
| 48 |
|
| 49 |
return { arg, val, count: count++ }; |
| 50 |
}); |
| 51 |
|
| 52 |
expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 }); |
| 53 |
expect(fn("one")).toBe(fn("one")); |
| 54 |
|
| 55 |
expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 }); |
| 56 |
expect(fn("two")).toBe(fn("two")); |
| 57 |
|
| 58 |
other = "new"; |
| 59 |
|
| 60 |
expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 }); |
| 61 |
expect(fn("one")).toBe(fn("one")); |
| 62 |
|
| 63 |
expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 }); |
| 64 |
expect(fn("two")).toBe(fn("two")); |
| 65 |
|
| 66 |
other = "default"; |
| 67 |
|
| 68 |
expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 }); |
| 69 |
expect(fn("one")).toBe(fn("one")); |
| 70 |
|
| 71 |
expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 }); |
| 72 |
expect(fn("two")).toBe(fn("two")); |
| 73 |
|
| 74 |
other = "new"; |
| 75 |
|
| 76 |
expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 }); |
| 77 |
expect(fn("one")).toBe(fn("one")); |
| 78 |
|
| 79 |
expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 }); |
| 80 |
expect(fn("two")).toBe(fn("two")); |
| 81 |
}); |
| 82 |
|
| 83 |
it("should allow invalidation based on a value with .invalidate(fn)", () => { |
| 84 |
let count = 0; |
| 85 |
let other = "default"; |
| 86 |
|
| 87 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 88 |
const val = cache.invalidate(() => other); |
| 89 |
|
| 90 |
return { arg, val, count: count++ }; |
| 91 |
}); |
| 92 |
|
| 93 |
expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 }); |
| 94 |
expect(fn("one")).toBe(fn("one")); |
| 95 |
|
| 96 |
expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 }); |
| 97 |
expect(fn("two")).toBe(fn("two")); |
| 98 |
|
| 99 |
other = "new"; |
| 100 |
|
| 101 |
expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 }); |
| 102 |
expect(fn("one")).toBe(fn("one")); |
| 103 |
|
| 104 |
expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 }); |
| 105 |
expect(fn("two")).toBe(fn("two")); |
| 106 |
|
| 107 |
other = "default"; |
| 108 |
|
| 109 |
expect(fn("one")).toEqual({ arg: "one", val: "default", count: 4 }); |
| 110 |
expect(fn("one")).toBe(fn("one")); |
| 111 |
|
| 112 |
expect(fn("two")).toEqual({ arg: "two", val: "default", count: 5 }); |
| 113 |
expect(fn("two")).toBe(fn("two")); |
| 114 |
|
| 115 |
other = "new"; |
| 116 |
|
| 117 |
expect(fn("one")).toEqual({ arg: "one", val: "new", count: 6 }); |
| 118 |
expect(fn("one")).toBe(fn("one")); |
| 119 |
|
| 120 |
expect(fn("two")).toEqual({ arg: "two", val: "new", count: 7 }); |
| 121 |
expect(fn("two")).toBe(fn("two")); |
| 122 |
}); |
| 123 |
|
| 124 |
it("should allow invalidation with .using and .invalidate", () => { |
| 125 |
let count = 0; |
| 126 |
let other = "default"; |
| 127 |
let another = "another"; |
| 128 |
|
| 129 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 130 |
const val = cache.using(() => other); |
| 131 |
const val2 = cache.invalidate(() => another); |
| 132 |
|
| 133 |
return { arg, val, val2, count: count++ }; |
| 134 |
}); |
| 135 |
|
| 136 |
expect(fn("one")).toEqual({ |
| 137 |
arg: "one", |
| 138 |
val: "default", |
| 139 |
val2: "another", |
| 140 |
count: 0, |
| 141 |
}); |
| 142 |
expect(fn("one")).toBe(fn("one")); |
| 143 |
|
| 144 |
expect(fn("two")).toEqual({ |
| 145 |
arg: "two", |
| 146 |
val: "default", |
| 147 |
val2: "another", |
| 148 |
count: 1, |
| 149 |
}); |
| 150 |
expect(fn("two")).toBe(fn("two")); |
| 151 |
|
| 152 |
other = "new"; |
| 153 |
|
| 154 |
expect(fn("one")).toEqual({ |
| 155 |
arg: "one", |
| 156 |
val: "new", |
| 157 |
val2: "another", |
| 158 |
count: 2, |
| 159 |
}); |
| 160 |
expect(fn("one")).toBe(fn("one")); |
| 161 |
|
| 162 |
expect(fn("two")).toEqual({ |
| 163 |
arg: "two", |
| 164 |
val: "new", |
| 165 |
val2: "another", |
| 166 |
count: 3, |
| 167 |
}); |
| 168 |
expect(fn("two")).toBe(fn("two")); |
| 169 |
|
| 170 |
other = "default"; |
| 171 |
|
| 172 |
expect(fn("one")).toEqual({ |
| 173 |
arg: "one", |
| 174 |
val: "default", |
| 175 |
val2: "another", |
| 176 |
count: 4, |
| 177 |
}); |
| 178 |
expect(fn("one")).toBe(fn("one")); |
| 179 |
|
| 180 |
expect(fn("two")).toEqual({ |
| 181 |
arg: "two", |
| 182 |
val: "default", |
| 183 |
val2: "another", |
| 184 |
count: 5, |
| 185 |
}); |
| 186 |
expect(fn("two")).toBe(fn("two")); |
| 187 |
|
| 188 |
other = "new"; |
| 189 |
|
| 190 |
expect(fn("one")).toEqual({ |
| 191 |
arg: "one", |
| 192 |
val: "new", |
| 193 |
val2: "another", |
| 194 |
count: 6, |
| 195 |
}); |
| 196 |
expect(fn("one")).toBe(fn("one")); |
| 197 |
|
| 198 |
expect(fn("two")).toEqual({ |
| 199 |
arg: "two", |
| 200 |
val: "new", |
| 201 |
val2: "another", |
| 202 |
count: 7, |
| 203 |
}); |
| 204 |
expect(fn("two")).toBe(fn("two")); |
| 205 |
|
| 206 |
another = "second"; |
| 207 |
|
| 208 |
expect(fn("one")).toEqual({ |
| 209 |
arg: "one", |
| 210 |
val: "new", |
| 211 |
val2: "second", |
| 212 |
count: 8, |
| 213 |
}); |
| 214 |
expect(fn("one")).toBe(fn("one")); |
| 215 |
|
| 216 |
expect(fn("two")).toEqual({ |
| 217 |
arg: "two", |
| 218 |
val: "new", |
| 219 |
val2: "second", |
| 220 |
count: 9, |
| 221 |
}); |
| 222 |
expect(fn("two")).toBe(fn("two")); |
| 223 |
}); |
| 224 |
|
| 225 |
it("should auto-permacache by default", () => { |
| 226 |
let count = 0; |
| 227 |
|
| 228 |
const fn = makeStrongCacheSync(arg => ({ arg, count: count++ })); |
| 229 |
|
| 230 |
expect(fn("one")).toEqual({ arg: "one", count: 0 }); |
| 231 |
expect(fn("one")).toBe(fn("one")); |
| 232 |
|
| 233 |
expect(fn("two")).toEqual({ arg: "two", count: 1 }); |
| 234 |
expect(fn("two")).toBe(fn("two")); |
| 235 |
|
| 236 |
expect(fn("one")).not.toEqual(fn("two")); |
| 237 |
}); |
| 238 |
|
| 239 |
it("should throw if you set permacaching and use .using", () => { |
| 240 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 241 |
cache.forever(); |
| 242 |
|
| 243 |
cache.using(() => null); |
| 244 |
}); |
| 245 |
|
| 246 |
expect(() => fn()).toThrow(/Caching has already been configured/); |
| 247 |
}); |
| 248 |
|
| 249 |
it("should throw if you set permacaching and use .invalidate", () => { |
| 250 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 251 |
cache.forever(); |
| 252 |
|
| 253 |
cache.invalidate(() => null); |
| 254 |
}); |
| 255 |
|
| 256 |
expect(() => fn()).toThrow(/Caching has already been configured/); |
| 257 |
}); |
| 258 |
|
| 259 |
it("should throw if you set permacaching and use .never", () => { |
| 260 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 261 |
cache.forever(); |
| 262 |
|
| 263 |
cache.never(); |
| 264 |
}); |
| 265 |
|
| 266 |
expect(() => fn()).toThrow(/Caching has already been configured/); |
| 267 |
}); |
| 268 |
|
| 269 |
it("should throw if you set no caching and use .using", () => { |
| 270 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 271 |
cache.never(); |
| 272 |
|
| 273 |
cache.using(() => null); |
| 274 |
}); |
| 275 |
|
| 276 |
expect(() => fn()).toThrow(/Caching has already been configured/); |
| 277 |
}); |
| 278 |
|
| 279 |
it("should throw if you set no caching and use .invalidate", () => { |
| 280 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 281 |
cache.never(); |
| 282 |
|
| 283 |
cache.invalidate(() => null); |
| 284 |
}); |
| 285 |
|
| 286 |
expect(() => fn()).toThrow(/Caching has already been configured/); |
| 287 |
}); |
| 288 |
|
| 289 |
it("should throw if you set no caching and use .never", () => { |
| 290 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 291 |
cache.never(); |
| 292 |
|
| 293 |
cache.using(() => null); |
| 294 |
}); |
| 295 |
|
| 296 |
expect(() => fn()).toThrow(/Caching has already been configured/); |
| 297 |
}); |
| 298 |
|
| 299 |
it("should throw if you configure .forever after exiting", () => { |
| 300 |
const fn = makeStrongCacheSync((arg, cache) => cache); |
| 301 |
|
| 302 |
expect(() => fn().forever()).toThrow( |
| 303 |
/Cannot change caching after evaluation/, |
| 304 |
); |
| 305 |
}); |
| 306 |
|
| 307 |
it("should throw if you configure .never after exiting", () => { |
| 308 |
const fn = makeStrongCacheSync((arg, cache) => cache); |
| 309 |
|
| 310 |
expect(() => fn().never()).toThrow( |
| 311 |
/Cannot change caching after evaluation/, |
| 312 |
); |
| 313 |
}); |
| 314 |
|
| 315 |
it("should throw if you configure .using after exiting", () => { |
| 316 |
const fn = makeStrongCacheSync((arg, cache) => cache); |
| 317 |
|
| 318 |
expect(() => fn().using(() => null)).toThrow( |
| 319 |
/Cannot change caching after evaluation/, |
| 320 |
); |
| 321 |
}); |
| 322 |
|
| 323 |
it("should throw if you configure .invalidate after exiting", () => { |
| 324 |
const fn = makeStrongCacheSync((arg, cache) => cache); |
| 325 |
|
| 326 |
expect(() => fn().invalidate(() => null)).toThrow( |
| 327 |
/Cannot change caching after evaluation/, |
| 328 |
); |
| 329 |
}); |
| 330 |
|
| 331 |
describe("simple", () => { |
| 332 |
it("should allow permacaching with cache(true)", () => { |
| 333 |
let count = 0; |
| 334 |
|
| 335 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 336 |
cache = cache.simple(); |
| 337 |
|
| 338 |
cache(true); |
| 339 |
return { arg, count: count++ }; |
| 340 |
}); |
| 341 |
|
| 342 |
expect(fn("one")).toEqual({ arg: "one", count: 0 }); |
| 343 |
expect(fn("one")).toBe(fn("one")); |
| 344 |
|
| 345 |
expect(fn("two")).toEqual({ arg: "two", count: 1 }); |
| 346 |
expect(fn("two")).toBe(fn("two")); |
| 347 |
|
| 348 |
expect(fn("one")).not.toEqual(fn("two")); |
| 349 |
}); |
| 350 |
|
| 351 |
it("should allow disabling caching with cache(false)", () => { |
| 352 |
let count = 0; |
| 353 |
|
| 354 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 355 |
cache = cache.simple(); |
| 356 |
|
| 357 |
cache(false); |
| 358 |
return { arg, count: count++ }; |
| 359 |
}); |
| 360 |
|
| 361 |
expect(fn("one")).toEqual({ arg: "one", count: 0 }); |
| 362 |
expect(fn("one")).toEqual({ arg: "one", count: 1 }); |
| 363 |
expect(fn("one")).not.toEqual(fn("one")); |
| 364 |
|
| 365 |
expect(fn("two")).toEqual({ arg: "two", count: 4 }); |
| 366 |
expect(fn("two")).toEqual({ arg: "two", count: 5 }); |
| 367 |
expect(fn("two")).not.toEqual(fn("two")); |
| 368 |
|
| 369 |
expect(fn("one")).not.toEqual(fn("two")); |
| 370 |
}); |
| 371 |
|
| 372 |
it("should allow caching based on a value with cache(fn)", () => { |
| 373 |
let count = 0; |
| 374 |
let other = "default"; |
| 375 |
|
| 376 |
const fn = makeStrongCacheSync((arg, cache) => { |
| 377 |
cache = cache.simple(); |
| 378 |
|
| 379 |
const val = cache(() => other); |
| 380 |
|
| 381 |
return { arg, val, count: count++ }; |
| 382 |
}); |
| 383 |
|
| 384 |
expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 }); |
| 385 |
expect(fn("one")).toBe(fn("one")); |
| 386 |
|
| 387 |
expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 }); |
| 388 |
expect(fn("two")).toBe(fn("two")); |
| 389 |
|
| 390 |
other = "new"; |
| 391 |
|
| 392 |
expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 }); |
| 393 |
expect(fn("one")).toBe(fn("one")); |
| 394 |
|
| 395 |
expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 }); |
| 396 |
expect(fn("two")).toBe(fn("two")); |
| 397 |
|
| 398 |
other = "default"; |
| 399 |
|
| 400 |
expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 }); |
| 401 |
expect(fn("one")).toBe(fn("one")); |
| 402 |
|
| 403 |
expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 }); |
| 404 |
expect(fn("two")).toBe(fn("two")); |
| 405 |
|
| 406 |
other = "new"; |
| 407 |
|
| 408 |
expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 }); |
| 409 |
expect(fn("one")).toBe(fn("one")); |
| 410 |
|
| 411 |
expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 }); |
| 412 |
expect(fn("two")).toBe(fn("two")); |
| 413 |
}); |
| 414 |
}); |
| 415 |
|
| 416 |
describe("async", () => { |
| 417 |
const wait = gensync({ |
| 418 |
sync: () => {}, |
| 419 |
errback: (t, cb) => setTimeout(cb, t), |
| 420 |
}); |
| 421 |
|
| 422 |
it("should throw if the cache is configured asynchronously", async () => { |
| 423 |
const fn = gensync( |
| 424 |
makeStrongCache(function* (arg, cache) { |
| 425 |
yield* wait(1000); |
| 426 |
cache.never(); |
| 427 |
return { arg }; |
| 428 |
}), |
| 429 |
).async; |
| 430 |
|
| 431 |
await expect(fn("bar")).rejects.toThrowErrorMatchingInlineSnapshot( |
| 432 |
`"Cannot change caching after evaluation has completed."`, |
| 433 |
); |
| 434 |
}); |
| 435 |
|
| 436 |
it("should allow asynchronous cache invalidation functions", async () => { |
| 437 |
const fn = gensync( |
| 438 |
makeStrongCache(function* (arg, cache) { |
| 439 |
yield* waitFor( |
| 440 |
cache.using(async () => { |
| 441 |
await wait.async(50); |
| 442 |
return "x"; |
| 443 |
}), |
| 444 |
); |
| 445 |
return { arg }; |
| 446 |
}), |
| 447 |
).async; |
| 448 |
|
| 449 |
const [res1, res2] = await Promise.all([fn("foo"), fn("foo")]); |
| 450 |
|
| 451 |
expect(res1).toBe(res2); |
| 452 |
}); |
| 453 |
|
| 454 |
it("should allow synchronous yield before cache configuration", async () => { |
| 455 |
const fn = gensync( |
| 456 |
makeStrongCache(function* (arg, cache) { |
| 457 |
yield* gensync({ |
| 458 |
sync: () => 2, |
| 459 |
errback: cb => cb(null, 2), |
| 460 |
})(); |
| 461 |
cache.forever(); |
| 462 |
return { arg }; |
| 463 |
}), |
| 464 |
).async; |
| 465 |
|
| 466 |
const [res1, res2] = await Promise.all([fn("foo"), fn("foo")]); |
| 467 |
|
| 468 |
expect(res1).toBe(res2); |
| 469 |
}); |
| 470 |
}); |
| 471 |
}); |