Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add a test moving average decaying even without new data #1141

Merged
merged 5 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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 || '';
const NO_TEST_BUNDLE = process.env.NO_TEST_BUNDLE || '';

let syncWorker;
// see https://github.com/videojs/videojs-generate-rollup-config
Expand Down Expand Up @@ -57,6 +59,11 @@ const options = {
}
defaults.module.unshift('replace');

if (NO_TEST_BUNDLE) {
defaults.module.unshift('strip');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use rollup-plugin-stub in tests to prevent having to remove stuff from the main build? This is what we do in video.js https://github.com/videojs/video.js/blob/main/test/unit/player.test.js#L947-L955

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into this, I think that we don't want to use stub anymore. The repo is archived. Also, it adds a lot of time to the build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to always strip this code from the module/browser build? I think we can just remove the if statement and always add the strip plugin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, seem reasonable. I think I wasn't sure exactly how the test/mobule/browser builds stuff worked when I first added this.

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

return defaults;
},
primedPlugins(defaults) {
Expand All @@ -71,6 +78,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 moing average wont decay average unless new bandwidth data was provided', function(assert) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('Calling exponential moing average wont decay average unless new bandwidth data was provided', function(assert) {
test('Calling exponential moving average won\'t 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