Skip to content

Commit

Permalink
Use async/await in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loilo committed May 23, 2020
1 parent ff53e16 commit afd4e43
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions test/concurrency.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,81 @@ const branchy = require('../lib/branchy')

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

test('handles process numbers below threshold concurrently', () => {
test('handles process numbers below threshold concurrently', async () => {
const results = []
const sleeper = branchy(sleep, { concurrent: 2 })

return Promise.all([
sleeper(300).then(() => results.push(1)),
sleeper(0).then(() => results.push(2))
]).then(() => {
expect(results).toEqual([2, 1])
})
const call1 = sleeper(300).then(() => results.push(1))
const call2 = sleeper(0).then(() => results.push(2))

await Promise.all([call1, call2])

expect(results).toEqual([2, 1])
})

test('respects call priority', () => {
test('respects call priority', async () => {
const results = []
const sleeper = branchy(sleep, {
concurrent: { threads: 1, priority: num => num }
})

return Promise.all([
sleeper(300).then(() => results.push(1)),
sleeper(0).then(() => results.push(2))
]).then(() => {
expect(results).toEqual([1, 2])
})
const call1 = sleeper(300).then(() => results.push(1))
const call2 = sleeper(0).then(() => results.push(2))

await Promise.all([call1, call2])

expect(results).toEqual([1, 2])
})

test('handles process numbers above threshold sequentially', () => {
test('handles process numbers above threshold sequentially', async () => {
const results = []
const sleeper = branchy(sleep, { concurrent: 1 })

return Promise.all([
sleeper(300).then(() => results.push(1)),
sleeper(0).then(() => results.push(2))
]).then(() => {
expect(results).toEqual([1, 2])
})
const call1 = sleeper(300).then(() => results.push(1))
const call2 = sleeper(0).then(() => results.push(2))

await Promise.all([call1, call2])

expect(results).toEqual([1, 2])
})

test('handles concurrency threshold independently', () => {
test('handles concurrency threshold independently', async () => {
const results = []
const sleeper1 = branchy(sleep, { concurrent: 1 })
const sleeper2 = branchy(sleep, { concurrent: 1 })

return Promise.all([
sleeper1(300).then(() => results.push(1)),
sleeper2(0).then(() => results.push(2))
]).then(() => {
expect(results).toEqual([2, 1])
})
const call1 = sleeper1(300).then(() => results.push(1))
const call2 = sleeper2(0).then(() => results.push(2))

await Promise.all([call1, call2])

expect(results).toEqual([2, 1])
})

test('handles stacks correctly', () => {
test('handles stacks correctly', async () => {
const results = []
const sleeper = branchy(sleep, {
concurrent: { threads: 1, strategy: 'stack' }
})

return Promise.all([
sleeper(0).then(() => results.push(1)),
sleeper(300).then(() => results.push(2))
]).then(() => {
expect(results).toEqual([2, 1])
})
const call1 = sleeper(0).then(() => results.push(1))
const call2 = sleeper(300).then(() => results.push(2))

await Promise.all([call1, call2])

expect(results).toEqual([2, 1])
})

test('handles shared context correctly', () => {
test('handles shared context correctly', async () => {
const results = []
const context = branchy.createContext({ concurrent: 1 })
const sleeper1 = branchy(sleep, { concurrent: context })
const sleeper2 = branchy(sleep, { concurrent: context })

return Promise.all([
sleeper1(300).then(() => results.push(1)),
sleeper2(0).then(() => results.push(2))
]).then(() => {
expect(results).toEqual([1, 2])
})
const call1 = sleeper1(300).then(() => results.push(1))
const call2 = sleeper2(0).then(() => results.push(2))

await Promise.all([call1, call2])

expect(results).toEqual([1, 2])
})

0 comments on commit afd4e43

Please sign in to comment.