| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { |
| 3 | mapPageMergeConcurrent, |
| 4 | PAGE_MERGE_PREPARE_CONCURRENCY |
| 5 | } from '../../../src/main/session/page-merge-concurrency' |
| 6 | |
| 7 | describe('page merge concurrency', () => { |
| 8 | it('keeps prepare concurrency within the configured limit and preserves result order', async () => { |
| 9 | let active = 0 |
| 10 | let peak = 0 |
| 11 | const result = await mapPageMergeConcurrent( |
| 12 | Array.from({ length: 12 }, (_, index) => index), |
| 13 | async (item) => { |
| 14 | active += 1 |
| 15 | peak = Math.max(peak, active) |
| 16 | await new Promise((resolve) => setTimeout(resolve, 5)) |
| 17 | active -= 1 |
| 18 | return item * 2 |
| 19 | } |
| 20 | ) |
| 21 | |
| 22 | expect(peak).toBe(PAGE_MERGE_PREPARE_CONCURRENCY) |
| 23 | expect(peak).toBeLessThanOrEqual(4) |
| 24 | expect(result).toEqual(Array.from({ length: 12 }, (_, index) => index * 2)) |
| 25 | }) |
| 26 | }) |
| 27 |