[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / native_client_sdk / src / tools / tests / httpd_test.py
blob9cdeaeca1f57b68d870ab8f471a98e2135ced612
1 #!/usr/bin/env python
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.
6 import os
7 import Queue
8 import sys
9 import subprocess
10 import threading
11 import unittest
12 import urllib2
14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
15 PARENT_DIR = os.path.dirname(SCRIPT_DIR)
17 sys.path.append(PARENT_DIR)
19 import httpd
22 class HTTPDTest(unittest.TestCase):
23 def setUp(self):
24 self.server = httpd.LocalHTTPServer('.', 0)
26 def tearDown(self):
27 self.server.Shutdown()
29 def testQuit(self):
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):
36 def setUp(self):
37 self.process = None
39 def tearDown(self):
40 if self.process and self.process.returncode is None:
41 self.process.kill()
43 @staticmethod
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):
49 args = args or []
50 run_py = os.path.join(PARENT_DIR, 'run.py')
51 cmd = [sys.executable, run_py]
52 cmd.extend(args)
53 self.process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
54 stderr=subprocess.PIPE)
55 queue = Queue.Queue()
56 thread = threading.Thread(target=RunTest._SubprocessThread,
57 args=(self.process, queue))
58 thread.daemon = True
59 thread.start()
60 thread.join(timeout)
61 if not thread.is_alive():
62 returncode, stdout, stderr = queue.get(False)
63 return returncode, stdout, stderr
65 return -1, None, None
67 def _GetChromeMockArgs(self, page, http_request_type, sleep,
68 expect_to_be_killed=True):
69 args = []
70 if page:
71 args.extend(['-P', page])
72 args.append('--')
73 args.extend([sys.executable, os.path.join(SCRIPT_DIR, 'chrome_mock.py')])
74 if http_request_type:
75 args.append('--' + http_request_type)
76 if sleep:
77 args.extend(['--sleep', str(sleep)])
78 if expect_to_be_killed:
79 args.append('--expect-to-be-killed')
80 return args
82 def testQuit(self):
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__':
97 unittest.main()