tileWarmupExecutor.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import { runTileWarmup } from '../src/modules/cockpit/domain/tileWarmup.js'
  4. const URLS = Array.from({ length: 12 }, (_, index) => `/gis/pic-ring0/15/${index}/0.png`)
  5. function okResponse() {
  6. return { ok: true, status: 200, arrayBuffer: async () => new ArrayBuffer(8) }
  7. }
  8. test('单次取数失败不能终止整批预热:其余瓦片仍要全部尝试', async () => {
  9. const attempted = []
  10. let transientHit = false
  11. await runTileWarmup(URLS, {
  12. concurrency: 1,
  13. fetchImpl: async (url) => {
  14. attempted.push(url)
  15. if (!transientHit && url === URLS[2]) {
  16. transientHit = true
  17. throw new Error('偶发网络抖动')
  18. }
  19. return okResponse()
  20. },
  21. })
  22. assert.deepEqual([...new Set(attempted)], URLS, '每个 URL 都必须发起过请求')
  23. })
  24. test('短暂失败应重试同一瓦片,重试成功后不算失败', async () => {
  25. const attempts = new Map()
  26. const result = await runTileWarmup(URLS.slice(0, 3), {
  27. concurrency: 1,
  28. retries: 1,
  29. fetchImpl: async (url) => {
  30. const count = (attempts.get(url) || 0) + 1
  31. attempts.set(url, count)
  32. if (url === URLS[1] && count === 1) throw new Error('第一次失败')
  33. return okResponse()
  34. },
  35. })
  36. assert.equal(attempts.get(URLS[1]), 2, '失败瓦片应重试一次')
  37. assert.equal(result.failed, 0)
  38. assert.equal(result.warmed, 3)
  39. })
  40. test('非 2xx 响应算失败,不重试也不拖垮其它 worker', async () => {
  41. const attempted = []
  42. const result = await runTileWarmup(URLS, {
  43. concurrency: 2,
  44. retries: 2,
  45. fetchImpl: async (url) => {
  46. attempted.push(url)
  47. if (url === URLS[4]) return { ok: false, status: 404, arrayBuffer: async () => new ArrayBuffer(0) }
  48. return okResponse()
  49. },
  50. })
  51. assert.deepEqual(attempted, URLS, '404 不应被重复请求')
  52. assert.equal(result.failed, 1)
  53. assert.equal(result.warmed, URLS.length - 1)
  54. })
  55. test('HTTP 缓存之外还要触发 Image 解码缓存,Cesium 首帧才不必现解码', async () => {
  56. const decoded = []
  57. class FakeImage {
  58. set src(value) {
  59. decoded.push(value)
  60. queueMicrotask(() => this.onload?.())
  61. }
  62. }
  63. const result = await runTileWarmup(URLS.slice(0, 5), {
  64. concurrency: 2,
  65. ImageCtor: FakeImage,
  66. fetchImpl: async () => okResponse(),
  67. })
  68. assert.deepEqual(decoded, URLS.slice(0, 5))
  69. assert.equal(result.warmed, 5)
  70. })
  71. test('预热结束回报完整统计,便于判断进区前是否捂热完成', async () => {
  72. let finalProgress
  73. const result = await runTileWarmup(URLS.slice(0, 4), {
  74. concurrency: 2,
  75. onProgress: (state) => { finalProgress = state },
  76. fetchImpl: async () => okResponse(),
  77. })
  78. assert.deepEqual(result, { planned: 4, warmed: 4, failed: 0, aborted: false })
  79. assert.equal(finalProgress.done, 4)
  80. assert.equal(finalProgress.warmed, 4)
  81. })
  82. test('取消后不再发起新的取数请求', async () => {
  83. const controller = new AbortController()
  84. const attempted = []
  85. const result = await runTileWarmup(URLS, {
  86. concurrency: 1,
  87. signal: controller.signal,
  88. fetchImpl: async (url) => {
  89. attempted.push(url)
  90. if (attempted.length === 2) controller.abort()
  91. return okResponse()
  92. },
  93. })
  94. assert.equal(attempted.length, 2)
  95. assert.equal(result.aborted, true)
  96. assert.equal(result.warmed, 2)
  97. })