From 45b8bba6a28bfcde3dffa35c2f4964af1aa6e42c Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Wed, 4 Jul 2012 11:52:37 +0200
Subject: [PATCH] Use erp5.tool.taskdistribution in runTestSuite.

---
 product/ERP5Type/tests/runTestSuite.py | 85 ++++++--------------------
 1 file changed, 18 insertions(+), 67 deletions(-)

diff --git a/product/ERP5Type/tests/runTestSuite.py b/product/ERP5Type/tests/runTestSuite.py
index fb0b5378d7..93fa781336 100644
--- a/product/ERP5Type/tests/runTestSuite.py
+++ b/product/ERP5Type/tests/runTestSuite.py
@@ -1,35 +1,14 @@
 #!/usr/bin/env python2.6
-import argparse, pprint, socket, sys, time, xmlrpclib
-from DummyTaskDistributionTool import DummyTaskDistributionTool
-from ERP5TypeTestSuite import ERP5TypeTestSuite
+import argparse, sys
+from erp5.util import taskdistribution
 
-# XXX: below section is shared with erp5/util/testnode/testnode.py .
-# They are supposed to be merged into a common library/tool someday, until
-# then please keep them synchronised.
-# Depending on used xmlrpc backend, different exceptions can be thrown.
-SAFE_RPC_EXCEPTION_LIST = [socket.error, xmlrpclib.ProtocolError, xmlrpclib.Fault]
-parser, _ = xmlrpclib.getparser()
-if xmlrpclib.ExpatParser and isinstance(parser, xmlrpclib.ExpatParser):
-  SAFE_RPC_EXCEPTION_LIST.append(xmlrpclib.expat.ExpatError)
-else:
-  print >>sys.stderr, 'Warning: unhandled xmlrpclib parser %r, some ' \
-    'exceptions might get through safeRpcCall' % (parser, )
-SAFE_RPC_EXCEPTION_LIST = tuple(SAFE_RPC_EXCEPTION_LIST)
-parser_klass = parser.__class__
-__original_feed = parser_klass.feed
-def verbose_feed(self, data):
-  try:
-    return __original_feed(self, data)
-  except Exception:
-    print >>sys.stderr, 'Error parsing data:', repr(data)
-    raise
-try:
-  parser_klass.feed = verbose_feed
-except TypeError:
-  print >>sys.stderr, 'Warning: could not monkey-patch %r.feed to output ' \
-    'parsed data on error, debugging in case of error will be more ' \
-    'difficult' % (parser_klass, )
-del parser, verbose_feed, parser_klass, _
+# XXX: This import is required, just to populate sys.modules['test_suite'].
+# Even if it's not used in this file. Yuck.
+import ERP5TypeTestSuite
+
+def _parsingErrorHandler(data, _):
+  print >> sys.stderr, 'Error parsing data:', repr(data)
+taskdistribution.patchRPCParser(_parsingErrorHandler)
 
 def makeSuite(node_quantity=None, test_suite=None, revision=None,
               db_list=None, **kwargs):
@@ -56,23 +35,6 @@ def makeSuite(node_quantity=None, test_suite=None, revision=None,
                       **kwargs)
   return suite
 
-def safeRpcCall(function, *args):
-  retry = 64
-  xmlrpc_arg_list = []
-  for argument in args:
-    if isinstance(argument, dict):
-      argument = dict([(x, isinstance(y,str) and xmlrpclib.Binary(y) or y) \
-           for (x,y) in argument.iteritems()])
-    xmlrpc_arg_list.append(argument)
-  while True:
-    try:
-      return function(*xmlrpc_arg_list)
-    except SAFE_RPC_EXCEPTION_LIST, e:
-      print >>sys.stderr, e
-      pprint.pprint(args, file(function._Method__name, 'w'))
-      time.sleep(retry)
-      retry += retry >> 1
-
 def main():
   parser = argparse.ArgumentParser(description='Run a test suite.')
   parser.add_argument('--test_suite', help='The test suite name')
@@ -102,16 +64,7 @@ def main():
   args = parser.parse_args()
   if args.bt5_path is not None:
     sys.path[0:0] = args.bt5_path.split(",")
-  if args.master_url is not None:
-    master_url = args.master_url
-    if master_url[-1] != '/':
-      master_url += '/'
-    master = xmlrpclib.ServerProxy("%s%s" %
-              (master_url, 'portal_task_distribution'),
-              allow_none=1)
-    assert master.getProtocolRevision() == 1
-  else:
-    master = DummyTaskDistributionTool()
+  master = taskdistribution.TaskDistributionTool(args.master_url)
   test_suite_title = args.test_suite_title or args.test_suite
   revision = args.revision
   suite = makeSuite(test_suite=args.test_suite,
@@ -119,17 +72,15 @@ def main():
                     revision=revision,
                     db_list=args.db_list,
                     bt5_path=args.bt5_path)
-  test_result = safeRpcCall(master.createTestResult,
-    args.test_suite, revision, suite.getTestList(),
-    suite.allow_restart, test_suite_title, args.test_node_title,
+  test_result = master.createTestResult(revision, suite.getTestList(),
+    args.test_node_title, suite.allow_restart, test_suite_title,
     args.project_title)
-  if test_result:
-    test_result_path, test_revision = test_result
+  if test_result is not None:
+    assert revision == test_result.revision, (revision, test_result.revision)
     while suite.acquire():
-      test = safeRpcCall(master.startUnitTest, test_result_path,
-                          suite.running.keys())
-      if test:
-        suite.start(test[1], lambda status_dict, __test_path=test[0]:
-          safeRpcCall(master.stopUnitTest, __test_path, status_dict))
+      test = test_result.start(suite.running.keys())
+      if test is not None:
+        suite.start(test.name, lambda status_dict, __test=test:
+          test.stop(**status_dict))
       elif not suite.running:
         break
-- 
2.30.9