Skip to content

Commit

Permalink
more tests and a review comment
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
  • Loading branch information
freben committed Nov 11, 2021
1 parent b5c106a commit 92d85b8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/backend-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ then make use of its facilities as necessary:

```typescript
import { TaskManager } from '@backstage/backend-tasks';
import { Duration } from 'luxon';

const manager = TaskManager.fromConfig(rootConfig).forPlugin('my-plugin');

Expand Down
36 changes: 35 additions & 1 deletion packages/backend-tasks/src/tasks/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,44 @@
* limitations under the License.
*/

import { Duration } from 'luxon';
import { AbortController } from 'node-abort-controller';
import { delegateAbortController } from './util';
import { delegateAbortController, sleep, validateId } from './util';

describe('util', () => {
describe('validateId', () => {
it.each(['a', 'a_b', 'ab123c_2'])(
'accepts valid inputs, %p',
async input => {
expect(validateId(input)).toBeUndefined();
},
);

it.each(['', 'a!', 'A', 'a-b', 'a.b', '_a', 'a_', null, Symbol('a')])(
'rejects invalid inputs, %p',
async input => {
expect(() => validateId(input as any)).toThrow();
},
);
});

describe('sleep', () => {
it('finishes the wait as expected with no signal', async () => {
const ac = new AbortController();
const start = Date.now();
await sleep(Duration.fromObject({ seconds: 1 }), ac.signal);
expect(Date.now() - start).toBeGreaterThan(800);
}, 5_000);

it('aborts properly on the signal', async () => {
const ac = new AbortController();
const promise = sleep(Duration.fromObject({ seconds: 10 }), ac.signal);
ac.abort();
await promise;
expect(true).toBe(true);
}, 1_000);
});

describe('delegateAbortController', () => {
it('inherits parent abort state', () => {
const parent = new AbortController();
Expand Down

0 comments on commit 92d85b8

Please sign in to comment.