| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import assert from 'node:assert/strict'
- import { test } from 'node:test'
- import { runTileWarmup } from '../src/modules/cockpit/domain/tileWarmup.js'
- const URLS = Array.from({ length: 12 }, (_, index) => `/gis/pic-ring0/15/${index}/0.png`)
- function okResponse() {
- return { ok: true, status: 200, arrayBuffer: async () => new ArrayBuffer(8) }
- }
- test('单次取数失败不能终止整批预热:其余瓦片仍要全部尝试', async () => {
- const attempted = []
- let transientHit = false
- await runTileWarmup(URLS, {
- concurrency: 1,
- fetchImpl: async (url) => {
- attempted.push(url)
- if (!transientHit && url === URLS[2]) {
- transientHit = true
- throw new Error('偶发网络抖动')
- }
- return okResponse()
- },
- })
- assert.deepEqual([...new Set(attempted)], URLS, '每个 URL 都必须发起过请求')
- })
- test('短暂失败应重试同一瓦片,重试成功后不算失败', async () => {
- const attempts = new Map()
- const result = await runTileWarmup(URLS.slice(0, 3), {
- concurrency: 1,
- retries: 1,
- fetchImpl: async (url) => {
- const count = (attempts.get(url) || 0) + 1
- attempts.set(url, count)
- if (url === URLS[1] && count === 1) throw new Error('第一次失败')
- return okResponse()
- },
- })
- assert.equal(attempts.get(URLS[1]), 2, '失败瓦片应重试一次')
- assert.equal(result.failed, 0)
- assert.equal(result.warmed, 3)
- })
- test('非 2xx 响应算失败,不重试也不拖垮其它 worker', async () => {
- const attempted = []
- const result = await runTileWarmup(URLS, {
- concurrency: 2,
- retries: 2,
- fetchImpl: async (url) => {
- attempted.push(url)
- if (url === URLS[4]) return { ok: false, status: 404, arrayBuffer: async () => new ArrayBuffer(0) }
- return okResponse()
- },
- })
- assert.deepEqual(attempted, URLS, '404 不应被重复请求')
- assert.equal(result.failed, 1)
- assert.equal(result.warmed, URLS.length - 1)
- })
- test('预热结束回报完整统计,便于判断进区前是否捂热完成', async () => {
- let finalProgress
- const result = await runTileWarmup(URLS.slice(0, 4), {
- concurrency: 2,
- onProgress: (state) => { finalProgress = state },
- fetchImpl: async () => okResponse(),
- })
- assert.deepEqual(result, { planned: 4, warmed: 4, failed: 0, aborted: false })
- assert.equal(finalProgress.done, 4)
- assert.equal(finalProgress.warmed, 4)
- })
- test('取消后不再发起新的取数请求', async () => {
- const controller = new AbortController()
- const attempted = []
- const result = await runTileWarmup(URLS, {
- concurrency: 1,
- signal: controller.signal,
- fetchImpl: async (url) => {
- attempted.push(url)
- if (attempted.length === 2) controller.abort()
- return okResponse()
- },
- })
- assert.equal(attempted.length, 2)
- assert.equal(result.aborted, true)
- assert.equal(result.warmed, 2)
- })
|