|
@@ -17,44 +17,50 @@ describe('Poll', () => {
|
|
|
describe('#constructor()', () => {
|
|
|
it('should create a poll', () => {
|
|
|
poll = new Poll({
|
|
|
+ auto: false,
|
|
|
factory: () => Promise.resolve(),
|
|
|
- name: '@jupyterlab/test-coreutils:Poll#constructor()-1',
|
|
|
- when: new Promise(() => undefined) // Never.
|
|
|
+ name: '@jupyterlab/test-coreutils:Poll#constructor()-1'
|
|
|
});
|
|
|
expect(poll).to.be.an.instanceof(Poll);
|
|
|
});
|
|
|
|
|
|
- it('should be `when-resolved` after `when` resolves', async () => {
|
|
|
- const promise = Promise.resolve();
|
|
|
+ it('should start polling automatically', async () => {
|
|
|
+ const expected = 'started resolved';
|
|
|
+ const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
- factory: () => Promise.resolve(),
|
|
|
name: '@jupyterlab/test-coreutils:Poll#constructor()-2',
|
|
|
- when: promise
|
|
|
+ frequency: { interval: 100 },
|
|
|
+ factory: () => Promise.resolve()
|
|
|
+ });
|
|
|
+ poll.ticked.connect((_, tick) => {
|
|
|
+ ticker.push(tick.phase);
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
- await promise;
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ await poll.tick;
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
+ await poll.tick;
|
|
|
+ expect(poll.state.phase).to.equal('resolved');
|
|
|
+ expect(ticker.join(' ')).to.equal(expected);
|
|
|
});
|
|
|
|
|
|
- it('should be `when-rejected` after `when` rejects', async () => {
|
|
|
- const promise = Promise.reject();
|
|
|
+ it('should not poll if `auto` is set to false', async () => {
|
|
|
+ const expected = '';
|
|
|
+ const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
- factory: () => Promise.resolve(),
|
|
|
- name: '@jupyterlab/test-coreutils:Poll#constructor()-3',
|
|
|
- when: promise
|
|
|
+ auto: false,
|
|
|
+ name: '@jupyterlab/test-coreutils:Poll#constructor()-2',
|
|
|
+ frequency: { interval: 100 },
|
|
|
+ factory: () => Promise.resolve()
|
|
|
+ });
|
|
|
+ poll.ticked.connect((_, tick) => {
|
|
|
+ ticker.push(tick.phase);
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
- await promise.catch(() => undefined);
|
|
|
- expect(poll.state.phase).to.equal('when-rejected');
|
|
|
+ await sleep(250); // Sleep for longer than the interval.
|
|
|
+ expect(ticker.join(' ')).to.equal(expected);
|
|
|
});
|
|
|
|
|
|
describe('#options.frequency', () => {
|
|
|
- let poll: Poll;
|
|
|
-
|
|
|
- afterEach(() => {
|
|
|
- poll.dispose();
|
|
|
- });
|
|
|
-
|
|
|
it('should set frequency interval', () => {
|
|
|
const interval = 9000;
|
|
|
poll = new Poll({
|
|
@@ -131,13 +137,11 @@ describe('Poll', () => {
|
|
|
const name = '@jupyterlab/test-coreutils:Poll#name-1';
|
|
|
poll = new Poll({ factory, name });
|
|
|
expect(poll.name).to.equal(name);
|
|
|
- poll.dispose();
|
|
|
});
|
|
|
|
|
|
it('should default to `unknown`', () => {
|
|
|
poll = new Poll({ factory: () => Promise.resolve() });
|
|
|
expect(poll.name).to.equal('unknown');
|
|
|
- poll.dispose();
|
|
|
});
|
|
|
});
|
|
|
|
|
@@ -171,17 +175,19 @@ describe('Poll', () => {
|
|
|
describe('#tick', () => {
|
|
|
it('should resolve after a tick', async () => {
|
|
|
poll = new Poll({
|
|
|
+ auto: false,
|
|
|
factory: () => Promise.resolve(),
|
|
|
frequency: { interval: 200, backoff: false },
|
|
|
name: '@jupyterlab/test-coreutils:Poll#tick-1'
|
|
|
});
|
|
|
- const expected = 'when-resolved resolved';
|
|
|
+ const expected = 'started resolved resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
const tock = (poll: Poll) => {
|
|
|
ticker.push(poll.state.phase);
|
|
|
poll.tick.then(tock).catch(() => undefined);
|
|
|
};
|
|
|
void poll.tick.then(tock);
|
|
|
+ void poll.start();
|
|
|
await sleep(250); // Sleep for longer than the interval.
|
|
|
expect(ticker.join(' ')).to.equal(expected);
|
|
|
});
|
|
@@ -226,39 +232,6 @@ describe('Poll', () => {
|
|
|
});
|
|
|
|
|
|
describe('#ticked', () => {
|
|
|
- it('should emit when the poll ticks after `when` resolves', async () => {
|
|
|
- const expected = 'when-resolved resolved';
|
|
|
- const ticker: IPoll.Phase<any>[] = [];
|
|
|
- poll = new Poll<void, void>({
|
|
|
- factory: () => Promise.resolve(),
|
|
|
- frequency: { interval: 200, backoff: false },
|
|
|
- name: '@jupyterlab/test-coreutils:Poll#ticked-1'
|
|
|
- });
|
|
|
- poll.ticked.connect(() => {
|
|
|
- ticker.push(poll.state.phase);
|
|
|
- });
|
|
|
- await sleep(250); // Sleep for longer than the interval.
|
|
|
- expect(ticker.join(' ')).to.equal(expected);
|
|
|
- });
|
|
|
-
|
|
|
- it('should emit when the poll ticks after `when` rejects', async () => {
|
|
|
- const expected = 'when-rejected resolved';
|
|
|
- const ticker: IPoll.Phase<any>[] = [];
|
|
|
- const promise = Promise.reject();
|
|
|
- poll = new Poll({
|
|
|
- factory: () => Promise.resolve(),
|
|
|
- frequency: { interval: 200, backoff: false },
|
|
|
- name: '@jupyterlab/test-coreutils:Poll#ticked-2',
|
|
|
- when: promise
|
|
|
- });
|
|
|
- poll.ticked.connect(() => {
|
|
|
- ticker.push(poll.state.phase);
|
|
|
- });
|
|
|
- await promise.catch(() => undefined);
|
|
|
- await sleep(250); // Sleep for longer than the interval.
|
|
|
- expect(ticker.join(' ')).to.equal(expected);
|
|
|
- });
|
|
|
-
|
|
|
it('should emit a tick identical to the poll state', async () => {
|
|
|
poll = new Poll<void, void>({
|
|
|
factory: () => Promise.resolve(),
|
|
@@ -295,8 +268,8 @@ describe('Poll', () => {
|
|
|
});
|
|
|
|
|
|
describe('#refresh()', () => {
|
|
|
- it('should refresh the poll when the poll is ready', async () => {
|
|
|
- const expected = 'when-resolved refreshed resolved';
|
|
|
+ it('should refresh the poll, superseding `started`', async () => {
|
|
|
+ const expected = 'refreshed resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
name: '@jupyterlab/test-coreutils:Poll#refresh()-1',
|
|
@@ -315,7 +288,7 @@ describe('Poll', () => {
|
|
|
});
|
|
|
|
|
|
it('should be safe to call multiple times', async () => {
|
|
|
- const expected = 'when-resolved refreshed resolved';
|
|
|
+ const expected = 'started resolved refreshed resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
name: '@jupyterlab/test-coreutils:Poll#refresh()-2',
|
|
@@ -327,7 +300,9 @@ describe('Poll', () => {
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
await poll.tick;
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
+ await poll.tick;
|
|
|
+ expect(poll.state.phase).to.equal('resolved');
|
|
|
await poll.refresh();
|
|
|
expect(poll.state.phase).to.equal('refreshed');
|
|
|
await poll.refresh();
|
|
@@ -342,7 +317,7 @@ describe('Poll', () => {
|
|
|
|
|
|
describe('#start()', () => {
|
|
|
it('should start the poll if it is stopped', async () => {
|
|
|
- const expected = 'when-resolved stopped started resolved';
|
|
|
+ const expected = 'stopped started resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
name: '@jupyterlab/test-coreutils:Poll#start()-1',
|
|
@@ -352,8 +327,6 @@ describe('Poll', () => {
|
|
|
poll.ticked.connect((_, tick) => {
|
|
|
ticker.push(tick.phase);
|
|
|
});
|
|
|
- await poll.tick;
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
await poll.stop();
|
|
|
expect(poll.state.phase).to.equal('stopped');
|
|
|
await poll.start();
|
|
@@ -364,9 +337,10 @@ describe('Poll', () => {
|
|
|
});
|
|
|
|
|
|
it('be safe to call multiple times and no-op if unnecessary', async () => {
|
|
|
- const expected = 'when-resolved resolved stopped started resolved';
|
|
|
+ const expected = 'started resolved stopped started resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
+ auto: false,
|
|
|
name: '@jupyterlab/test-coreutils:Poll#start()-2',
|
|
|
frequency: { interval: 100 },
|
|
|
factory: () => Promise.resolve()
|
|
@@ -376,11 +350,11 @@ describe('Poll', () => {
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
await poll.start();
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.start();
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.start();
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.tick;
|
|
|
expect(poll.state.phase).to.equal('resolved');
|
|
|
await poll.stop();
|
|
@@ -395,9 +369,10 @@ describe('Poll', () => {
|
|
|
|
|
|
describe('#stop()', () => {
|
|
|
it('should stop the poll if it is active', async () => {
|
|
|
- const expected = 'when-resolved stopped started resolved';
|
|
|
+ const expected = 'started stopped started resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
+ auto: false,
|
|
|
name: '@jupyterlab/test-coreutils:Poll#stop()-1',
|
|
|
frequency: { interval: 100 },
|
|
|
factory: () => Promise.resolve()
|
|
@@ -405,7 +380,8 @@ describe('Poll', () => {
|
|
|
poll.ticked.connect((_, tick) => {
|
|
|
ticker.push(tick.phase);
|
|
|
});
|
|
|
- expect(poll.state.phase).to.equal('constructed');
|
|
|
+ await poll.start();
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.stop();
|
|
|
expect(poll.state.phase).to.equal('stopped');
|
|
|
await poll.start();
|
|
@@ -416,9 +392,10 @@ describe('Poll', () => {
|
|
|
});
|
|
|
|
|
|
it('be safe to call multiple times', async () => {
|
|
|
- const expected = 'when-resolved stopped started resolved';
|
|
|
+ const expected = 'started stopped started resolved';
|
|
|
const ticker: IPoll.Phase<any>[] = [];
|
|
|
poll = new Poll({
|
|
|
+ auto: false,
|
|
|
name: '@jupyterlab/test-coreutils:Poll#stop()-2',
|
|
|
frequency: { interval: 100 },
|
|
|
factory: () => Promise.resolve()
|
|
@@ -427,6 +404,8 @@ describe('Poll', () => {
|
|
|
ticker.push(tick.phase);
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
+ await poll.start();
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.stop();
|
|
|
expect(poll.state.phase).to.equal('stopped');
|
|
|
await poll.stop();
|
|
@@ -450,7 +429,7 @@ describe('Poll', () => {
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
await poll.tick;
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.tick;
|
|
|
expect(poll.state.phase).to.equal('resolved');
|
|
|
await poll.schedule({ phase: 'refreshed' });
|
|
@@ -466,7 +445,7 @@ describe('Poll', () => {
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
await poll.tick;
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.tick;
|
|
|
expect(poll.state.phase).to.equal('resolved');
|
|
|
await poll.schedule();
|
|
@@ -482,7 +461,7 @@ describe('Poll', () => {
|
|
|
});
|
|
|
expect(poll.state.phase).to.equal('constructed');
|
|
|
await poll.tick;
|
|
|
- expect(poll.state.phase).to.equal('when-resolved');
|
|
|
+ expect(poll.state.phase).to.equal('started');
|
|
|
await poll.tick;
|
|
|
expect(poll.state.phase).to.equal('resolved');
|
|
|
await poll.schedule();
|