1616
1717DEBUG = True
1818
19+ lock = threading .Lock ()
20+
1921class RunTestsThread (threading .Thread ):
2022 def __init__ (self , group = None , target = None , name = None , args = (), kwargs = None , verbose = None , adbClient = None , testClass = None , testRunner = None ):
2123 threading .Thread .__init__ (self , group = group , target = target , name = name , verbose = verbose )
@@ -24,11 +26,18 @@ def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, ver
2426 self .testRunner = testRunner
2527
2628 def run (self ):
29+ lock .acquire ()
30+ pkg = re .sub ('\.test$' , '' , self .testClass )
31+ if DEBUG :
32+ print >> sys .stderr , "Cleaning up before start. Stopping '%s'" % pkg
33+ self .adbClient .shell ('am force-stop ' + pkg )
34+ time .sleep (3 )
2735 if DEBUG :
2836 print >> sys .stderr , "Starting test..."
37+ lock .release ()
2938 out = self .adbClient .shell ('am instrument -w ' + self .testClass + '/' + self .testRunner + '; echo "ERROR: $?"' )
3039 if DEBUG :
31- print >> sys .stderr , "Finished test."
40+ print >> sys .stderr , "\n Finished test."
3241 errmsg = out .splitlines ()[- 1 ]
3342 m = re .match ('ERROR: (\d+)' , errmsg )
3443 if m :
@@ -64,9 +73,11 @@ def __init__(self, adbclient, adb=None, localport=9999, remoteport=9999, hostnam
6473 self .__connectToServer (hostname , localport )
6574
6675 def __connectToServer (self , hostname , port ):
76+ lock .acquire ()
6777 self .conn = httplib .HTTPConnection (hostname , port )
6878 if not self .conn :
6979 raise RuntimeError ("Cannot connect to %s:%d" % (hostname , port ))
80+ lock .release ()
7081
7182 def __whichAdb (self , adb ):
7283 if adb :
@@ -91,31 +102,37 @@ def __runTests(self):
91102 # We need a new AdbClient instance with timeout=None (means, no timeout) for the long running test service
92103 newAdbClient = AdbClient (self .adbClient .serialno , self .adbClient .hostname , self .adbClient .port , timeout = None )
93104 self .thread = RunTestsThread (adbClient = newAdbClient , testClass = self .TEST_CLASS , testRunner = self .TEST_RUNNER )
105+ if DEBUG :
106+ print >> sys .stderr , "__runTests: starting thread"
94107 self .thread .start ()
95108 if DEBUG :
96109 print >> sys .stderr , "__runTests: end"
97110
98111
99112 def __httpCommand (self , url , method = 'GET' ):
100113 if self .isDarwin :
101- #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
102- #!! The connection cannot be resued in OSX, it gives:
103- #!! response = conn.getresponse()
104- #!! File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1045, in getresponse
105- #!! response.begin()
106- #!! File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 409, in begin
107- #!! version, status, reason = self._read_status()
108- #!! File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 373, in _read_status
109- #!! raise BadStatusLine(line)
110- #!! httplib.BadStatusLine: ''
111- #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
114+ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
115+ # !! The connection cannot be resued in OSX, it gives:
116+ # !! response = conn.getresponse()
117+ # !! File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1045, in getresponse
118+ # !! response.begin()
119+ # !! File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 409, in begin
120+ # !! version, status, reason = self._read_status()
121+ # !! File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 373, in _read_status
122+ # !! raise BadStatusLine(line)
123+ # !! httplib.BadStatusLine: ''
124+ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
112125 self .__connectToServer (self .hostname , self .localPort )
113126 time .sleep (3 )
114127 self .conn .request (method , url )
115- response = self .conn .getresponse ()
116- if response .status == 200 :
117- return response .read ()
118- raise RuntimeError (response .status + " " + response .reason + " while " + method + " " + url )
128+ try :
129+ response = self .conn .getresponse ()
130+ if response .status == 200 :
131+ return response .read ()
132+ raise RuntimeError (response .status + " " + response .reason + " while " + method + " " + url )
133+ except httplib .BadStatusLine , ex :
134+ print >> sys .stderr , 'ERROR:' , ex
135+ return u''
119136
120137 #
121138 # UiAutomatorHelper internal commands
0 commit comments