tileWarmupExecutor.test.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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('预热结束回报完整统计,便于判断进区前是否捂热完成', async () => {
  56. let finalProgress
  57. const result = await runTileWarmup(URLS.slice(0, 4), {
  58. concurrency: 2,
  59. onProgress: (state) => { finalProgress = state },
  60. fetchImpl: async () => okResponse(),
  61. })
  62. assert.deepEqual(result, { planned: 4, warmed: 4, failed: 0, aborted: false })
  63. assert.equal(finalProgress.done, 4)
  64. assert.equal(finalProgress.warmed, 4)
  65. })
  66. test('取消后不再发起新的取数请求', async () => {
  67. const controller = new AbortController()
  68. const attempted = []
  69. const result = await runTileWarmup(URLS, {
  70. concurrency: 1,
  71. signal: controller.signal,
  72. fetchImpl: async (url) => {
  73. attempted.push(url)
  74. if (attempted.length === 2) controller.abort()
  75. return okResponse()
  76. },
  77. })
  78. assert.equal(attempted.length, 2)
  79. assert.equal(result.aborted, true)
  80. assert.equal(result.warmed, 2)
  81. })