Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(757)

Unified Diff: Tools/GardeningServer/lib/network-simulator.html

Issue 459463003: Convert moar Sheriff-o-Matic tests to Polymer. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: forgot to add lib/network-simulator.html Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Tools/GardeningServer/karma.conf.js ('k') | Tools/GardeningServer/model/tree-status-tests.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Tools/GardeningServer/lib/network-simulator.html
diff --git a/Tools/GardeningServer/model/ct-failure-group.html b/Tools/GardeningServer/lib/network-simulator.html
similarity index 10%
copy from Tools/GardeningServer/model/ct-failure-group.html
copy to Tools/GardeningServer/lib/network-simulator.html
index 84d01dc9177f9b3b34157482aece5da39e030ff7..9583bdc846b0f30f2507d08d90b38109e8fa5268 100644
--- a/Tools/GardeningServer/model/ct-failure-group.html
+++ b/Tools/GardeningServer/lib/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>
« no previous file with comments | « Tools/GardeningServer/karma.conf.js ('k') | Tools/GardeningServer/model/tree-status-tests.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698