Change RepoSync to use -j4 option.  Add tests to check for retries.

BUG=chromium-os:13647
TEST=Ran cbuildbot locally to verify retries and increased speed.

Review URL: http://codereview.chromium.org/6771036

(cherry picked from commit 4057aa211eb3027fe0d1565e5428ac656e5bb2b0)

Change-Id: I6f3996bdcc76b6e836bb6ddc9d861545f341f545
Reviewed-on: http://gerrit.chromium.org/gerrit/1705
Reviewed-by: Nick Sanders <nsanders@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/buildbot/cbuildbot_commands.py b/buildbot/cbuildbot_commands.py
index cd172d6..619bd67 100644
--- a/buildbot/cbuildbot_commands.py
+++ b/buildbot/cbuildbot_commands.py
@@ -53,10 +53,7 @@
   """
   while retries > 0:
     try:
-      # The --trace option ensures that repo shows the output from git. This
-      # is needed so that the buildbot can kill us if git is not making
-      # progress.
-      cros_lib.OldRunCommand(['repo', '--trace', 'sync'], cwd=buildroot)
+      cros_lib.OldRunCommand(['repo', 'sync', '-q', '--jobs=4'], cwd=buildroot)
       cros_lib.OldRunCommand(
           ['repo',
            'forall',
diff --git a/buildbot/cbuildbot_commands_unittest.py b/buildbot/cbuildbot_commands_unittest.py
index 5ae8ecc..099f630 100755
--- a/buildbot/cbuildbot_commands_unittest.py
+++ b/buildbot/cbuildbot_commands_unittest.py
@@ -109,6 +109,54 @@
                              binhosts, 'chrome', 'tot')
     self.mox.VerifyAll()
 
+  def testRepoSyncRetriesFail(self):
+    """Case where we exceed default retry attempts."""
+    cros_lib.OldRunCommand(mox.In('sync'),
+                           cwd=self._buildroot).AndRaise(Exception("failed"))
+    cros_lib.OldRunCommand(mox.In('sync'),
+                            cwd=self._buildroot).AndRaise(Exception("failed"))
+    cros_lib.OldRunCommand(mox.In('sync'),
+                           cwd=self._buildroot).AndRaise(Exception("failed"))
+    self.mox.ReplayAll()
+    self.assertRaises(Exception, lambda: commands._RepoSync(self._buildroot))
+    self.mox.VerifyAll()
+
+  def testRepoSyncRetriesPass(self):
+    """Case where we fail twice and pass on the third try."""
+    cros_lib.OldRunCommand(mox.In('sync'),
+                           cwd=self._buildroot).AndRaise(Exception("failed"))
+    cros_lib.OldRunCommand(mox.In('sync'),
+                           cwd=self._buildroot).AndRaise(Exception("failed"))
+    cros_lib.OldRunCommand(mox.In('sync'), cwd=self._buildroot)
+    cros_lib.OldRunCommand(mox.In('forall'), cwd=self._buildroot)
+    cros_lib.OldRunCommand(mox.In('manifest'), cwd=self._buildroot)
+    self.mox.ReplayAll()
+    commands._RepoSync(self._buildroot)
+    self.mox.VerifyAll()
+
+  def testRepoSyncCustomRetriesFail(self):
+    """Case where _RepoSync is called with a custom retry value of 1.  Should
+    throw exception after first failure."""
+    cros_lib.OldRunCommand(mox.In('sync'),
+                           cwd=self._buildroot).AndRaise(Exception("failed"))
+    self.mox.ReplayAll()
+    self.assertRaises(
+        Exception,
+        lambda: commands._RepoSync(self._buildroot, retries=1))
+    self.mox.VerifyAll()
+
+  def testRepoSyncCustomRetriesPass(self):
+    """Case where _RepoSync is called with a custom retry value of 2 and passes
+    the second time."""
+    cros_lib.OldRunCommand(mox.In('sync'),
+                           cwd=self._buildroot).AndRaise(Exception("failed"))
+    cros_lib.OldRunCommand(mox.In('sync'), cwd=self._buildroot)
+    cros_lib.OldRunCommand(mox.In('forall'), cwd=self._buildroot)
+    cros_lib.OldRunCommand(mox.In('manifest'), cwd=self._buildroot)
+    self.mox.ReplayAll()
+    commands._RepoSync(self._buildroot, retries=2)
+    self.mox.VerifyAll()
+
 
 if __name__ == '__main__':
   unittest.main()