OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 import Queue | 7 import Queue |
8 import sys | 8 import sys |
9 import subprocess | 9 import subprocess |
10 import threading | 10 import threading |
11 import unittest | 11 import unittest |
12 import urllib2 | 12 import urllib2 |
13 | 13 |
14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
15 PARENT_DIR = os.path.dirname(SCRIPT_DIR) | 15 PARENT_DIR = os.path.dirname(SCRIPT_DIR) |
16 | 16 |
17 sys.path.append(PARENT_DIR) | 17 sys.path.append(PARENT_DIR) |
18 | 18 |
19 import httpd | 19 import httpd |
20 | 20 |
21 | 21 |
22 class HTTPDTest(unittest.TestCase): | 22 class HTTPDTest(unittest.TestCase): |
23 def setUp(self): | 23 def setUp(self): |
24 self.server = httpd.LocalHTTPServer('.', 0, False) | 24 self.server = httpd.LocalHTTPServer('.', 0) |
25 | 25 |
26 def tearDown(self): | 26 def tearDown(self): |
27 self.server.Shutdown() | 27 self.server.Shutdown() |
28 | 28 |
29 def testQuit(self): | 29 def testQuit(self): |
30 urllib2.urlopen(self.server.GetURL('?quit=1')) | 30 urllib2.urlopen(self.server.GetURL('?quit=1')) |
31 self.server.process.join(10) # Wait 10 seconds for the process to finish. | 31 self.server.process.join(10) # Wait 10 seconds for the process to finish. |
32 self.assertFalse(self.server.process.is_alive()) | 32 self.assertFalse(self.server.process.is_alive()) |
33 | 33 |
34 | 34 |
(...skipping 24 matching lines...) Expand all Loading... |
59 thread.start() | 59 thread.start() |
60 thread.join(timeout) | 60 thread.join(timeout) |
61 if not thread.is_alive(): | 61 if not thread.is_alive(): |
62 returncode, stdout, stderr = queue.get(False) | 62 returncode, stdout, stderr = queue.get(False) |
63 return returncode, stdout, stderr | 63 return returncode, stdout, stderr |
64 | 64 |
65 return -1, None, None | 65 return -1, None, None |
66 | 66 |
67 def _GetChromeMockArgs(self, page, http_request_type, sleep, | 67 def _GetChromeMockArgs(self, page, http_request_type, sleep, |
68 expect_to_be_killed=True): | 68 expect_to_be_killed=True): |
69 args = ['--test-mode'] | 69 args = [] |
70 if page: | 70 if page: |
71 args.extend(['-P', page]) | 71 args.extend(['-P', page]) |
72 args.append('--') | 72 args.append('--') |
73 args.extend([sys.executable, os.path.join(SCRIPT_DIR, 'chrome_mock.py')]) | 73 args.extend([sys.executable, os.path.join(SCRIPT_DIR, 'chrome_mock.py')]) |
74 if http_request_type: | 74 if http_request_type: |
75 args.append('--' + http_request_type) | 75 args.append('--' + http_request_type) |
76 if sleep: | 76 if sleep: |
77 args.extend(['--sleep', str(sleep)]) | 77 args.extend(['--sleep', str(sleep)]) |
78 if expect_to_be_killed: | 78 if expect_to_be_killed: |
79 args.append('--expect-to-be-killed') | 79 args.append('--expect-to-be-killed') |
80 return args | 80 return args |
81 | 81 |
82 def testQuit(self): | 82 def testQuit(self): |
83 args = self._GetChromeMockArgs('?quit=1', 'get', sleep=10) | 83 args = self._GetChromeMockArgs('?quit=1', 'get', sleep=10) |
84 _, stdout, _ = self._Run(args, timeout=20) | 84 _, stdout, _ = self._Run(args, timeout=20) |
85 self.assertTrue('Starting' in stdout) | 85 self.assertTrue('Starting' in stdout) |
86 self.assertTrue('Expected to be killed' not in stdout) | 86 self.assertTrue('Expected to be killed' not in stdout) |
87 | 87 |
88 def testSubprocessDies(self): | 88 def testSubprocessDies(self): |
89 args = self._GetChromeMockArgs(page=None, http_request_type=None, sleep=0, | 89 args = self._GetChromeMockArgs(page=None, http_request_type=None, sleep=0, |
90 expect_to_be_killed=False) | 90 expect_to_be_killed=False) |
91 returncode, stdout, _ = self._Run(args, timeout=10) | 91 returncode, stdout, _ = self._Run(args, timeout=10) |
92 self.assertNotEqual(-1, returncode) | 92 self.assertNotEqual(-1, returncode) |
93 self.assertTrue('Starting' in stdout) | 93 self.assertTrue('Starting' in stdout) |
94 | 94 |
95 def testPostOk(self): | |
96 args = self._GetChromeMockArgs('ok', 'post', sleep=10) | |
97 returncode, stdout, _ = self._Run(args, timeout=20) | |
98 self.assertEqual(0, returncode) | |
99 self.assertTrue('Starting' in stdout) | |
100 self.assertTrue('Expected to be killed' not in stdout) | |
101 | |
102 def testPostFail(self): | |
103 args = self._GetChromeMockArgs('fail', 'post', sleep=10) | |
104 returncode, stdout, _ = self._Run(args, timeout=20) | |
105 self.assertEqual(1, returncode) | |
106 self.assertTrue('Starting' in stdout) | |
107 self.assertTrue('Expected to be killed' not in stdout) | |
108 | |
109 | 95 |
110 if __name__ == '__main__': | 96 if __name__ == '__main__': |
111 unittest.main() | 97 unittest.main() |
OLD | NEW |