OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python2.7 |
| 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 |
| 33 import argparse |
| 34 import BaseHTTPServer |
| 35 import hashlib |
| 36 import os |
| 37 import socket |
| 38 import sys |
| 39 import time |
| 40 |
| 41 |
| 42 # increment this number whenever making a change to ensure that |
| 43 # the changes are picked up by running CI servers |
| 44 # note that all changes must be backwards compatible |
| 45 _MY_VERSION = 7 |
| 46 |
| 47 |
| 48 if len(sys.argv) == 2 and sys.argv[1] == 'dump_version': |
| 49 print _MY_VERSION |
| 50 sys.exit(0) |
| 51 |
| 52 |
| 53 argp = argparse.ArgumentParser(description='Server for httpcli_test') |
| 54 argp.add_argument('-p', '--port', default=12345, type=int) |
| 55 argp.add_argument('-l', '--logfile', default=None, type=str) |
| 56 args = argp.parse_args() |
| 57 |
| 58 if args.logfile is not None: |
| 59 sys.stdin.close() |
| 60 sys.stderr.close() |
| 61 sys.stdout.close() |
| 62 sys.stderr = open(args.logfile, 'w') |
| 63 sys.stdout = sys.stderr |
| 64 |
| 65 print 'port server running on port %d' % args.port |
| 66 |
| 67 pool = [] |
| 68 in_use = {} |
| 69 |
| 70 |
| 71 def refill_pool(max_timeout, req): |
| 72 """Scan for ports not marked for being in use""" |
| 73 for i in range(1025, 32767): |
| 74 if len(pool) > 100: break |
| 75 if i in in_use: |
| 76 age = time.time() - in_use[i] |
| 77 if age < max_timeout: |
| 78 continue |
| 79 req.log_message("kill old request %d" % i) |
| 80 del in_use[i] |
| 81 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 82 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 83 try: |
| 84 s.bind(('localhost', i)) |
| 85 req.log_message("found available port %d" % i) |
| 86 pool.append(i) |
| 87 except: |
| 88 pass # we really don't care about failures |
| 89 finally: |
| 90 s.close() |
| 91 |
| 92 |
| 93 def allocate_port(req): |
| 94 global pool |
| 95 global in_use |
| 96 max_timeout = 600 |
| 97 while not pool: |
| 98 refill_pool(max_timeout, req) |
| 99 if not pool: |
| 100 req.log_message("failed to find ports: retrying soon") |
| 101 time.sleep(1) |
| 102 max_timeout /= 2 |
| 103 port = pool[0] |
| 104 pool = pool[1:] |
| 105 in_use[port] = time.time() |
| 106 return port |
| 107 |
| 108 |
| 109 keep_running = True |
| 110 |
| 111 |
| 112 class Handler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 113 |
| 114 def do_GET(self): |
| 115 global keep_running |
| 116 if self.path == '/get': |
| 117 # allocate a new port, it will stay bound for ten minutes and until |
| 118 # it's unused |
| 119 self.send_response(200) |
| 120 self.send_header('Content-Type', 'text/plain') |
| 121 self.end_headers() |
| 122 p = allocate_port(self) |
| 123 self.log_message('allocated port %d' % p) |
| 124 self.wfile.write('%d' % p) |
| 125 elif self.path[0:6] == '/drop/': |
| 126 self.send_response(200) |
| 127 self.send_header('Content-Type', 'text/plain') |
| 128 self.end_headers() |
| 129 p = int(self.path[6:]) |
| 130 if p in in_use: |
| 131 del in_use[p] |
| 132 pool.append(p) |
| 133 self.log_message('drop known port %d' % p) |
| 134 else: |
| 135 self.log_message('drop unknown port %d' % p) |
| 136 elif self.path == '/version_number': |
| 137 # fetch a version string and the current process pid |
| 138 self.send_response(200) |
| 139 self.send_header('Content-Type', 'text/plain') |
| 140 self.end_headers() |
| 141 self.wfile.write(_MY_VERSION) |
| 142 elif self.path == '/dump': |
| 143 # yaml module is not installed on Macs and Windows machines by default |
| 144 # so we import it lazily (/dump action is only used for debugging) |
| 145 import yaml |
| 146 self.send_response(200) |
| 147 self.send_header('Content-Type', 'text/plain') |
| 148 self.end_headers() |
| 149 now = time.time() |
| 150 self.wfile.write(yaml.dump({'pool': pool, 'in_use': dict((k, now - v) for
k, v in in_use.iteritems())})) |
| 151 elif self.path == '/quitquitquit': |
| 152 self.send_response(200) |
| 153 self.end_headers() |
| 154 keep_running = False |
| 155 |
| 156 |
| 157 httpd = BaseHTTPServer.HTTPServer(('', args.port), Handler) |
| 158 while keep_running: |
| 159 httpd.handle_request() |
| 160 sys.stderr.flush() |
| 161 |
| 162 print 'done' |
OLD | NEW |