feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
@ -26,14 +26,21 @@ test('large non-ASCII editorial content remains complete without truncation', ()
|
||||
assert.equal(first.content_truncated, false)
|
||||
assert.equal(first.content_state, 'complete')
|
||||
})
|
||||
test('enrichment deduplicates loads, bounds concurrency and preserves input order', async () => {
|
||||
test('enrichment deduplicates loads, bounds concurrency and preserves input order', async t => {
|
||||
t.mock.timers.enable({ apis: ['setTimeout', 'Date'] })
|
||||
const urls = ['a', 'b', 'a', 'c'].map(id => `https://www.bloomberg.com/news/articles/${id}`)
|
||||
const calls = []; let active = 0; let peak = 0
|
||||
const result = await enrichArticles(urls, async url => {
|
||||
let done = false
|
||||
const operation = enrichArticles(urls, async url => {
|
||||
calls.push(url); active++; peak = Math.max(peak, active)
|
||||
await new Promise(resolve => setTimeout(resolve, url.endsWith('/a') ? 10 : 1))
|
||||
active--; return basic('synthetic ' + url.slice(-1))
|
||||
}, { concurrency: 2, articleTimeoutMs: 100, totalTimeoutMs: 200 })
|
||||
}, { concurrency: 2, articleTimeoutMs: 100, totalTimeoutMs: 200 }).then(result => { done = true; return result })
|
||||
for (let i = 0; i < 20 && !done; i++) {
|
||||
await new Promise(resolve => setImmediate(resolve))
|
||||
t.mock.timers.tick(5)
|
||||
}
|
||||
const result = await operation
|
||||
assert.equal(calls.length, 3); assert.equal(peak, 2)
|
||||
assert.deepEqual(result.map(v => v.url), urls)
|
||||
assert.deepEqual(result.map(v => v.content), ['synthetic a', 'synthetic b', 'synthetic a', 'synthetic c'])
|
||||
|
||||
Reference in New Issue
Block a user