Skip to content

Commit 9cea819

Browse files
committed
Add additional unit tests for ThroughputRule
1 parent 7114a93 commit 9cea819

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

test/unit/mocks/AbrControllerMock.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ function AbrControllerMock () {
4545

4646
this.setAbandonmentStateFor = function () {};
4747

48-
this.getAbandonmentStateFor = function () {};
48+
this.getAbandonmentStateFor = function () {
49+
return 'allowload';
50+
};
4951

5052
this.getQualityForBitrate = function () {};
5153

test/unit/test/streaming/streaming.rules.abr.ThroughputRule.js

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
import ThroughputRule from '../../../../src/streaming/rules/abr/ThroughputRule.js';
22
import SwitchRequest from '../../../../src/streaming/rules/SwitchRequest.js';
33
import {expect} from 'chai';
4+
import DashMetricsMock from '../../mocks/DashMetricsMock.js';
5+
import AbrControllerMock from '../../mocks/AbrControllerMock.js';
6+
import Settings from '../../../../src/core/Settings.js';
47

58
const context = {};
6-
const throughputRule = ThroughputRule(context).create({});
79

810
describe('ThroughputRule', function () {
11+
12+
let throughputRule;
13+
let rulesContextMock;
14+
let dashMetricsMock;
15+
let abrControllerMock;
16+
let settings;
17+
18+
beforeEach(function () {
19+
settings = Settings(context).getInstance();
20+
dashMetricsMock = new DashMetricsMock();
21+
dashMetricsMock.bufferState = {state: 'bufferLoaded'};
22+
abrControllerMock = new AbrControllerMock();
23+
throughputRule = ThroughputRule(context).create({
24+
dashMetrics: dashMetricsMock
25+
})
26+
rulesContextMock = {}
27+
rulesContextMock.getThroughputController = function () {
28+
return null;
29+
}
30+
rulesContextMock.getAbrController = function () {
31+
return abrControllerMock
32+
}
33+
rulesContextMock.getMediaInfo = function () {}
34+
rulesContextMock.getMediaType = function () {}
35+
rulesContextMock.getStreamInfo = function () {}
36+
rulesContextMock.getScheduleController = function () {
37+
return {
38+
setTimeToLoadDelay: function () {
39+
}
40+
}
41+
}
42+
});
43+
944
it('should return an empty switchRequest when getSwitchRequest function is called with an empty parameter', function () {
1045
const maxIndexRequest = throughputRule.getSwitchRequest();
1146

@@ -18,4 +53,70 @@ describe('ThroughputRule', function () {
1853
expect(maxIndexRequest.representation).to.be.equal(SwitchRequest.NO_CHANGE);
1954
});
2055

56+
it('should return NO_CHANGE with RulesContextMock when there are no throughput samples', function () {
57+
const req = throughputRule.getSwitchRequest(rulesContextMock);
58+
expect(req.representation).to.equal(SwitchRequest.NO_CHANGE);
59+
});
60+
61+
62+
63+
it('should return NO_CHANGE with RulesContextMock when there is no buffer state', function () {
64+
dashMetricsMock.bufferState = null;
65+
rulesContextMock.getThroughputController = function () {
66+
return {
67+
getSafeAverageThroughput: function () {
68+
return 3000000;
69+
},
70+
getAverageLatency: function () {
71+
return 0
72+
}
73+
}
74+
}
75+
const req = throughputRule.getSwitchRequest(rulesContextMock);
76+
expect(req.representation).to.equal(SwitchRequest.NO_CHANGE);
77+
});
78+
79+
it('should switch and provide correct throughput and priority', function () {
80+
// Current quality = 1 (1 Mbps). Provide throughput ~3 Mbps => should go to index 3 (3.5 Mbps) if safetyFactor * throughput > 3.5M? 3 Mbps * 0.9 = 2.7 < 3.5 so expect index 2 (2 Mbps).
81+
const throughput = 3000000;
82+
settings.update({
83+
streaming: {
84+
abr: {
85+
rules: {
86+
throughputRule: {
87+
priority: 5
88+
}
89+
}
90+
}
91+
}
92+
})
93+
rulesContextMock.getThroughputController = function () {
94+
return {
95+
getSafeAverageThroughput: function () {
96+
return throughput;
97+
},
98+
getAverageLatency: function () {
99+
return 0
100+
}
101+
}
102+
}
103+
rulesContextMock.getAbrController = function () {
104+
return {
105+
getAbandonmentStateFor: function () {
106+
return 'allowload'
107+
},
108+
getOptimalRepresentationForBitrate: function (mediaInfo, throughput) {
109+
return {
110+
id: 1,
111+
throughput
112+
}
113+
}
114+
}
115+
}
116+
const switchRequest = throughputRule.getSwitchRequest(rulesContextMock);
117+
expect(switchRequest.representation.id).to.equal(1);
118+
expect(switchRequest.reason.throughput).to.equal(throughput);
119+
expect(switchRequest.priority).to.equal(5);
120+
});
121+
21122
});

0 commit comments

Comments
 (0)