netwerk/test/unit/test_reentrancy.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 Cu.import("resource://testing-common/httpd.js");
michael@0 2
michael@0 3 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 4 return "http://localhost:" + httpserver.identity.primaryPort;
michael@0 5 });
michael@0 6
michael@0 7 var httpserver = new HttpServer();
michael@0 8 var testpath = "/simple";
michael@0 9 var httpbody = "<?xml version='1.0' ?><root>0123456789</root>";
michael@0 10
michael@0 11 function syncXHR()
michael@0 12 {
michael@0 13 var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
michael@0 14 .createInstance(Ci.nsIXMLHttpRequest);
michael@0 15 xhr.open("GET", URL + testpath, false);
michael@0 16 xhr.send(null);
michael@0 17 }
michael@0 18
michael@0 19 const MAX_TESTS = 2;
michael@0 20
michael@0 21 var listener = {
michael@0 22 _done_onStart: false,
michael@0 23 _done_onData: false,
michael@0 24 _test: 0,
michael@0 25
michael@0 26 QueryInterface: function(iid) {
michael@0 27 if (iid.equals(Components.interfaces.nsIStreamListener) ||
michael@0 28 iid.equals(Components.interfaces.nsIRequestObserver) ||
michael@0 29 iid.equals(Components.interfaces.nsISupports))
michael@0 30 return this;
michael@0 31 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 32 },
michael@0 33
michael@0 34 onStartRequest: function(request, ctx) {
michael@0 35 switch(this._test) {
michael@0 36 case 0:
michael@0 37 request.suspend();
michael@0 38 syncXHR();
michael@0 39 request.resume();
michael@0 40 break;
michael@0 41 case 1:
michael@0 42 request.suspend();
michael@0 43 syncXHR();
michael@0 44 do_execute_soon(function() request.resume());
michael@0 45 break;
michael@0 46 case 2:
michael@0 47 do_execute_soon(function() request.suspend());
michael@0 48 do_execute_soon(function() request.resume());
michael@0 49 syncXHR();
michael@0 50 break;
michael@0 51 }
michael@0 52
michael@0 53 this._done_onStart = true;
michael@0 54 },
michael@0 55
michael@0 56 onDataAvailable: function(request, context, stream, offset, count) {
michael@0 57 do_check_true(this._done_onStart);
michael@0 58 read_stream(stream, count);
michael@0 59 this._done_onData = true;
michael@0 60 },
michael@0 61
michael@0 62 onStopRequest: function(request, ctx, status) {
michael@0 63 do_check_true(this._done_onData);
michael@0 64 this._reset();
michael@0 65 if (this._test <= MAX_TESTS)
michael@0 66 next_test();
michael@0 67 else
michael@0 68 httpserver.stop(do_test_finished);
michael@0 69 },
michael@0 70
michael@0 71 _reset: function() {
michael@0 72 this._done_onStart = false;
michael@0 73 this._done_onData = false;
michael@0 74 this._test++;
michael@0 75 }
michael@0 76 };
michael@0 77
michael@0 78 function makeChan(url) {
michael@0 79 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 80 var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
michael@0 81 return chan;
michael@0 82 }
michael@0 83
michael@0 84 function next_test()
michael@0 85 {
michael@0 86 var chan = makeChan(URL + testpath);
michael@0 87 chan.QueryInterface(Ci.nsIRequest);
michael@0 88 chan.asyncOpen(listener, null);
michael@0 89 }
michael@0 90
michael@0 91 function run_test()
michael@0 92 {
michael@0 93 httpserver.registerPathHandler(testpath, serverHandler);
michael@0 94 httpserver.start(-1);
michael@0 95
michael@0 96 next_test();
michael@0 97
michael@0 98 do_test_pending();
michael@0 99 }
michael@0 100
michael@0 101 function serverHandler(metadata, response)
michael@0 102 {
michael@0 103 response.setHeader("Content-Type", "text/xml", false);
michael@0 104 response.bodyOutputStream.write(httpbody, httpbody.length);
michael@0 105 }

mercurial