Skip to content

Commit

Permalink
Merge branch 'master' into feat_use_nbd_client_backup
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-f committed Oct 27, 2022
2 parents 21de28c + ed7ff1f commit 68fa091
Show file tree
Hide file tree
Showing 55 changed files with 810 additions and 416 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ module.exports = {
},
},
{
files: ['*.spec.{,c,m}js'],
excludedFiles: ['@vates/nbd-client', '@vates/otp'],
files: ['*.{spec,test}.{,c,m}js'],
rules: {
'n/no-unpublished-require': 'off',
'n/no-unpublished-import': 'off',
'n/no-unsupported-features/node-builtins': [
'error',
{
Expand Down
52 changes: 35 additions & 17 deletions @vates/async-each/index.spec.js → @vates/async-each/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

/* eslint-env jest */
const { describe, it, beforeEach } = require('test')
const assert = require('assert').strict
const { spy } = require('sinon')

const { asyncEach } = require('./')

Expand Down Expand Up @@ -34,12 +36,18 @@ describe('asyncEach', () => {
})

it('works', async () => {
const iteratee = jest.fn(async () => {})
const iteratee = spy(async () => {})

await asyncEach.call(thisArg, iterable, iteratee, { concurrency: 1 })

expect(iteratee.mock.instances).toEqual(Array.from(values, () => thisArg))
expect(iteratee.mock.calls).toEqual(Array.from(values, (value, index) => [value, index, iterable]))
assert.deepStrictEqual(
iteratee.thisValues,
Array.from(values, () => thisArg)
)
assert.deepStrictEqual(
iteratee.args,
Array.from(values, (value, index) => [value, index, iterable])
)
})
;[1, 2, 4].forEach(concurrency => {
it('respects a concurrency of ' + concurrency, async () => {
Expand All @@ -49,7 +57,7 @@ describe('asyncEach', () => {
values,
async () => {
++running
expect(running).toBeLessThanOrEqual(concurrency)
assert.deepStrictEqual(running <= concurrency, true)
await randomDelay()
--running
},
Expand All @@ -59,42 +67,52 @@ describe('asyncEach', () => {
})

it('stops on first error when stopOnError is true', async () => {
const tracker = new assert.CallTracker()

const error = new Error()
const iteratee = jest.fn((_, i) => {
const iteratee = tracker.calls((_, i) => {
if (i === 1) {
throw error
}
})
}, 2)
assert.deepStrictEqual(
await rejectionOf(asyncEach(iterable, iteratee, { concurrency: 1, stopOnError: true })),
error
)

expect(await rejectionOf(asyncEach(iterable, iteratee, { concurrency: 1, stopOnError: true }))).toBe(error)
expect(iteratee).toHaveBeenCalledTimes(2)
tracker.verify()
})

it('rejects AggregateError when stopOnError is false', async () => {
const errors = []
const iteratee = jest.fn(() => {
const iteratee = spy(() => {
const error = new Error()
errors.push(error)
throw error
})

const error = await rejectionOf(asyncEach(iterable, iteratee, { stopOnError: false }))
expect(error.errors).toEqual(errors)
expect(iteratee.mock.calls).toEqual(Array.from(values, (value, index) => [value, index, iterable]))
assert.deepStrictEqual(error.errors, errors)
assert.deepStrictEqual(
iteratee.args,
Array.from(values, (value, index) => [value, index, iterable])
)
})

it('can be interrupted with an AbortSignal', async () => {
const tracker = new assert.CallTracker()

const ac = new AbortController()
const iteratee = jest.fn((_, i) => {
const iteratee = tracker.calls((_, i) => {
if (i === 1) {
ac.abort()
}
}, 2)
await assert.rejects(asyncEach(iterable, iteratee, { concurrency: 1, signal: ac.signal }), {
message: 'asyncEach aborted',
})

await expect(asyncEach(iterable, iteratee, { concurrency: 1, signal: ac.signal })).rejects.toThrow(
'asyncEach aborted'
)
expect(iteratee).toHaveBeenCalledTimes(2)
tracker.verify()
})
})
)
Expand Down
8 changes: 7 additions & 1 deletion @vates/async-each/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"node": ">=8.10"
},
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "node--test"
},
"devDependencies": {
"sinon": "^14.0.1",
"tap": "^16.3.0",
"test": "^3.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

/* eslint-env jest */
const { describe, it } = require('test')
const assert = require('assert')

const { coalesceCalls } = require('./')

Expand All @@ -23,13 +24,13 @@ describe('coalesceCalls', () => {
const promise2 = fn(defer2.promise)

defer1.resolve('foo')
expect(await promise1).toBe('foo')
expect(await promise2).toBe('foo')
assert.strictEqual(await promise1, 'foo')
assert.strictEqual(await promise2, 'foo')

const defer3 = pDefer()
const promise3 = fn(defer3.promise)

defer3.resolve('bar')
expect(await promise3).toBe('bar')
assert.strictEqual(await promise3, 'bar')
})
})
6 changes: 5 additions & 1 deletion @vates/coalesce-calls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"node": ">=8.10"
},
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "node--test"
},
"devDependencies": {
"test": "^3.2.1"
}
}
32 changes: 15 additions & 17 deletions @vates/compose/index.spec.js → @vates/compose/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

/* eslint-env jest */
const { describe, it } = require('test')
const assert = require('node:assert').strict

const { compose } = require('./')

Expand All @@ -9,59 +10,56 @@ const mul3 = x => x * 3

describe('compose()', () => {
it('throws when no functions is passed', () => {
expect(() => compose()).toThrow(TypeError)
expect(() => compose([])).toThrow(TypeError)
assert.throws(() => compose(), TypeError)
assert.throws(() => compose([]), TypeError)
})

it('applies from left to right', () => {
expect(compose(add2, mul3)(5)).toBe(21)
assert.strictEqual(compose(add2, mul3)(5), 21)
})

it('accepts functions in an array', () => {
expect(compose([add2, mul3])(5)).toBe(21)
assert.strictEqual(compose([add2, mul3])(5), 21)
})

it('can apply from right to left', () => {
expect(compose({ right: true }, add2, mul3)(5)).toBe(17)
assert.strictEqual(compose({ right: true }, add2, mul3)(5), 17)
})

it('accepts options with functions in an array', () => {
expect(compose({ right: true }, [add2, mul3])(5)).toBe(17)
assert.strictEqual(compose({ right: true }, [add2, mul3])(5), 17)
})

it('can compose async functions', async () => {
expect(
assert.strictEqual(
await compose(
{ async: true },
async x => x + 2,
async x => x * 3
)(5)
).toBe(21)
)(5),
21
)
})

it('forwards all args to first function', () => {
expect.assertions(1)

const expectedArgs = [Math.random(), Math.random()]
compose(
(...args) => {
expect(args).toEqual(expectedArgs)
assert.deepEqual(args, expectedArgs)
},
// add a second function to avoid the one function special case
Function.prototype
)(...expectedArgs)
})

it('forwards context to all functions', () => {
expect.assertions(2)

const expectedThis = {}
compose(
function () {
expect(this).toBe(expectedThis)
assert.strictEqual(this, expectedThis)
},
function () {
expect(this).toBe(expectedThis)
assert.strictEqual(this, expectedThis)
}
).call(expectedThis)
})
Expand Down
6 changes: 5 additions & 1 deletion @vates/compose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"node": ">=7.6"
},
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "node--test"
},
"devDependencies": {
"test": "^3.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assert = require('assert')
const { describe, it } = require('tap').mocha
const { describe, it } = require('test')

const { decorateClass, decorateWith, decorateMethodsWith, perInstance } = require('./')

Expand Down
4 changes: 2 additions & 2 deletions @vates/decorate-with/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
},
"scripts": {
"postversion": "npm publish --access public",
"test": "tap"
"test": "node--test"
},
"devDependencies": {
"tap": "^16.0.1"
"test": "^3.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict'

/* eslint-env jest */
const { describe, it } = require('test')
const { useFakeTimers, spy, assert } = require('sinon')

const { createDebounceResource } = require('./debounceResource')

jest.useFakeTimers()
const clock = useFakeTimers()

describe('debounceResource()', () => {
it('calls the resource disposer after 10 seconds', async () => {
const debounceResource = createDebounceResource()
const delay = 10e3
const dispose = jest.fn()
const dispose = spy()

const resource = await debounceResource(
Promise.resolve({
Expand All @@ -22,10 +23,10 @@ describe('debounceResource()', () => {

resource.dispose()

expect(dispose).not.toBeCalled()
assert.notCalled(dispose)

jest.advanceTimersByTime(delay)
clock.tick(delay)

expect(dispose).toBeCalled()
assert.called(dispose)
})
})

0 comments on commit 68fa091

Please sign in to comment.