Skip to content

Commit

Permalink
test: moving average should not decay without new data (#1141)
Browse files Browse the repository at this point in the history
This tests #1137
  • Loading branch information
gkatsev committed Jun 22, 2021
1 parent 1c7a63b commit 98d8368
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"devDependencies": {
"@rollup/plugin-replace": "^2.3.4",
"@rollup/plugin-strip": "^2.0.1",
"@videojs/generator-helpers": "~2.0.1",
"d3": "^3.4.8",
"es5-shim": "^4.5.13",
Expand Down
7 changes: 7 additions & 0 deletions scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const worker = require('rollup-plugin-worker-factory');
const {terser} = require('rollup-plugin-terser');
const createTestData = require('./create-test-data.js');
const replace = require('@rollup/plugin-replace');
const strip = require('@rollup/plugin-strip');

const CI_TEST_TYPE = process.env.CI_TEST_TYPE || '';

Expand Down Expand Up @@ -57,6 +58,9 @@ const options = {
}
defaults.module.unshift('replace');

defaults.module.unshift('strip');
defaults.browser.unshift('strip');

return defaults;
},
primedPlugins(defaults) {
Expand All @@ -71,6 +75,9 @@ const options = {
output: {comments: 'some'},
compress: {passes: 2}
}),
strip: strip({
functions: ['TEST_ONLY_*']
}),
createTestData: createTestData()
});

Expand Down
12 changes: 11 additions & 1 deletion src/playlist-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const comparePlaylistResolution = function(left, right) {
* currently detected bandwidth, accounting for some amount of
* bandwidth variance
*/
export const simpleSelector = function(
export let simpleSelector = function(
master,
playerBandwidth,
playerWidth,
Expand Down Expand Up @@ -313,6 +313,16 @@ export const simpleSelector = function(
return null;
};

export const TEST_ONLY_SIMPLE_SELECTOR = (newSimpleSelector) => {
const oldSimpleSelector = simpleSelector;

simpleSelector = newSimpleSelector;

return function resetSimpleSelector() {
simpleSelector = oldSimpleSelector;
};
};

// Playlist Selectors

/**
Expand Down
45 changes: 45 additions & 0 deletions test/playlist-selectors.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { module, test } from 'qunit';
import document from 'global/document';
import {
TEST_ONLY_SIMPLE_SELECTOR,
simpleSelector,
movingAverageBandwidthSelector,
minRebufferMaxBandwidthSelector,
Expand Down Expand Up @@ -64,6 +65,50 @@ test('Exponential moving average has a configurable decay parameter', function(a
assert.equal(playlist.attributes.BANDWIDTH, 50, 'selected the middle playlist');
});

test('Calling exponential moving average wont decay average unless new bandwidth data was provided', function(assert) {
let playlist;
const simSel = simpleSelector;
const bandwidthAverages = [];

const resetSimpleSelector = TEST_ONLY_SIMPLE_SELECTOR((...args) => {
// second argument to simpleSelector is the average
bandwidthAverages.push(args[1]);
return simSel(...args);
});

this.vhs.playlists.master.playlists = [
{ attributes: { BANDWIDTH: 1 } },
{ attributes: { BANDWIDTH: 50 } },
{ attributes: { BANDWIDTH: 100 } }
];

const fiftyPercentDecay = movingAverageBandwidthSelector(0.50);

this.vhs.systemBandwidth = 50 * Config.BANDWIDTH_VARIANCE + 1;
playlist = fiftyPercentDecay.call(this.vhs);
assert.equal(playlist.attributes.BANDWIDTH, 50, 'selected the middle playlist');

this.vhs.systemBandwidth = 1000 * Config.BANDWIDTH_VARIANCE + 1;
playlist = fiftyPercentDecay.call(this.vhs);
assert.equal(playlist.attributes.BANDWIDTH, 100, 'selected the top playlist');

// using the systemBandwidth values above, 50->1000
// we decay into 1000 after 50 iterations
let i = 50;

while (i--) {
playlist = fiftyPercentDecay.call(this.vhs);
}

assert.equal(
bandwidthAverages[bandwidthAverages.length - 1],
bandwidthAverages[1],
'bandwidth should only change when we get new bandwidth data'
);

resetSimpleSelector();
});

test(
'minRebufferMaxBandwidthSelector picks highest rendition without rebuffering',
function(assert) {
Expand Down

0 comments on commit 98d8368

Please sign in to comment.