Siddharth Shukla | 8e64d90 | 2017-03-12 19:50:18 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 2 | # Copyright 2015, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Manage TCP ports for unit tests; started by run_tests.py""" |
| 32 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 33 | from __future__ import print_function |
| 34 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 35 | import argparse |
Craig Tiller | ea525eb | 2017-04-24 17:50:32 +0000 | [diff] [blame] | 36 | from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 37 | import hashlib |
| 38 | import os |
| 39 | import socket |
| 40 | import sys |
| 41 | import time |
Craig Tiller | ea525eb | 2017-04-24 17:50:32 +0000 | [diff] [blame] | 42 | from SocketServer import ThreadingMixIn |
| 43 | import threading |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 44 | |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 45 | |
| 46 | # increment this number whenever making a change to ensure that |
| 47 | # the changes are picked up by running CI servers |
| 48 | # note that all changes must be backwards compatible |
Craig Tiller | ec49524 | 2017-04-24 20:55:43 +0000 | [diff] [blame^] | 49 | _MY_VERSION = 11 |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 50 | |
| 51 | |
| 52 | if len(sys.argv) == 2 and sys.argv[1] == 'dump_version': |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 53 | print(_MY_VERSION) |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 54 | sys.exit(0) |
| 55 | |
| 56 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 57 | argp = argparse.ArgumentParser(description='Server for httpcli_test') |
| 58 | argp.add_argument('-p', '--port', default=12345, type=int) |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 59 | argp.add_argument('-l', '--logfile', default=None, type=str) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 60 | args = argp.parse_args() |
| 61 | |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 62 | if args.logfile is not None: |
Craig Tiller | 41a7bf5 | 2015-10-12 12:54:55 -0700 | [diff] [blame] | 63 | sys.stdin.close() |
| 64 | sys.stderr.close() |
| 65 | sys.stdout.close() |
Craig Tiller | f0a293e | 2015-10-12 10:05:50 -0700 | [diff] [blame] | 66 | sys.stderr = open(args.logfile, 'w') |
| 67 | sys.stdout = sys.stderr |
| 68 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 69 | print('port server running on port %d' % args.port) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 70 | |
| 71 | pool = [] |
| 72 | in_use = {} |
Craig Tiller | ec49524 | 2017-04-24 20:55:43 +0000 | [diff] [blame^] | 73 | mu = threading.Lock() |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 74 | |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 75 | |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 76 | def refill_pool(max_timeout, req): |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 77 | """Scan for ports not marked for being in use""" |
Ken Payson | fa51de5 | 2016-06-30 23:50:48 -0700 | [diff] [blame] | 78 | for i in range(1025, 32766): |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 79 | if len(pool) > 100: break |
| 80 | if i in in_use: |
| 81 | age = time.time() - in_use[i] |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 82 | if age < max_timeout: |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 83 | continue |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 84 | req.log_message("kill old request %d" % i) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 85 | del in_use[i] |
| 86 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 87 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 88 | try: |
| 89 | s.bind(('localhost', i)) |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 90 | req.log_message("found available port %d" % i) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 91 | pool.append(i) |
| 92 | except: |
| 93 | pass # we really don't care about failures |
| 94 | finally: |
| 95 | s.close() |
| 96 | |
| 97 | |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 98 | def allocate_port(req): |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 99 | global pool |
| 100 | global in_use |
Craig Tiller | ec49524 | 2017-04-24 20:55:43 +0000 | [diff] [blame^] | 101 | global mu |
| 102 | mu.acquire() |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 103 | max_timeout = 600 |
| 104 | while not pool: |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 105 | refill_pool(max_timeout, req) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 106 | if not pool: |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 107 | req.log_message("failed to find ports: retrying soon") |
Craig Tiller | ec49524 | 2017-04-24 20:55:43 +0000 | [diff] [blame^] | 108 | mu.release() |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 109 | time.sleep(1) |
Craig Tiller | ec49524 | 2017-04-24 20:55:43 +0000 | [diff] [blame^] | 110 | mu.acquire() |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 111 | max_timeout /= 2 |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 112 | port = pool[0] |
| 113 | pool = pool[1:] |
| 114 | in_use[port] = time.time() |
Craig Tiller | ec49524 | 2017-04-24 20:55:43 +0000 | [diff] [blame^] | 115 | mu.release() |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 116 | return port |
| 117 | |
| 118 | |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 119 | keep_running = True |
| 120 | |
| 121 | |
Craig Tiller | ea525eb | 2017-04-24 17:50:32 +0000 | [diff] [blame] | 122 | class Handler(BaseHTTPRequestHandler): |
Ken Payson | 189d685 | 2016-07-09 15:58:18 -0700 | [diff] [blame] | 123 | |
| 124 | def setup(self): |
| 125 | # If the client is unreachable for 5 seconds, close the connection |
| 126 | self.timeout = 5 |
Craig Tiller | ea525eb | 2017-04-24 17:50:32 +0000 | [diff] [blame] | 127 | BaseHTTPRequestHandler.setup(self) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 128 | |
| 129 | def do_GET(self): |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 130 | global keep_running |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 131 | if self.path == '/get': |
| 132 | # allocate a new port, it will stay bound for ten minutes and until |
| 133 | # it's unused |
| 134 | self.send_response(200) |
| 135 | self.send_header('Content-Type', 'text/plain') |
| 136 | self.end_headers() |
Craig Tiller | c20b19d | 2015-09-22 13:06:06 -0700 | [diff] [blame] | 137 | p = allocate_port(self) |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 138 | self.log_message('allocated port %d' % p) |
| 139 | self.wfile.write('%d' % p) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 140 | elif self.path[0:6] == '/drop/': |
| 141 | self.send_response(200) |
| 142 | self.send_header('Content-Type', 'text/plain') |
| 143 | self.end_headers() |
| 144 | p = int(self.path[6:]) |
Craig Tiller | d2c3971 | 2015-10-12 11:08:49 -0700 | [diff] [blame] | 145 | if p in in_use: |
| 146 | del in_use[p] |
| 147 | pool.append(p) |
| 148 | self.log_message('drop known port %d' % p) |
| 149 | else: |
| 150 | self.log_message('drop unknown port %d' % p) |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 151 | elif self.path == '/version_number': |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 152 | # fetch a version string and the current process pid |
| 153 | self.send_response(200) |
| 154 | self.send_header('Content-Type', 'text/plain') |
| 155 | self.end_headers() |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 156 | self.wfile.write(_MY_VERSION) |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 157 | elif self.path == '/dump': |
Jan Tattermusch | 13e65df | 2015-09-21 09:30:49 -0700 | [diff] [blame] | 158 | # yaml module is not installed on Macs and Windows machines by default |
| 159 | # so we import it lazily (/dump action is only used for debugging) |
| 160 | import yaml |
Craig Tiller | ae322af | 2015-09-13 18:41:21 +0000 | [diff] [blame] | 161 | self.send_response(200) |
| 162 | self.send_header('Content-Type', 'text/plain') |
| 163 | self.end_headers() |
| 164 | now = time.time() |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 165 | self.wfile.write(yaml.dump({'pool': pool, 'in_use': dict((k, now - v) for k, v in in_use.items())})) |
Craig Tiller | fe4939f | 2015-10-06 12:55:36 -0700 | [diff] [blame] | 166 | elif self.path == '/quitquitquit': |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 167 | self.send_response(200) |
| 168 | self.end_headers() |
Craig Tiller | ea525eb | 2017-04-24 17:50:32 +0000 | [diff] [blame] | 169 | sys.exit(0) |
| 170 | |
| 171 | class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): |
| 172 | """Handle requests in a separate thread""" |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 173 | |
| 174 | |
Craig Tiller | ea525eb | 2017-04-24 17:50:32 +0000 | [diff] [blame] | 175 | httpd = ThreadedHTTPServer(('', args.port), Handler) |
| 176 | httpd.serve_forever() |
Craig Tiller | ef12559 | 2015-08-05 07:41:35 -0700 | [diff] [blame] | 177 | |