fune/netwerk/test/unit/test_cache2-29c-concurrent_read_half-interrupted.js
Kris Maglione 918ed6c474 Bug 1431533: Part 5a - Auto-rewrite code to use ChromeUtils import methods. r=florian
This was done using the following script:
37e3803c7a/processors/chromeutils-import.jsm

MozReview-Commit-ID: 1Nc3XDu0wGl

--HG--
extra : source : 12fc4dee861c812fd2bd032c63ef17af61800c70
extra : intermediate-source : 34c999fa006bffe8705cf50c54708aa21a962e62
extra : histedit_source : b2be2c5e5d226e6c347312456a6ae339c1e634b0
2018-01-29 15:20:18 -08:00

92 lines
4.2 KiB
JavaScript

/*
Checkes if the concurrent cache read/write works when the write is interrupted because of max-entry-size limits.
This is enhancement of 29a test, this test checks that cocurrency is resumed when the first channel is interrupted
in the middle of reading and the second channel already consumed some content from the cache entry.
This test is using a resumable response.
- with a profile, set max-entry-size to 1 (=1024 bytes)
- first channel makes a request for a resumable response
- second channel makes a request for the same resource, concurrent read happens
- first channel sets predicted data size on the entry with every chunk, it's doomed on 1024
- second channel now must engage interrupted concurrent write algorithm and read the rest of the content from the network
- both channels must deliver full content w/o errors
*/
ChromeUtils.import("resource://testing-common/httpd.js");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyGetter(this, "URL", function() {
return "http://localhost:" + httpServer.identity.primaryPort;
});
var httpServer = null;
function make_channel(url, callback, ctx) {
return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true});
}
// need something bigger than 1024 bytes
const responseBody =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
function contentHandler(metadata, response)
{
response.setHeader("Content-Type", "text/plain");
response.setHeader("ETag", "Just testing");
response.setHeader("Cache-Control", "max-age=99999");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Length", "" + responseBody.length);
if (metadata.hasHeader("If-Range")) {
response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
let len = responseBody.length;
response.setHeader("Content-Range", "0-" + (len - 1) + "/" + len);
}
response.bodyOutputStream.write(responseBody, responseBody.length);
}
function run_test()
{
// Static check
Assert.ok(responseBody.length > 1024);
do_get_profile();
Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1);
Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
httpServer = new HttpServer();
httpServer.registerPathHandler("/content", contentHandler);
httpServer.start(-1);
var chan1 = make_channel(URL + "/content");
chan1.asyncOpen2(new ChannelListener(firstTimeThrough, null));
var chan2 = make_channel(URL + "/content");
chan2.asyncOpen2(new ChannelListener(secondTimeThrough, null));
do_test_pending();
}
function firstTimeThrough(request, buffer)
{
Assert.equal(buffer, responseBody);
}
function secondTimeThrough(request, buffer)
{
Assert.equal(buffer, responseBody);
httpServer.stop(do_test_finished);
}