Skip to content

Commit

Permalink
Remove reliance on sinon for fake timers and stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Jun 4, 2024
1 parent 2ef85f3 commit 7f6a8fe
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 80 deletions.
17 changes: 8 additions & 9 deletions tests/integration/scenarios/dash_live_multi_periods.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { describe, beforeEach, afterEach, it, expect } from "vitest";
import { describe, beforeEach, afterEach, it, expect, vi } from "vitest";
import RxPlayer from "../../../dist/es2017";
import { manifestInfos } from "../../contents/DASH_dynamic_SegmentTemplate_Multi_Periods";
import sinon from "sinon";

/**
* Workaround to provide a "real" sleep function, which does not depend on
* sinon fakeTimers.
* vitest fakeTimers.
* Here, the environment's setTimeout function is stored before being stubed
* by sinon, allowing to sleep the wanted time without waiting sinon's clock
* by vitest, allowing to sleep the wanted time without waiting vitest's clock
* to tick.
* @param {Number} [ms=0]
* @returns {Promise}
*/
const sleepWithoutSinonStub = (function () {
const sleepWithoutFakeTimer = (function () {
const timeoutFn = window.setTimeout;
return function _nextTick(ms = 0) {
return new Promise((res) => {
Expand All @@ -23,16 +22,16 @@ const sleepWithoutSinonStub = (function () {

describe("DASH live content multi-periods (SegmentTemplate)", function () {
let player;
let clock;

beforeEach(() => {
player = new RxPlayer();
clock = sinon.useFakeTimers((1567781280 + 500) * 1000);
vi.useFakeTimers();
vi.setSystemTime((1567781280 + 500) * 1000);
});

afterEach(() => {
player.dispose();
clock.restore();
vi.useRealTimers();
});

it("should return correct maximum and minimum positions", async () => {
Expand All @@ -52,7 +51,7 @@ describe("DASH live content multi-periods (SegmentTemplate)", function () {
segmentLoader,
});

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);

const now = 1567781280 + 500;
const maxPos = player.getMaximumPosition();
Expand Down
17 changes: 8 additions & 9 deletions tests/integration/scenarios/dash_static.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { stub } from "sinon";
import { describe, beforeEach, afterEach, it, expect } from "vitest";
import { describe, beforeEach, afterEach, it, expect, vi } from "vitest";
import RxPlayer from "../../../dist/es2017";
import launchTestsForContent from "../utils/launch_tests_for_content.js";
import sleep from "../../utils/sleep.js";
Expand Down Expand Up @@ -81,15 +80,15 @@ describe("DASH content CENC wrong version in MPD", function () {
);
}
let player;
let stubs = [];
let spies = [];
beforeEach(() => {});
afterEach(() => {
if (player !== undefined) {
player.dispose();
player = undefined;
}
stubs.forEach((declaredStub) => declaredStub.restore());
stubs = [];
spies.forEach((declaredSpy) => declaredSpy.mockRestore());
spies = [];
});
it("should filter out CENC pssh with a wrong version", async function () {
if (window.MediaKeySession === undefined || window.MediaKeySession === null) {
Expand All @@ -103,10 +102,10 @@ describe("DASH content CENC wrong version in MPD", function () {
let foundCencV1 = false;
let foundOtherCencVersion = false;
player = new RxPlayer();
const generateRequestStub = stub(
const generateRequestSpy = vi.spyOn(
window.MediaKeySession.prototype,
"generateRequest",
).callsFake((_initDataType, initData) => {
).mockImplementation((_initDataType, initData) => {
let offset = 0;
while (offset < initData.length) {
const size = be4toi(initData, offset);
Expand Down Expand Up @@ -142,7 +141,7 @@ describe("DASH content CENC wrong version in MPD", function () {
}
return Promise.resolve();
});
stubs.push(generateRequestStub);
spies.push(generateRequestSpy);

player.loadVideo({
transport: brokenCencManifestInfos.transport,
Expand All @@ -169,7 +168,7 @@ describe("DASH content CENC wrong version in MPD", function () {
],
});
await checkAfterSleepWithBackoff({ maxTimeMs: 200 }, () => {
expect(generateRequestStub.called).to.equal(true, "generateRequest was not called");
expect(generateRequestSpy).toHaveBeenCalled();
expect(foundCencV1).to.equal(true, "should have found a CENC pssh v1");
expect(foundOtherCencVersion).to.equal(
false,
Expand Down
124 changes: 62 additions & 62 deletions tests/integration/scenarios/manifest_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import sinon from "sinon";
import RxPlayer from "../../../dist/es2017";

import { manifestInfos } from "../../contents/DASH_dynamic_SegmentTimeline";
import { describe, beforeEach, afterEach, it, expect } from "vitest";
import { describe, beforeEach, afterEach, it, expect, vi } from "vitest";

const MANIFEST_URL_INFOS = manifestInfos.url;

/**
* Workaround to provide a "real" sleep function, which does not depend on
* sinon fakeTimers.
* vitest fakeTimers.
* Here, the environment's setTimeout function is stored before being stubed
* by sinon, allowing to sleep the wanted time without waiting sinon's clock
* by vitest, allowing to sleep the wanted time without waiting vitest's clock
* to tick.
* @param {Number} [ms=0]
* @returns {Promise}
*/
const sleepWithoutSinonStub = (function () {
const sleepWithoutFakeTimer = (function () {
const timeoutFn = window.setTimeout;
return function _nextTick(ms = 0) {
return new Promise((res) => {
Expand All @@ -44,7 +44,7 @@ describe("manifest error management", function () {
});

it("should retry to download the manifest 5 times", async () => {
const clock = sinon.useFakeTimers();
vi.useFakeTimers();
fakeServer.respondWith("GET", MANIFEST_URL_INFOS.url, (res) => res.respond(500));

player.loadVideo({
Expand All @@ -54,39 +54,39 @@ describe("manifest error management", function () {

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
await sleepWithoutFakeTimer(0);

clock.restore();
vi.useRealTimers();

await sleep(5);
const error = player.getError();
Expand All @@ -96,7 +96,7 @@ describe("manifest error management", function () {
});

it("should parse the manifest if it works the second time", async () => {
const clock = sinon.useFakeTimers();
vi.useFakeTimers();

fakeServer.respondWith("GET", MANIFEST_URL_INFOS.url, (xhr) => {
return xhr.respond(500);
Expand All @@ -109,23 +109,23 @@ describe("manifest error management", function () {

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
await sleepWithoutFakeTimer(0);
fakeServer.restore();
clock.tick(5000);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);
await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);

clock.restore();
vi.useRealTimers();

await sleep(50);
expect(player.getError()).to.equal(null);
});

it("should parse the manifest if it works the third time", async () => {
const clock = sinon.useFakeTimers();
vi.useFakeTimers();
fakeServer.respondWith("GET", MANIFEST_URL_INFOS.url, (xhr) => {
return xhr.respond(500);
});
Expand All @@ -137,30 +137,30 @@ describe("manifest error management", function () {

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
await sleepWithoutFakeTimer(0);
fakeServer.restore();
clock.tick(5000);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);
await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);

clock.restore();
vi.useRealTimers();

await sleep(1000);
expect(player.getError()).to.equal(null);
}, 10000);

it("should parse the manifest if it works the fourth time", async () => {
const clock = sinon.useFakeTimers();
vi.useFakeTimers();
fakeServer.respondWith("GET", MANIFEST_URL_INFOS.url, (xhr) => {
return xhr.respond(500);
});
Expand All @@ -172,37 +172,37 @@ describe("manifest error management", function () {

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
await sleepWithoutFakeTimer(0);
fakeServer.restore();
clock.tick(5000);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);
await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);

clock.restore();
vi.useRealTimers();

await sleep(5);
expect(player.getError()).to.equal(null);
});

it("should parse the manifest if it works the fifth time", async () => {
const clock = sinon.useFakeTimers();
vi.useFakeTimers();
fakeServer.respondWith("GET", MANIFEST_URL_INFOS.url, (xhr) => {
return xhr.respond(500);
});
Expand All @@ -214,35 +214,35 @@ describe("manifest error management", function () {

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
clock.tick(5000);
await sleepWithoutFakeTimer(0);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);

await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);
fakeServer.respond();
await sleepWithoutSinonStub(0);
await sleepWithoutFakeTimer(0);
fakeServer.restore();
clock.tick(5000);
vi.advanceTimersByTime(5000);

expect(player.getError()).to.equal(null);
await sleepWithoutSinonStub(50);
await sleepWithoutFakeTimer(50);

clock.restore();
vi.useRealTimers();

await sleep(5);
expect(player.getError()).to.equal(null);
Expand Down

0 comments on commit 7f6a8fe

Please sign in to comment.