tileWarmupExecutor.test.js 3.6 KB

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