Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Dec 1, 2024
1 parent 952a9dd commit 343cfc4
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions tests/async/parallel.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import * as _ from 'radashi'

describe('parallel', () => {
function makeProgressTracker() {
async function testConcurrency(
limit: number,
array: readonly unknown[],
expected: number[],
) {
vi.useFakeTimers()

let numInProgress = 0
const tracking: number[] = []
const func = async () => {

const promise = _.parallel(limit, array, async () => {
numInProgress++
tracking.push(numInProgress)
await _.sleep(0)
await _.sleep(10)
numInProgress--
}
})

return { tracking, func }
await vi.advanceTimersByTimeAsync(50)
await promise

expect(tracking).toEqual(expected)
}

test('returns all results from all functions', async () => {
Expand Down Expand Up @@ -42,9 +52,7 @@ describe('parallel', () => {
})

test('does not run more than the limit at once', async () => {
const { tracking, func } = makeProgressTracker()
await _.parallel(3, _.list(1, 14), func)
expect(Math.max(...tracking)).toBe(3)
await testConcurrency(3, _.list(1, 6), [1, 2, 3, 3, 3, 3])
})

test('abort before all iterations are complete', async () => {
Expand Down Expand Up @@ -115,29 +123,6 @@ describe('parallel', () => {
)
})

async function testConcurrency(
limit: number,
array: readonly unknown[],
expected: number[],
) {
vi.useFakeTimers()

let numInProgress = 0
const tracking: number[] = []

const promise = _.parallel(limit, array, async () => {
numInProgress++
tracking.push(numInProgress)
await _.sleep(10)
numInProgress--
})

await vi.advanceTimersByTimeAsync(50)
await promise

expect(tracking).toEqual(expected)
}

test('limit defaults to 1 if negative', async () => {
await testConcurrency(-1, _.list(1, 3), [1, 1, 1])
})
Expand Down

0 comments on commit 343cfc4

Please sign in to comment.