Stefan Huber

testing support

This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
......@@ -6,19 +6,30 @@ interface ProgramRepository {
}
interface Player {
new() : Player;
on(name:string, any);
once(name:string, any);
start() : void;
stop() : void;
programRepository: ProgramRepository;
programManager: ProgramManager;
}
interface ProgramManager {
new() : ProgramManager;
programRepository: ProgramRepository;
}
interface ProgramItemFactory {
basePath: string;
programRepository: ProgramRepository;
new() : ProgramItemFactory;
getProgramItem(programItemId:string) : Promise<any>;
basePath: string;
programRepository: ProgramRepository;
}
declare var digsigPlayer: {
......
......@@ -15,8 +15,8 @@ var rollupConfig = {
dest: 'build/bundle.js',
plugins: [
builtins(),
globals(),
// builtins(),
// globals(),
typescript()
]
......
......@@ -15,7 +15,7 @@ export class DummyProgramRepository implements ProgramRepository {
return null;
}
replicate() : Promise<void> {
replicate() : Promise<boolean> {
return null;
}
......
......@@ -31,8 +31,7 @@ describe('Player repository triggers', () => {
});
player.triggerReplication().then(() => {
expect(player.trigger).toHaveBeenCalledWith(player.triggerProgramItemId, Util.calculateNextMinute());
expect(player.trigger).toHaveBeenCalled();
done();
});
});
......@@ -44,10 +43,12 @@ describe('Player repository triggers', () => {
});
});
player.triggerReplication().then(() => {
expect(player.trigger).toHaveBeenCalledWith(player.triggerReplication, player.replicationRetry);
player.once('error', () => {
expect(player.trigger).toHaveBeenCalled();
done();
});
player.triggerReplication();
});
});
......
......@@ -13,7 +13,8 @@ describe("Util", () => {
});
it('should get the remaining seconds', () => {
expect(Util.calculateNextMinute()).toEqual((60 - (new Date()).getSeconds()) * 1000);
let now = (60 - (new Date()).getSeconds());
expect(Util.calculateNextMinute() / 1000).toBeCloseTo(now);
});
});
\ No newline at end of file
......
......@@ -8,9 +8,13 @@ const STATE_STOP = "stop";
export class Player extends EventEmitter {
constructor() {
super();
}
protected _programRepository:ProgramRepository;
protected _programManager:ProgramManager;
protected _minutesReplication:number = 5;
protected _minutesReplication:number = 3;
protected _replicationRetry:number = 10000;
protected _currentProgramItemId:string = '';
......@@ -54,17 +58,22 @@ export class Player extends EventEmitter {
}
triggerReplication() : Promise<void> {
console.info("digsig-player-service: trigger replication");
return this.programRepository.replicate()
.then(() => {
.then(changes => {
this._currentReplicationCounter = 0;
this.trigger(this.triggerProgramItemId, Util.calculateNextMinute());
this.trigger(() => { this.triggerProgramItemId(changes); });
})
.catch(() => {
this.trigger(this.triggerReplication, this.replicationRetry);
.catch(error => {
this.trigger(() => { this.triggerReplication(); }, this.replicationRetry);
this.emit('error', error);
});
}
triggerProgramItemId() {
triggerProgramItemId(changes:boolean = false) {
console.info("digsig-player-service: trigger program item id");
this.programManager.getCurrentProgramItemId()
.then(programItemId => {
this._currentReplicationCounter++;
......@@ -73,18 +82,20 @@ export class Player extends EventEmitter {
// else (1) calculate next potential program change point
// or (2) trigger replication
if (programItemId != this._currentProgramItemId) {
if (programItemId && (programItemId != this._currentProgramItemId || changes)) {
this._currentProgramItemId = programItemId;
this.emit('play', programItemId);
} else if (this._currentReplicationCounter >= this._minutesReplication) {
}
if (this._currentReplicationCounter >= this._minutesReplication) {
this.triggerReplication();
} else {
this.trigger(this.triggerProgramItemId, Util.calculateNextMinute());
this.trigger(() => { this.triggerProgramItemId(); }, Util.calculateNextMinute());
}
});
}
trigger(func:Function, milliseconds:number) {
trigger(func:Function, milliseconds:number = 0) {
if (this._state === STATE_START) {
setTimeout(() => { func(); }, milliseconds);
}
......
......@@ -33,6 +33,7 @@ export class ProgramManager {
findCurrentProgramItem(schedule:any, dateInMinutes:number) : string {
let timeList:any = [];
let tmpSchedule:any = {};
dateInMinutes--; // make it not so strict, which will start one minute earlier
for (let startTime in schedule) {
if (schedule.hasOwnProperty(startTime)) {
......
......@@ -6,6 +6,9 @@ export interface ProgramRepository {
findByType(type:string) : Promise<Array<any>>;
replicate() : Promise<void>;
/**
* returns true / false depending on if there where any changes to download
*/
replicate() : Promise<boolean>;
}
\ No newline at end of file
......