This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support passing AbortSignals to the configured repo (#89)
This is so the user can signal that they are no longer interested in the results of the operation and system components can stop trying to fulfil it.
- Loading branch information
1 parent
3eec2cc
commit 0c5f17c
Showing
3 changed files
with
112 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
chai.use(require('chai-as-promised')) | ||
const expect = chai.expect | ||
|
||
const { collect } = require('streaming-iterables') | ||
const AbortController = require('abort-controller') | ||
|
||
const BlockService = require('../src') | ||
|
||
describe('aborting requests', () => { | ||
let abortedErr | ||
let r | ||
|
||
beforeEach(() => { | ||
abortedErr = new Error('Aborted!') | ||
const abortOnSignal = (...args) => { | ||
const { signal } = args[args.length - 1] | ||
|
||
return new Promise((resolve, reject) => { | ||
signal.addEventListener('abort', () => { | ||
reject(abortedErr) | ||
}) | ||
}) | ||
} | ||
|
||
const repo = { | ||
blocks: { | ||
put: abortOnSignal, | ||
putMany: abortOnSignal, | ||
get: abortOnSignal, | ||
delete: abortOnSignal, | ||
deleteMany: abortOnSignal | ||
} | ||
} | ||
r = new BlockService(repo) | ||
}) | ||
|
||
it('put - supports abort signals', async () => { | ||
const controller = new AbortController() | ||
setTimeout(() => controller.abort(), 1) | ||
|
||
await expect(r.put('block', { | ||
signal: controller.signal | ||
})).to.eventually.rejectedWith(abortedErr) | ||
}) | ||
|
||
it('putMany - supports abort signals', async () => { | ||
const controller = new AbortController() | ||
setTimeout(() => controller.abort(), 1) | ||
|
||
await expect(r.putMany(['block'], { | ||
signal: controller.signal | ||
})).to.eventually.rejectedWith(abortedErr) | ||
}) | ||
|
||
it('get - supports abort signals', async () => { | ||
const controller = new AbortController() | ||
setTimeout(() => controller.abort(), 1) | ||
|
||
await expect(r.get('cid', { | ||
signal: controller.signal | ||
})).to.eventually.rejectedWith(abortedErr) | ||
}) | ||
|
||
it('getMany - supports abort signals', async () => { | ||
const controller = new AbortController() | ||
setTimeout(() => controller.abort(), 1) | ||
|
||
await expect(collect(r.getMany(['cid'], { | ||
signal: controller.signal | ||
}))).to.eventually.rejectedWith(abortedErr) | ||
}) | ||
|
||
it('remove - supports abort signals', async () => { | ||
const controller = new AbortController() | ||
setTimeout(() => controller.abort(), 1) | ||
|
||
await expect(r.delete('cid', { | ||
signal: controller.signal | ||
})).to.eventually.rejectedWith(abortedErr) | ||
}) | ||
}) |