11import ThroughputRule from '../../../../src/streaming/rules/abr/ThroughputRule.js' ;
22import SwitchRequest from '../../../../src/streaming/rules/SwitchRequest.js' ;
33import { 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
58const context = { } ;
6- const throughputRule = ThroughputRule ( context ) . create ( { } ) ;
79
810describe ( '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