Skip to content

Commit

Permalink
test: Work around general network flake in tests (#156)
Browse files Browse the repository at this point in the history
Retry any failed request (failed to fetch, not failed HTTP status) up to 3 times.
  • Loading branch information
joeyparrish authored Aug 31, 2024
1 parent c5042ea commit 8bee20a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,27 @@ jasmineEnv.execute = () => {
originalJasmineExecute();
};

async function fetchRetry(url, options) {
// Retry in case of network flake. This only retries if the request fails
// without an HTTP status code, such as a connection failure or other
// low-level issue. A non-200's status code does not throw from fetch().
// This is similar to, but less complex than Shaka Player's retry logic.
for (let i = 0; i < 3; i++) {
try {
return await fetch(url, options);
} catch (exception) {
// Give up on the last try only.
if (i == 2) throw exception;

// Wait 3s between retries.
await new Promise((resolve) => setTimeout(resolve, 3000));
}
}
}

async function startStreamer(inputConfig, pipelineConfig, bitrateConfig={}, outputLocation=OUTPUT_DIR) {
// Send a request to flask server to start Shaka Streamer.
const response = await fetch(flaskServerUrl + 'start', {
const response = await fetchRetry(flaskServerUrl + 'start', {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
Expand Down Expand Up @@ -69,7 +87,7 @@ async function startStreamer(inputConfig, pipelineConfig, bitrateConfig={}, outp

async function stopStreamer() {
// Send a request to flask server to stop Shaka Streamer.
const response = await fetch(flaskServerUrl + 'stop');
const response = await fetchRetry(flaskServerUrl + 'stop');
if (!response.ok) {
throw new Error('Failed to close Shaka Streamer');
}
Expand Down Expand Up @@ -1121,7 +1139,7 @@ function availabilityTests(manifestUrl, format) {
'availability_window': 500,
};
await startStreamer(inputConfigDict, pipelineConfigDict);
const response = await fetch(manifestUrl);
const response = await fetchRetry(manifestUrl);
const bodyText = await response.text();
const re = /timeShiftBufferDepth="([^"]*)"/;
const found = bodyText.match(re);
Expand Down Expand Up @@ -1172,7 +1190,7 @@ function updateTests(manifestUrl, format) {
'update_period': 42,
};
await startStreamer(inputConfigDict, pipelineConfigDict);
const response = await fetch(manifestUrl);
const response = await fetchRetry(manifestUrl);
const bodyText = await response.text();
const re = /minimumUpdatePeriod="([^"]*)"/;
const found = bodyText.match(re);
Expand Down

0 comments on commit 8bee20a

Please sign in to comment.