Skip to content

Commit

Permalink
Reject an unresolved promise when canceling CreateSDPTask
Browse files Browse the repository at this point in the history
  • Loading branch information
simmkyu committed Dec 23, 2019
1 parent c607861 commit 4cd90bd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
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();
});
});
});
});

0 comments on commit 4cd90bd

Please sign in to comment.