Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_rcwn_interrupted.js
blob5f3d059999f5bdd61f3d659c3d5dcc5f6257e472
1 /*
3 Checkes if the concurrent cache read/write works when the write is interrupted because of max-entry-size limits.
4 This is enhancement of 29a test, this test checks that cocurrency is resumed when the first channel is interrupted
5 in the middle of reading and the second channel already consumed some content from the cache entry.
6 This test is using a resumable response.
7 - with a profile, set max-entry-size to 1 (=1024 bytes)
8 - first channel makes a request for a resumable response
9 - second channel makes a request for the same resource, concurrent read happens
10 - first channel sets predicted data size on the entry with every chunk, it's doomed on 1024
11 - second channel now must engage interrupted concurrent write algorithm and read the rest of the content from the network
12 - both channels must deliver full content w/o errors
16 "use strict";
18 const { HttpServer } = ChromeUtils.importESModule(
19   "resource://testing-common/httpd.sys.mjs"
22 var httpProtocolHandler = Cc[
23   "@mozilla.org/network/protocol;1?name=http"
24 ].getService(Ci.nsIHttpProtocolHandler);
26 ChromeUtils.defineLazyGetter(this, "URL", function () {
27   return "http://localhost:" + httpServer.identity.primaryPort;
28 });
30 var httpServer = null;
32 function make_channel(url, callback, ctx) {
33   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
36 // need something bigger than 1024 bytes
37 const responseBody =
38   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
39   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
40   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
41   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
42   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
43   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
44   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
45   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
46   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
47   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
48   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
50 function contentHandler(metadata, response) {
51   response.processAsync();
52   do_timeout(500, () => {
53     response.setHeader("Content-Type", "text/plain");
54     response.setHeader("ETag", "Just testing");
55     response.setHeader("Cache-Control", "max-age=99999");
56     response.setHeader("Accept-Ranges", "bytes");
57     response.setHeader("Content-Length", "" + responseBody.length);
58     if (metadata.hasHeader("If-Range")) {
59       response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
61       let len = responseBody.length;
62       response.setHeader("Content-Range", "0-" + (len - 1) + "/" + len);
63     }
64     response.bodyOutputStream.write(responseBody, responseBody.length);
66     response.finish();
67   });
70 function run_test() {
71   // Static check
72   Assert.ok(responseBody.length > 1024);
74   do_get_profile();
76   Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1);
77   Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
79   httpServer = new HttpServer();
80   httpServer.registerPathHandler("/content", contentHandler);
81   httpServer.start(-1);
83   httpProtocolHandler.EnsureHSTSDataReady().then(function () {
84     var chan1 = make_channel(URL + "/content");
85     chan1.asyncOpen(
86       new ChannelListener(firstTimeThrough, null, CL_IGNORE_DELAYS)
87     );
88     var chan2 = make_channel(URL + "/content");
89     chan2
90       .QueryInterface(Ci.nsIRaceCacheWithNetwork)
91       .test_delayCacheEntryOpeningBy(200);
92     chan2.QueryInterface(Ci.nsIRaceCacheWithNetwork).test_triggerNetwork(50);
93     chan2.asyncOpen(
94       new ChannelListener(secondTimeThrough, null, CL_IGNORE_DELAYS)
95     );
96   });
98   do_test_pending();
101 function firstTimeThrough(request, buffer) {
102   Assert.equal(buffer, responseBody);
105 function secondTimeThrough(request, buffer) {
106   Assert.equal(buffer, responseBody);
107   httpServer.stop(do_test_finished);