toolkit/devtools/server/tests/unit/test_protocol_async.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /**
michael@0 5 * Make sure we get replies in the same order that we sent their
michael@0 6 * requests even when earlier requests take several event ticks to
michael@0 7 * complete.
michael@0 8 */
michael@0 9
michael@0 10 let protocol = devtools.require("devtools/server/protocol");
michael@0 11 let {method, Arg, Option, RetVal} = protocol;
michael@0 12 let events = devtools.require("sdk/event/core");
michael@0 13
michael@0 14 function simpleHello() {
michael@0 15 return {
michael@0 16 from: "root",
michael@0 17 applicationType: "xpcshell-tests",
michael@0 18 traits: [],
michael@0 19 }
michael@0 20 }
michael@0 21
michael@0 22 let RootActor = protocol.ActorClass({
michael@0 23 typeName: "root",
michael@0 24 initialize: function(conn) {
michael@0 25 protocol.Actor.prototype.initialize.call(this, conn);
michael@0 26 // Root actor owns itself.
michael@0 27 this.manage(this);
michael@0 28 this.actorID = "root";
michael@0 29 this.sequence = 0;
michael@0 30 },
michael@0 31
michael@0 32 sayHello: simpleHello,
michael@0 33
michael@0 34 simpleReturn: method(function() {
michael@0 35 return this.sequence++;
michael@0 36 }, {
michael@0 37 response: { value: RetVal() },
michael@0 38 }),
michael@0 39
michael@0 40 promiseReturn: method(function(toWait) {
michael@0 41 // Guarantee that this resolves after simpleReturn returns.
michael@0 42 let deferred = promise.defer();
michael@0 43 let sequence = this.sequence++;
michael@0 44
michael@0 45 // Wait until the number of requests specified by toWait have
michael@0 46 // happened, to test queuing.
michael@0 47 let check = () => {
michael@0 48 if ((this.sequence - sequence) < toWait) {
michael@0 49 do_execute_soon(check);
michael@0 50 return;
michael@0 51 }
michael@0 52 deferred.resolve(sequence);
michael@0 53 }
michael@0 54 do_execute_soon(check);
michael@0 55
michael@0 56 return deferred.promise;
michael@0 57 }, {
michael@0 58 request: { toWait: Arg(0, "number") },
michael@0 59 response: { value: RetVal("number") },
michael@0 60 }),
michael@0 61
michael@0 62 simpleThrow: method(function() {
michael@0 63 throw new Error(this.sequence++);
michael@0 64 }, {
michael@0 65 response: { value: RetVal("number") }
michael@0 66 }),
michael@0 67
michael@0 68 promiseThrow: method(function() {
michael@0 69 // Guarantee that this resolves after simpleReturn returns.
michael@0 70 let deferred = promise.defer();
michael@0 71 let sequence = this.sequence++;
michael@0 72 // This should be enough to force a failure if the code is broken.
michael@0 73 do_timeout(150, () => {
michael@0 74 deferred.reject(sequence++);
michael@0 75 });
michael@0 76 return deferred.promise;
michael@0 77 }, {
michael@0 78 response: { value: RetVal("number") },
michael@0 79 })
michael@0 80 });
michael@0 81
michael@0 82 let RootFront = protocol.FrontClass(RootActor, {
michael@0 83 initialize: function(client) {
michael@0 84 this.actorID = "root";
michael@0 85 protocol.Front.prototype.initialize.call(this, client);
michael@0 86 // Root owns itself.
michael@0 87 this.manage(this);
michael@0 88 }
michael@0 89 });
michael@0 90
michael@0 91 function run_test()
michael@0 92 {
michael@0 93 DebuggerServer.createRootActor = RootActor;
michael@0 94 DebuggerServer.init(() => true);
michael@0 95
michael@0 96 let trace = connectPipeTracing();
michael@0 97 let client = new DebuggerClient(trace);
michael@0 98 let rootClient;
michael@0 99
michael@0 100 client.connect((applicationType, traits) => {
michael@0 101 rootClient = RootFront(client);
michael@0 102
michael@0 103 let calls = [];
michael@0 104 let sequence = 0;
michael@0 105
michael@0 106 // Execute a call that won't finish processing until 2
michael@0 107 // more calls have happened
michael@0 108 calls.push(rootClient.promiseReturn(2).then(ret => {
michael@0 109 do_check_eq(sequence, 0); // Check right return order
michael@0 110 do_check_eq(ret, sequence++); // Check request handling order
michael@0 111 }));
michael@0 112
michael@0 113 // Put a few requests into the backlog
michael@0 114
michael@0 115 calls.push(rootClient.simpleReturn().then(ret => {
michael@0 116 do_check_eq(sequence, 1); // Check right return order
michael@0 117 do_check_eq(ret, sequence++); // Check request handling order
michael@0 118 }));
michael@0 119
michael@0 120 calls.push(rootClient.simpleReturn().then(ret => {
michael@0 121 do_check_eq(sequence, 2); // Check right return order
michael@0 122 do_check_eq(ret, sequence++); // Check request handling order
michael@0 123 }));
michael@0 124
michael@0 125 calls.push(rootClient.simpleThrow().then(() => {
michael@0 126 do_check_true(false, "simpleThrow shouldn't succeed!");
michael@0 127 }, error => {
michael@0 128 do_check_eq(sequence++, 3); // Check right return order
michael@0 129 }));
michael@0 130
michael@0 131 // While packets are sent in the correct order, rejection handlers
michael@0 132 // registered in "Promise.jsm" may be invoked later than fulfillment
michael@0 133 // handlers, meaning that we can't check the actual order with certainty.
michael@0 134 let deferAfterRejection = promise.defer();
michael@0 135
michael@0 136 calls.push(rootClient.promiseThrow().then(() => {
michael@0 137 do_check_true(false, "promiseThrow shouldn't succeed!");
michael@0 138 }, error => {
michael@0 139 do_check_eq(sequence++, 4); // Check right return order
michael@0 140 do_check_true(true, "simple throw should throw");
michael@0 141 deferAfterRejection.resolve();
michael@0 142 }));
michael@0 143
michael@0 144 calls.push(rootClient.simpleReturn().then(ret => {
michael@0 145 return deferAfterRejection.promise.then(function () {
michael@0 146 do_check_eq(sequence, 5); // Check right return order
michael@0 147 do_check_eq(ret, sequence++); // Check request handling order
michael@0 148 });
michael@0 149 }));
michael@0 150
michael@0 151 // Break up the backlog with a long request that waits
michael@0 152 // for another simpleReturn before completing
michael@0 153 calls.push(rootClient.promiseReturn(1).then(ret => {
michael@0 154 return deferAfterRejection.promise.then(function () {
michael@0 155 do_check_eq(sequence, 6); // Check right return order
michael@0 156 do_check_eq(ret, sequence++); // Check request handling order
michael@0 157 });
michael@0 158 }));
michael@0 159
michael@0 160 calls.push(rootClient.simpleReturn().then(ret => {
michael@0 161 return deferAfterRejection.promise.then(function () {
michael@0 162 do_check_eq(sequence, 7); // Check right return order
michael@0 163 do_check_eq(ret, sequence++); // Check request handling order
michael@0 164 });
michael@0 165 }));
michael@0 166
michael@0 167 promise.all(calls).then(() => {
michael@0 168 client.close(() => {
michael@0 169 do_test_finished();
michael@0 170 });
michael@0 171 })
michael@0 172 });
michael@0 173 do_test_pending();
michael@0 174 }

mercurial