Skip to content

Commit dac9a2b

Browse files
committed
test(http-cache): tighten the cache-control specs and cover a worker flipping the switch
- Drop the platform-transport clear spec: clearHttpCache() ignores the forced transport, so it only repeated the previous spec. - The platform transport stores nothing, so its "off" spec checks only the no-cache request headers. - Worker imports report success or the failure's message, and the shared-cache spec requires success. - A worker that turns the switch off makes the main isolate's next fetch bypass the cache.
1 parent e384d5c commit dac9a2b

2 files changed

Lines changed: 98 additions & 47 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// {import: url} imports url; {setHttpCacheEnabled: enabled} flips the
2+
// process-wide HTTP module cache switch from this isolate. Either answers
3+
// {ok: true}, or {ok: false, error} with the failure's message.
4+
onmessage = function (msg) {
5+
function failed(error) {
6+
postMessage({ ok: false, error: String((error && error.message) || error) });
7+
}
8+
var request = msg.data;
9+
if (typeof request.setHttpCacheEnabled === "boolean") {
10+
try {
11+
require("ns:module").setHttpCacheEnabled(request.setHttpCacheEnabled);
12+
} catch (error) {
13+
failed(error);
14+
return;
15+
}
16+
postMessage({ ok: true });
17+
return;
18+
}
19+
import(request.import).then(function () {
20+
postMessage({ ok: true });
21+
}, failed);
22+
};

‎test-app/app/src/main/assets/app/tests/testEsmHttpLoader.js‎

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,48 +1664,53 @@ describe("HTTP ESM Loader", function () {
16641664
});
16651665
});
16661666

1667-
it("clears without throwing when the platform transport is in use", function () {
1668-
nsModule.forcePlatformHttpTransport(true);
1669-
expect(function () {
1670-
nsModule.clearHttpCache();
1671-
}).not.toThrow();
1667+
it("reaches the origin every time and stores nothing while off", function (done) {
1668+
requireOkHttp();
1669+
var target = "/esm/cached.mjs?k=off-" + runTag;
1670+
var revisions = [];
1671+
nsModule.setHttpCacheEnabled(false);
1672+
import(origin + target).then(function (m) {
1673+
revisions.push(m.revision);
1674+
return reimport(target);
1675+
}).then(function (revision) {
1676+
revisions.push(revision);
1677+
return requestStats(target);
1678+
}).then(function (stats) {
1679+
expect(stats.count).toBe(2);
1680+
expect(stats.cacheControl).toBe("no-cache");
1681+
expect(stats.pragma).toBe("no-cache");
1682+
// Nothing was stored while off, so the next fetch with
1683+
// the cache back on reaches the origin too.
1684+
nsModule.setHttpCacheEnabled(true);
1685+
return reimport(target);
1686+
}).then(function (revision) {
1687+
revisions.push(revision);
1688+
return requestStats(target);
1689+
}).then(function (stats) {
1690+
expect(revisions).toEqual([1, 2, 3]);
1691+
expect(stats.count).toBe(3);
1692+
expect(stats.cacheControl).toBeNull();
1693+
expect(stats.pragma).toBeNull();
1694+
done();
1695+
}).catch(function (error) {
1696+
reportRejection(error, done);
1697+
});
16721698
});
16731699

1674-
["okhttp", "platform"].forEach(function (transport) {
1675-
it("reaches the origin every time and stores nothing while off, over " + transport, function (done) {
1676-
if (transport === "okhttp") {
1677-
requireOkHttp();
1678-
}
1679-
nsModule.forcePlatformHttpTransport(transport === "platform");
1680-
var target = "/esm/cached.mjs?k=off-" + transport + "-" + runTag;
1681-
var revisions = [];
1682-
nsModule.setHttpCacheEnabled(false);
1683-
import(origin + target).then(function (m) {
1684-
revisions.push(m.revision);
1685-
return reimport(target);
1686-
}).then(function (revision) {
1687-
revisions.push(revision);
1688-
return requestStats(target);
1689-
}).then(function (stats) {
1690-
expect(stats.count).toBe(2);
1691-
expect(stats.cacheControl).toBe("no-cache");
1692-
expect(stats.pragma).toBe("no-cache");
1693-
// Nothing was stored while off, so the next fetch with
1694-
// the cache back on reaches the origin too.
1695-
nsModule.setHttpCacheEnabled(true);
1696-
return reimport(target);
1697-
}).then(function (revision) {
1698-
revisions.push(revision);
1699-
return requestStats(target);
1700-
}).then(function (stats) {
1701-
expect(revisions).toEqual([1, 2, 3]);
1702-
expect(stats.count).toBe(3);
1703-
expect(stats.cacheControl).toBeNull();
1704-
expect(stats.pragma).toBeNull();
1705-
done();
1706-
}).catch(function (error) {
1707-
reportRejection(error, done);
1708-
});
1700+
// The platform transport has no cache; a cache on the way may.
1701+
it("sends no-cache directives while off, over the platform transport", function (done) {
1702+
nsModule.forcePlatformHttpTransport(true);
1703+
nsModule.setHttpCacheEnabled(false);
1704+
var target = "/esm/cached.mjs?k=off-platform-" + runTag;
1705+
import(origin + target).then(function () {
1706+
return requestStats(target);
1707+
}).then(function (stats) {
1708+
expect(stats.count).toBe(1);
1709+
expect(stats.cacheControl).toBe("no-cache");
1710+
expect(stats.pragma).toBe("no-cache");
1711+
done();
1712+
}).catch(function (error) {
1713+
reportRejection(error, done);
17091714
});
17101715
});
17111716

@@ -1804,25 +1809,31 @@ describe("HTTP ESM Loader", function () {
18041809
});
18051810

18061811
describe("in workers", function () {
1807-
// Imports `url` in a new worker and resolves once that import
1808-
// settled, terminating the worker.
1809-
function importInWorker(url) {
1812+
// Sends `request` (see httpCacheWorker.js) to a new worker and
1813+
// settles with its answer, terminating the worker.
1814+
function askWorker(request) {
18101815
return new Promise(function (resolve, reject) {
1811-
var worker = new Worker("./httpImportSettleWorker.js");
1816+
var worker = new Worker("./httpCacheWorker.js");
18121817
worker.onmessage = function (msg) {
1813-
if (msg.data === "settled") {
1814-
worker.terminate();
1818+
worker.terminate();
1819+
if (msg.data.ok) {
18151820
resolve();
1821+
} else {
1822+
reject(new Error("in the worker: " + msg.data.error));
18161823
}
18171824
};
18181825
worker.onerror = function (error) {
18191826
worker.terminate();
18201827
reject(error);
18211828
};
1822-
worker.postMessage(url);
1829+
worker.postMessage(request);
18231830
});
18241831
}
18251832

1833+
function importInWorker(url) {
1834+
return askWorker({ import: url });
1835+
}
1836+
18261837
it("share the main isolate's cache", function (done) {
18271838
requireOkHttp();
18281839
var target = "/esm/cached.mjs?k=worker-shared-" + runTag;
@@ -1838,6 +1849,24 @@ describe("HTTP ESM Loader", function () {
18381849
});
18391850
});
18401851

1852+
it("turn the switch off for the main isolate too", function (done) {
1853+
var target = "/esm/cached.mjs?k=worker-switch-" + runTag;
1854+
import(origin + target).then(function () {
1855+
return askWorker({ setHttpCacheEnabled: false });
1856+
}).then(function () {
1857+
return reimport(target);
1858+
}).then(function () {
1859+
return requestStats(target);
1860+
}).then(function (stats) {
1861+
expect(stats.count).toBe(2);
1862+
expect(stats.cacheControl).toBe("no-cache");
1863+
expect(stats.pragma).toBe("no-cache");
1864+
done();
1865+
}).catch(function (error) {
1866+
reportRejection(error, done);
1867+
});
1868+
});
1869+
18411870
it("see the switch the main isolate turned off", function (done) {
18421871
var target = "/esm/cached.mjs?k=worker-off-" + runTag;
18431872
import(origin + target).then(function () {

0 commit comments

Comments
 (0)