Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject an unresolved promise when canceling CreateSDPTask #46

Merged
merged 1 commit into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/task/CreateSDPTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import BaseTask from './BaseTask';
export default class CreateSDPTask extends BaseTask {
protected taskName = 'CreateSDPTask';

private cancelPromise: (error: Error) => void;

constructor(private context: AudioVideoControllerState) {
super(context.logger);
}

cancel(): void {
const error = new Error(`canceling ${this.name()}`);
this.cancelPromise && this.cancelPromise(error);
}

sessionUsesAudio(): boolean {
return true;
}
Expand All @@ -35,7 +42,20 @@ export default class CreateSDPTask extends BaseTask {
offerToReceiveVideo: this.sessionUsesVideo(),
};
this.logger.info(`offerOptions: ${JSON.stringify(offerOptions)}`);
this.context.sdpOfferInit = await this.context.peer.createOffer(offerOptions);

await new Promise<void>(async (resolve, reject) => {
this.cancelPromise = (error: Error) => {
reject(error);
};

try {
this.context.sdpOfferInit = await this.context.peer.createOffer(offerOptions);
resolve();
} catch (error) {
reject(error);
}
});

this.context.logger.info(`created offer ${JSON.stringify(this.context.sdpOfferInit)}`);
}
}
33 changes: 31 additions & 2 deletions test/task/CreateSDPTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import * as chai from 'chai';

import AudioVideoControllerState from '../../src/audiovideocontroller/AudioVideoControllerState';
import NoOpAudioVideoController from '../../src/audiovideocontroller/NoOpAudioVideoController';
import TimeoutScheduler from '../../src/scheduler/TimeoutScheduler';
import CreateSDPTask from '../../src/task/CreateSDPTask';
import Task from '../../src/task/Task';
import DefaultVideoStreamIdSet from '../../src/videostreamidset/DefaultVideoStreamIdSet';
import DOMMockBehavior from '../dommock/DOMMockBehavior';
import DOMMockBuilder from '../dommock/DOMMockBuilder';

describe('CreateSDPTask', () => {
const expect: Chai.ExpectStatic = chai.expect;
let context: AudioVideoControllerState;
let domMockBuilder: DOMMockBuilder | null = null;
let domMockBuilder: DOMMockBuilder;
let domMockBehavior: DOMMockBehavior;
let task: Task;

beforeEach(() => {
domMockBuilder = new DOMMockBuilder();
domMockBehavior = new DOMMockBehavior();
domMockBuilder = new DOMMockBuilder(domMockBehavior);
context = new AudioVideoControllerState();
context.audioVideoController = new NoOpAudioVideoController();
context.videoTileController = context.audioVideoController.videoTileController;
Expand Down Expand Up @@ -79,4 +83,29 @@ describe('CreateSDPTask', () => {
task.run().catch(() => done());
});
});

describe('cancel', () => {
it('cancels a task when the session is timed out', done => {
let called = false;

domMockBehavior.asyncWaitMs = 500;
new TimeoutScheduler(50).start(() => {
task.cancel();
});

task
.run()
.then(() => {
done(new Error('This line should not be reached.'));
})
.catch(() => {
called = true;
});

new TimeoutScheduler(domMockBehavior.asyncWaitMs + 50).start(() => {
expect(called).to.be.true;
done();
});
});
});
});