michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Make sure we get replies in the same order that we sent their michael@0: * requests even when earlier requests take several event ticks to michael@0: * complete. michael@0: */ michael@0: michael@0: let protocol = devtools.require("devtools/server/protocol"); michael@0: let {method, Arg, Option, RetVal} = protocol; michael@0: let events = devtools.require("sdk/event/core"); michael@0: michael@0: function simpleHello() { michael@0: return { michael@0: from: "root", michael@0: applicationType: "xpcshell-tests", michael@0: traits: [], michael@0: } michael@0: } michael@0: michael@0: let RootActor = protocol.ActorClass({ michael@0: typeName: "root", michael@0: initialize: function(conn) { michael@0: protocol.Actor.prototype.initialize.call(this, conn); michael@0: // Root actor owns itself. michael@0: this.manage(this); michael@0: this.actorID = "root"; michael@0: this.sequence = 0; michael@0: }, michael@0: michael@0: sayHello: simpleHello, michael@0: michael@0: simpleReturn: method(function() { michael@0: return this.sequence++; michael@0: }, { michael@0: response: { value: RetVal() }, michael@0: }), michael@0: michael@0: promiseReturn: method(function(toWait) { michael@0: // Guarantee that this resolves after simpleReturn returns. michael@0: let deferred = promise.defer(); michael@0: let sequence = this.sequence++; michael@0: michael@0: // Wait until the number of requests specified by toWait have michael@0: // happened, to test queuing. michael@0: let check = () => { michael@0: if ((this.sequence - sequence) < toWait) { michael@0: do_execute_soon(check); michael@0: return; michael@0: } michael@0: deferred.resolve(sequence); michael@0: } michael@0: do_execute_soon(check); michael@0: michael@0: return deferred.promise; michael@0: }, { michael@0: request: { toWait: Arg(0, "number") }, michael@0: response: { value: RetVal("number") }, michael@0: }), michael@0: michael@0: simpleThrow: method(function() { michael@0: throw new Error(this.sequence++); michael@0: }, { michael@0: response: { value: RetVal("number") } michael@0: }), michael@0: michael@0: promiseThrow: method(function() { michael@0: // Guarantee that this resolves after simpleReturn returns. michael@0: let deferred = promise.defer(); michael@0: let sequence = this.sequence++; michael@0: // This should be enough to force a failure if the code is broken. michael@0: do_timeout(150, () => { michael@0: deferred.reject(sequence++); michael@0: }); michael@0: return deferred.promise; michael@0: }, { michael@0: response: { value: RetVal("number") }, michael@0: }) michael@0: }); michael@0: michael@0: let RootFront = protocol.FrontClass(RootActor, { michael@0: initialize: function(client) { michael@0: this.actorID = "root"; michael@0: protocol.Front.prototype.initialize.call(this, client); michael@0: // Root owns itself. michael@0: this.manage(this); michael@0: } michael@0: }); michael@0: michael@0: function run_test() michael@0: { michael@0: DebuggerServer.createRootActor = RootActor; michael@0: DebuggerServer.init(() => true); michael@0: michael@0: let trace = connectPipeTracing(); michael@0: let client = new DebuggerClient(trace); michael@0: let rootClient; michael@0: michael@0: client.connect((applicationType, traits) => { michael@0: rootClient = RootFront(client); michael@0: michael@0: let calls = []; michael@0: let sequence = 0; michael@0: michael@0: // Execute a call that won't finish processing until 2 michael@0: // more calls have happened michael@0: calls.push(rootClient.promiseReturn(2).then(ret => { michael@0: do_check_eq(sequence, 0); // Check right return order michael@0: do_check_eq(ret, sequence++); // Check request handling order michael@0: })); michael@0: michael@0: // Put a few requests into the backlog michael@0: michael@0: calls.push(rootClient.simpleReturn().then(ret => { michael@0: do_check_eq(sequence, 1); // Check right return order michael@0: do_check_eq(ret, sequence++); // Check request handling order michael@0: })); michael@0: michael@0: calls.push(rootClient.simpleReturn().then(ret => { michael@0: do_check_eq(sequence, 2); // Check right return order michael@0: do_check_eq(ret, sequence++); // Check request handling order michael@0: })); michael@0: michael@0: calls.push(rootClient.simpleThrow().then(() => { michael@0: do_check_true(false, "simpleThrow shouldn't succeed!"); michael@0: }, error => { michael@0: do_check_eq(sequence++, 3); // Check right return order michael@0: })); michael@0: michael@0: // While packets are sent in the correct order, rejection handlers michael@0: // registered in "Promise.jsm" may be invoked later than fulfillment michael@0: // handlers, meaning that we can't check the actual order with certainty. michael@0: let deferAfterRejection = promise.defer(); michael@0: michael@0: calls.push(rootClient.promiseThrow().then(() => { michael@0: do_check_true(false, "promiseThrow shouldn't succeed!"); michael@0: }, error => { michael@0: do_check_eq(sequence++, 4); // Check right return order michael@0: do_check_true(true, "simple throw should throw"); michael@0: deferAfterRejection.resolve(); michael@0: })); michael@0: michael@0: calls.push(rootClient.simpleReturn().then(ret => { michael@0: return deferAfterRejection.promise.then(function () { michael@0: do_check_eq(sequence, 5); // Check right return order michael@0: do_check_eq(ret, sequence++); // Check request handling order michael@0: }); michael@0: })); michael@0: michael@0: // Break up the backlog with a long request that waits michael@0: // for another simpleReturn before completing michael@0: calls.push(rootClient.promiseReturn(1).then(ret => { michael@0: return deferAfterRejection.promise.then(function () { michael@0: do_check_eq(sequence, 6); // Check right return order michael@0: do_check_eq(ret, sequence++); // Check request handling order michael@0: }); michael@0: })); michael@0: michael@0: calls.push(rootClient.simpleReturn().then(ret => { michael@0: return deferAfterRejection.promise.then(function () { michael@0: do_check_eq(sequence, 7); // Check right return order michael@0: do_check_eq(ret, sequence++); // Check request handling order michael@0: }); michael@0: })); michael@0: michael@0: promise.all(calls).then(() => { michael@0: client.close(() => { michael@0: do_test_finished(); michael@0: }); michael@0: }) michael@0: }); michael@0: do_test_pending(); michael@0: }