| Index: Tools/GardeningServer/scripts/network-simulator.html
|
| diff --git a/Tools/GardeningServer/model/ct-failure-group.html b/Tools/GardeningServer/scripts/network-simulator.html
|
| similarity index 10%
|
| copy from Tools/GardeningServer/model/ct-failure-group.html
|
| copy to Tools/GardeningServer/scripts/network-simulator.html
|
| index 84d01dc9177f9b3b34157482aece5da39e030ff7..9583bdc846b0f30f2507d08d90b38109e8fa5268 100644
|
| --- a/Tools/GardeningServer/model/ct-failure-group.html
|
| +++ b/Tools/GardeningServer/scripts/network-simulator.html
|
| @@ -5,59 +5,81 @@ found in the LICENSE file.
|
| -->
|
|
|
| <script>
|
| -function CTFailureGroup(key, failures, annotation) {
|
| - this.key = key;
|
| - this.failures = failures;
|
| - this.annotation = annotation || {};
|
| - this._computeProperties();
|
| +function NetworkSimulator(assert, done) {
|
| + this._assert = assert;
|
| + this._done = done;
|
| + this._pendingPromises = [];
|
| }
|
|
|
| -CTFailureGroup.prototype.snoozeUntil = function(time) {
|
| - return this._annotate({
|
| - snoozeTime: time,
|
| - });
|
| +NetworkSimulator._testInProgress = false;
|
| +
|
| +NetworkSimulator.prototype.schedulePromise = function(promise) {
|
| + this._pendingPromises.push(promise);
|
| + return promise;
|
| };
|
|
|
| -CTFailureGroup.prototype.unsnooze = function() {
|
| - return this._annotate({
|
| - snoozeTime: undefined,
|
| +NetworkSimulator.prototype.resolvePromises = function() {
|
| + var self = this;
|
| + return new Promise(function(resolve, reject) {
|
| + var pendingPromises = self._pendingPromises;
|
| + self._pendingPromises = [];
|
| + function allResolved(results) {
|
| + if (self._pendingPromises.length) {
|
| + resolve(self.resolvePromises());
|
| + return;
|
| + }
|
| + resolve(results);
|
| + }
|
| + Promise.all(pendingPromises).then(allResolved, allResolved);
|
| });
|
| };
|
|
|
| -CTFailureGroup.prototype._computeProperties = function() {
|
| - this.isSnoozed = Date.now() < this.annotation.snoozeTime;
|
| -};
|
| +NetworkSimulator.prototype.runTest = function(testCase) {
|
| + if (NetworkSimulator._testInProgress) {
|
| + this._assert(false, "runTest calls cannot be nested");
|
| + this._done();
|
| + return;
|
| + }
|
|
|
| -CTFailureGroup.prototype._annotate = function(newAnnotation) {
|
| - // FIXME: Post the new annotation to frontend rather than storing locally.
|
| - return CTFailureGroup.fetchAnnotations().then(function(annotations) {
|
| - var annotation = annotations[this.key] || {};
|
| + NetworkSimulator._testInProgress = true;
|
|
|
| - Object.keys(newAnnotation, function(key, value) {
|
| - if (value === 'undefined') {
|
| - delete annotation[key];
|
| - } else {
|
| - annotation[key] = value;
|
| - }
|
| - });
|
| + var self = this;
|
| + return new Promise(function(resolve, reject) {
|
| + var realNet = window.net;
|
|
|
| - if (Object.size(annotation) == 0) {
|
| - delete annotations[this.key];
|
| - } else {
|
| - annotations[this.key] = annotation;
|
| + function reset() {
|
| + window.net = realNet;
|
| + NetworkSimulator._testInProgress = false;
|
| }
|
|
|
| - localStorage.CTFailureGroupAnnotations = JSON.stringify(annotations);
|
| + // All net.* methods should return promises. This watches all
|
| + // promises generated by test-overridden methods.
|
| + window.net = {};
|
| + ['probe', 'jsonp', 'get', 'post',
|
| + 'ajax', 'json', 'xml'].forEach(function(method) {
|
| + if (method in self) {
|
| + net[method] = function() {
|
| + return self.schedulePromise(self[method].apply(self, arguments));
|
| + };
|
| + };
|
| + });
|
|
|
| - this.annotation = annotation;
|
| - this._computeProperties();
|
| - }.bind(this));
|
| -};
|
| + try {
|
| + testCase();
|
| + } catch(e) {
|
| + // Make sure errors thrown in the test case don't leave window.net in a bad state.
|
| + reset();
|
| + self._assert(false, "Test case threw an error:" + e);
|
| + }
|
|
|
| -CTFailureGroup.fetchAnnotations = function() {
|
| - // FIXME: Fetch annotations from frontend.
|
| - var stored = localStorage.CTFailureGroupAnnotations;
|
| - var annotations = stored ? JSON.parse(stored) : {};
|
| - return Promise.resolve(annotations);
|
| + self.resolvePromises().then(function() {
|
| + reset();
|
| + self._assert(window.net == realNet);
|
| + resolve();
|
| + }).catch(function(e) {
|
| + reset();
|
| + self._assert(false, "Failed to finish test: " + e);
|
| + });
|
| + });
|
| };
|
| </script>
|
|
|