2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
14 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
15 PARENT_DIR
= os
.path
.dirname(SCRIPT_DIR
)
17 sys
.path
.append(PARENT_DIR
)
22 class HTTPDTest(unittest
.TestCase
):
24 self
.server
= httpd
.LocalHTTPServer('.', 0)
27 self
.server
.Shutdown()
30 urllib2
.urlopen(self
.server
.GetURL('?quit=1'))
31 self
.server
.process
.join(10) # Wait 10 seconds for the process to finish.
32 self
.assertFalse(self
.server
.process
.is_alive())
35 class RunTest(unittest
.TestCase
):
40 if self
.process
and self
.process
.returncode
is None:
44 def _SubprocessThread(process
, queue
):
45 stdout
, stderr
= process
.communicate()
46 queue
.put((process
.returncode
, stdout
, stderr
))
48 def _Run(self
, args
=None, timeout
=None):
50 run_py
= os
.path
.join(PARENT_DIR
, 'run.py')
51 cmd
= [sys
.executable
, run_py
]
53 self
.process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
,
54 stderr
=subprocess
.PIPE
)
56 thread
= threading
.Thread(target
=RunTest
._SubprocessThread
,
57 args
=(self
.process
, queue
))
61 if not thread
.is_alive():
62 returncode
, stdout
, stderr
= queue
.get(False)
63 return returncode
, stdout
, stderr
67 def _GetChromeMockArgs(self
, page
, http_request_type
, sleep
,
68 expect_to_be_killed
=True):
71 args
.extend(['-P', page
])
73 args
.extend([sys
.executable
, os
.path
.join(SCRIPT_DIR
, 'chrome_mock.py')])
75 args
.append('--' + http_request_type
)
77 args
.extend(['--sleep', str(sleep
)])
78 if expect_to_be_killed
:
79 args
.append('--expect-to-be-killed')
83 args
= self
._GetChromeMockArgs
('?quit=1', 'get', sleep
=10)
84 _
, stdout
, _
= self
._Run
(args
, timeout
=20)
85 self
.assertTrue('Starting' in stdout
)
86 self
.assertTrue('Expected to be killed' not in stdout
)
88 def testSubprocessDies(self
):
89 args
= self
._GetChromeMockArgs
(page
=None, http_request_type
=None, sleep
=0,
90 expect_to_be_killed
=False)
91 returncode
, stdout
, _
= self
._Run
(args
, timeout
=10)
92 self
.assertNotEqual(-1, returncode
)
93 self
.assertTrue('Starting' in stdout
)
96 if __name__
== '__main__':