Skip to content

Commit

Permalink
increase test coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
wilk committed Oct 15, 2019
1 parent da3edc9 commit ecc5868
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
40 changes: 7 additions & 33 deletions __tests__/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ describe('Job Context Testing', () => {
let error
let res

class Foo {}
class Foo { }

const ctx = {
hello: 'world',
fun: () => console.log('hello world!'),
numb: 10,
bool: true,
obj: {
nested: 'deep' /*,
date: new Date()*/
nested: 'deep'
},
// date: new Date(),
arr: [10, 20, 'meh'],
myClass: Foo,
myInstance: new Foo()
}

// foreignKey is used to increase the test coverage
// @ts-ignore
ctx.__proto__.foreignKey = 'test'

try {
// @ts-ignore
res = await job(() => ({ hello, numb, bool, obj, arr /*, date*/ }), {
Expand Down Expand Up @@ -56,7 +58,7 @@ describe('Job Context Testing', () => {
}
return sum
},
fun: function() {
fun: function () {
let sum = 0
for (let i = 0; i < 1000; i++) {
sum += i
Expand All @@ -75,32 +77,4 @@ describe('Job Context Testing', () => {
expect(error).toBeUndefined()
expect(res).toEqual([499500, 499500])
})

// todo: implement instance serialization
xit('should execute a an inline job passing object instances to context', async () => {
let error
let res

class Person {
constructor(private name, private surname) {}

fullname() {
return `${this.name} ${this.surname}`
}
}

const ctx = {
foo: new Person('foo', 'bar')
}

try {
// @ts-ignore
res = await job(() => foo.fullname(), { ctx })
} catch (err) {
error = err
}

expect(error).toBeUndefined()
expect(res).toEqual(ctx.foo.fullname())
})
})
26 changes: 25 additions & 1 deletion __tests__/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import os from 'os'

const MAX_THREADS = os.cpus().length

beforeAll(async () => await start())
afterAll(async () => await stop())

describe('Worker Pool Testing', () => {
it(`should execute at most ${MAX_THREADS} jobs at a time`, async () => {
await start()

let error
let diff

Expand All @@ -27,4 +28,27 @@ describe('Worker Pool Testing', () => {
expect(error).toBeUndefined()
expect(diff).toBeGreaterThan(40)
})

it(`should execute at most 12 jobs at a time`, async () => {
await start({ maxWorkers: 12 })

let error
let diff

const task = () => new Promise(resolve => setTimeout(() => resolve(), 20))

try {
const start = Date.now()
const promises = []
for (let i = 0; i < 12; i++) promises.push(job(task))
promises.push(job(task))
await Promise.all(promises)
diff = Date.now() - start
} catch (err) {
error = err
}

expect(error).toBeUndefined()
expect(diff).toBeGreaterThan(40)
})
})
2 changes: 1 addition & 1 deletion src/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class WorkerPool {
}
}

setup(config: SetupConfig = { maxWorkers: AVAILABLE_CPUS }): Promise<void> {
setup(config: SetupConfig = {}): Promise<void> {
this.maxWorkers = config.maxWorkers > 0 ? config.maxWorkers : AVAILABLE_CPUS

if (this.maxWorkers > 10) console.warn(`Worker pool has more than 10 workers.\nYou should also increase the Max Listeners of Node.js (https://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n)\nOtherwise, limit them with start({maxWorkers: 10})`)
Expand Down

0 comments on commit ecc5868

Please sign in to comment.