original_docstring: A small helper function to launch the simple HTTP server.
  
          This function launches the simple HTTP server, then waits for its
          banner output to appear.  If the banner doesn't appear within 10
          seconds, the test fails.  The banner is checked validate that it
          displays the right port number.
  
          Once the server is verified as running, this function sends it a GET
          request with the ?quit=1 URL parmeter.  It then waits to see if the
          server process exits with a return code of 0.  If the server process
          doesn't exit within 20 seconds, the test fails.
  
          Args:
            port: The port to use, defaults to 5103.
          
docstring: A small helper function to launch the simple HTTP server.
  This function launches the simple HTTP server, then waits for its
  banner output to appear.  If the banner doesn't appear within 10
  seconds, the test fails.  The banner is checked validate that it
  displays the right port number.
  
  Once the server is verified as running, this function sends it a GET
  request with the ?quit=1 URL parmeter.  It then waits to see if the
  server process exits with a return code of 0.  If the server process
  doesn't exit within 20 seconds, the test fails.
docstring_tokens: A small helper function to launch the simple HTTP server . This function launches the simple HTTP server then waits for its banner output to appear . If the banner doesn ' t appear within 10 seconds the test fails . The banner is checked validate that it displays the right port number . Once the server is verified as running this function sends it a GET request with the ?quit = 1 URL parmeter . It then waits to see if the server process exits with a return code of 0 . If the server process doesn ' t exit within 20 seconds the test fails .
code: def runAndQuitHttpServer(port=DEFAULT_SERVER_PORT,
                                 alternate_cwd=None,
                                 extra_args=[],
                                 should_fail=False):
          '''A small helper function to launch the simple HTTP server.
  
          This function launches the simple HTTP server, then waits for its
          banner output to appear.  If the banner doesn't appear within 10
          seconds, the test fails.  The banner is checked validate that it
          displays the right port number.
  
          Once the server is verified as running, this function sends it a GET
          request with the ?quit=1 URL parmeter.  It then waits to see if the
          server process exits with a return code of 0.  If the server process
          doesn't exit within 20 seconds, the test fails.
  
          Args:
            port: The port to use, defaults to 5103.
          '''
          path = staging_path
          command = [sys.executable, os.path.join(path, 'httpd.py')]
          # Add the port only if it's not the default.
          if port != DEFAULT_SERVER_PORT:
            command += [str(port)]
          command += extra_args
          # Can't use annotator.Run() because the HTTP server doesn't stop, which
          # causes Run() to hang.
          annotator.Print('Starting server: %s' % command)
          annotator.Print('extra_args=%s' % str(extra_args))
          current_working_dir = path if alternate_cwd is None else alternate_cwd
          annotator.Print('cwd=%s' % current_working_dir)
          process = subprocess.Popen(command,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     cwd=current_working_dir,
                                     env=self.GetBotShellEnv())
          self.assertNotEqual(None, process)
          # Wait until the process starts by trying to send it a GET request until
          # the server responds, or until the timeout expires.  If the timeout
          # expires, fail the test.
          time_start = time.time()
          time_now = time_start
          timeout_time = time_start + 20  # 20 sec timeout.
          output = ''
          conn = None
          while time_now < timeout_time:
            conn = httplib.HTTPConnection('localhost', port)
            try:
              # Send the quit request.
              conn.request("GET", "/?quit=1")
              output = process.stdout.readline()
              break
            except socket.error:
              # If the server is not listening for connections, then
              # HTTPConnetion() raises a socket exception, not one of the
              # exceptions defined in httplib.  In order to resend a request in
              # this case, the original connection has to be closed and re-opened.
              conn.close()
              conn = None
              time.sleep(1)  # Wait a second to try again.
            time_now = time.time()
  
          # If we expect the test to fail (e.g. bad directory without
          # --no_dir_check) then there should be no connection.
          if should_fail:
            self.assertEqual(None, conn)
            return_code = process.poll()
            # If the process has not terminated, return_code will be None
            # but since the server should have failed to launch, it should
            # have terminated by now.
            self.assertNotEqual(return_code, None)
            return
  
          self.assertNotEqual(None, conn)
          # Validate the first line of the startup banner.  An example of the
          # full line is:
          #   INFO:root:Starting local server on port 5103
          self.assertTrue(output.startswith('INFO:root:Starting'))
          self.assertEqual(1, output.count(str(port)))
          annotator.Print('Server startup banner: %s' % output)
  
          # Close down the connection and wait for the server to quit.
          conn.getresponse()
          conn.close()
  
          # Check to see if the server quit properly.  It should quit within
          # 0.5 seconds, so if the first poll() indicates that the process is
          # still running, wait 1 sec and then poll again.  If the process is
          # still running after 20 sec, then fail the test.
          return_code = process.poll()
          poll_count = 0
          while return_code == None and poll_count < 20:
            time.sleep(1)
            return_code = process.poll()
            poll_count += 1
          self.assertEqual(0, return_code)
code_tokens: def runAndQuitHttpServer ( port = DEFAULT_SERVER_PORT , alternate_cwd = None , extra_args = [ ] , should_fail = False ) : path = staging_path command = [ sys . executable , os . path . join ( path , 'httpd.py' ) ] if port != DEFAULT_SERVER_PORT : command += [ str ( port ) ] command += extra_args annotator . Print ( 'Starting server: %s' % command ) annotator . Print ( 'extra_args=%s' % str ( extra_args ) ) current_working_dir = path if alternate_cwd is None else alternate_cwd annotator . Print ( 'cwd=%s' % current_working_dir ) process = subprocess . Popen ( command , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , cwd = current_working_dir , env = self . GetBotShellEnv ( ) ) self . assertNotEqual ( None , process ) time_start = time . time ( ) time_now = time_start timeout_time = time_start + 20 output = '' conn = None while time_now < timeout_time : conn = httplib . HTTPConnection ( 'localhost' , port ) try : conn . request ( "GET" , "/?quit=1" ) output = process . stdout . readline ( ) break except socket . error : conn . close ( ) conn = None time . sleep ( 1 ) time_now = time . time ( ) if should_fail : self . assertEqual ( None , conn ) return_code = process . poll ( ) self . assertNotEqual ( return_code , None ) return self . assertNotEqual ( None , conn ) self . assertTrue ( output . startswith ( 'INFO:root:Starting' ) ) self . assertEqual ( 1 , output . count ( str ( port ) ) ) annotator . Print ( 'Server startup banner: %s' % output ) conn . getresponse ( ) conn . close ( ) return_code = process . poll ( ) poll_count = 0 while return_code == None and poll_count < 20 : time . sleep ( 1 ) return_code = process . poll ( ) poll_count += 1 self . assertEqual ( 0 , return_code )
short_docstring: A small helper function to launch the simple HTTP server.
short_docstring_tokens: A small helper function to launch the simple HTTP server .
comment: '''A small helper function to launch the simple HTTP server.
  
          This function launches the simple HTTP server, then waits for its
          banner output to appear.  If the banner doesn't appear within 10
          seconds, the test fails.  The banner is checked validate that it
          displays the right port number.
  
          Once the server is verified as running, this function sends it a GET
          request with the ?quit=1 URL parmeter.  It then waits to see if the
          server process exits with a return code of 0.  If the server process
          doesn't exit within 20 seconds, the test fails.
  
          Args:
            port: The port to use, defaults to 5103.
          '''$SEP$# Add the port only if it's not the default.$SEP$# Can't use annotator.Run() because the HTTP server doesn't stop, which$SEP$# causes Run() to hang.$SEP$# Wait until the process starts by trying to send it a GET request until$SEP$# the server responds, or until the timeout expires.  If the timeout$SEP$# expires, fail the test.$SEP$# 20 sec timeout.$SEP$# Send the quit request.$SEP$# If the server is not listening for connections, then$SEP$# HTTPConnetion() raises a socket exception, not one of the$SEP$# exceptions defined in httplib.  In order to resend a request in$SEP$# this case, the original connection has to be closed and re-opened.$SEP$# Wait a second to try again.$SEP$# If we expect the test to fail (e.g. bad directory without$SEP$# --no_dir_check) then there should be no connection.$SEP$# If the process has not terminated, return_code will be None$SEP$# but since the server should have failed to launch, it should$SEP$# have terminated by now.$SEP$# Validate the first line of the startup banner.  An example of the$SEP$# full line is:$SEP$#   INFO:root:Starting local server on port 5103$SEP$# Close down the connection and wait for the server to quit.$SEP$# Check to see if the server quit properly.  It should quit within$SEP$# 0.5 seconds, so if the first poll() indicates that the process is$SEP$# still running, wait 1 sec and then poll again.  If the process is$SEP$# still running after 20 sec, then fail the test.