Refactor compareIterations to remove duplicate code.
authorjmarcell@apple.com <jmarcell@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 21 Jan 2016 00:20:33 +0000 (00:20 +0000)
committerjmarcell@apple.com <jmarcell@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 21 Jan 2016 00:20:33 +0000 (00:20 +0000)
https://bugs.webkit.org/show_bug.cgi?id=152913

Reviewed by Daniel Bates.

* BuildSlaveSupport/build.webkit.org-config/public_html/dashboard/Scripts/BuildbotQueue.js:
(BuildbotQueue.prototype.compareIterations): Refactored to remove duplicate code.
(BuildbotQueue.prototype.sortIterations): Add binding to call to compareIterations.
* BuildSlaveSupport/build.webkit.org-config/public_html/dashboard/Scripts/tests/tests.js: Added tests in order to ensure
the same behavior before and after refactor.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@195391 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/BuildSlaveSupport/build.webkit.org-config/public_html/dashboard/Scripts/BuildbotQueue.js
Tools/BuildSlaveSupport/build.webkit.org-config/public_html/dashboard/Scripts/tests/tests.js
Tools/ChangeLog

index e4a9951..77198d0 100644 (file)
@@ -268,13 +268,9 @@ BuildbotQueue.prototype = {
 
     compareIterations: function(a, b)
     {
-        var sortedRepositories = Dashboard.sortedRepositories;
-        for (var i = 0; i < sortedRepositories.length; ++i) {
-            var repositoryName = sortedRepositories[i].name;
-            var result = b.revision[repositoryName] - a.revision[repositoryName];
-            if (result)
-                return result;
-        }
+        result = this.compareIterationsByRevisions(a, b);
+        if (result)
+            return result;
 
         // A loaded iteration may not have revision numbers if it failed early, before svn steps finished.
         result = b.loaded - a.loaded;
@@ -299,6 +295,6 @@ BuildbotQueue.prototype = {
 
     sortIterations: function()
     {
-        this.iterations.sort(this.compareIterations);
+        this.iterations.sort(this.compareIterations.bind(this));
     }
 };
index 1934008..4a68313 100644 (file)
@@ -42,3 +42,97 @@ test("_appendPendingRevisionCount", function()
     var revisionsBehind = view.element.getElementsByClassName("message")[0].innerHTML.match(/.*(\d+) revision(|s) behind/)[1];
     equal(revisionsBehind, "1", "assert revisions behind");
 });
+
+module("BuildBotQueue", {
+    setup: function() {
+        this.queue = new MockBuildbotQueue();
+        this.queue.branches = [{
+            name: "trunk",
+            repository: {
+                name: "openSource",
+            }
+        }];
+    }
+});
+
+test("compareIterations by revisions", function()
+{
+    var finished = false;
+    var iteration1 = new BuildbotIteration(this.queue, 1, finished);
+    var iteration2 = new BuildbotIteration(this.queue, 2, finished);
+    iteration1.revision = { "openSource": 33018 };
+    iteration2.revision = { "openSource": 33019 };
+    iteration1.loaded = true;
+    iteration2.loaded = true;
+    ok(this.queue.compareIterations(iteration2, iteration1) < 0, "compareIterations: less than");
+    ok(this.queue.compareIterations(iteration1, iteration2) > 0, "compareIterations: greater than");
+    strictEqual(this.queue.compareIterations(iteration2, iteration2), 0, "compareIterations: equal");
+});
+
+test("compareIterations by loaded (one revision missing)", function()
+{
+    var finished = false;
+    var iteration1 = new BuildbotIteration(this.queue, 1, finished);
+    var iteration2 = new BuildbotIteration(this.queue, 2, finished);
+    iteration1.revision = {};
+    iteration2.revision = { "openSource": 33019 };
+    iteration1.loaded = false;
+    iteration2.loaded = true;
+    ok(this.queue.compareIterations(iteration1, iteration2) > 0, "compareIterations: greater than");
+    ok(this.queue.compareIterations(iteration2, iteration1) < 0, "compareIterations: less than");
+});
+
+test("compareIterations by loaded (same revision)", function()
+{
+    var finished = false;
+    var iteration1 = new BuildbotIteration(this.queue, 1, finished);
+    var iteration2 = new BuildbotIteration(this.queue, 2, finished);
+    iteration1.revision = { "openSource": 33019 };
+    iteration2.revision = { "openSource": 33019 };
+    iteration1.loaded = false;
+    iteration2.loaded = true;
+    ok(this.queue.compareIterations(iteration1, iteration2) > 0, "compareIterations: greater than");
+    ok(this.queue.compareIterations(iteration2, iteration1) < 0, "compareIterations: less than");
+});
+
+test("compareIterations by id (revisions not specified)", function()
+{
+    var finished = false;
+    var iteration1 = new BuildbotIteration(this.queue, 1, finished);
+    var iteration2 = new BuildbotIteration(this.queue, 2, finished);
+    iteration1.revision = {};
+    iteration2.revision = {};
+    iteration1.loaded = false;
+    iteration2.loaded = false;
+    ok(this.queue.compareIterations(iteration2, iteration1) < 0, "compareIterations: less than");
+    ok(this.queue.compareIterations(iteration1, iteration2) > 0, "compareIterations: greater than");
+    strictEqual(this.queue.compareIterations(iteration2, iteration2), 0, "compareIterations: equal");
+});
+
+test("compareIterations by id (same revision)", function()
+{
+    var finished = false;
+    var iteration1 = new BuildbotIteration(this.queue, 1, finished);
+    var iteration2 = new BuildbotIteration(this.queue, 2, finished);
+    iteration1.revision = { "openSource": 33019 };
+    iteration2.revision = { "openSource": 33019 };
+    iteration1.loaded = false;
+    iteration2.loaded = false;
+    ok(this.queue.compareIterations(iteration2, iteration1) < 0, "compareIterations: less than");
+    ok(this.queue.compareIterations(iteration1, iteration2) > 0, "compareIterations: greater than");
+    strictEqual(this.queue.compareIterations(iteration2, iteration2), 0, "compareIterations: equal");
+});
+
+test("compareIterationsByRevisions", function()
+{
+    var finished = false;
+    var iteration1 = new BuildbotIteration(this.queue, 1, finished);
+    var iteration2 = new BuildbotIteration(this.queue, 2, finished);
+    iteration1.revision = { "openSource": 33018 };
+    iteration2.revision = { "openSource": 33019 };
+    iteration1.loaded = true;
+    iteration2.loaded = false;
+    ok(this.queue.compareIterationsByRevisions(iteration2, iteration1) < 0, "compareIterationsByRevisions: less than");
+    ok(this.queue.compareIterationsByRevisions(iteration1, iteration2) > 0, "compareIterationsByRevisions: greater than");
+    strictEqual(this.queue.compareIterationsByRevisions(iteration2, iteration2), 0, "compareIterationsByRevisions: equal");
+});
\ No newline at end of file
index 5c408fc..68f05c5 100644 (file)
@@ -1,3 +1,16 @@
+2016-01-20  Jason Marcell  <jmarcell@apple.com>
+
+        Refactor compareIterations to remove duplicate code.
+        https://bugs.webkit.org/show_bug.cgi?id=152913
+
+        Reviewed by Daniel Bates.
+
+        * BuildSlaveSupport/build.webkit.org-config/public_html/dashboard/Scripts/BuildbotQueue.js:
+        (BuildbotQueue.prototype.compareIterations): Refactored to remove duplicate code.
+        (BuildbotQueue.prototype.sortIterations): Add binding to call to compareIterations.
+        * BuildSlaveSupport/build.webkit.org-config/public_html/dashboard/Scripts/tests/tests.js: Added tests in order to ensure
+        the same behavior before and after refactor.
+
 2016-01-20  Dana Burkart  <dburkart@apple.com>
 
         Botwatcher's dashboard should show an 'X' when the build is broken