~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/HTTPTestUtil.py

merge John Meinels http test server reliability patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import BaseHTTPServer, SimpleHTTPServer
 
17
import BaseHTTPServer, SimpleHTTPServer, socket, errno, time
18
18
from bzrlib.selftest import TestCaseInTempDir
19
19
 
20
20
 
27
27
 
28
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
29
29
    def log_message(self, format, *args):
30
 
        self.server.test_case.log("webserver - %s - - [%s] %s\n" %
 
30
        self.server.test_case.log("webserver - %s - - [%s] %s" %
31
31
                                  (self.address_string(),
32
32
                                   self.log_date_time_string(),
33
33
                                   format%args))
34
34
 
 
35
    def handle_one_request(self):
 
36
        """Handle a single HTTP request.
 
37
 
 
38
        You normally don't need to override this method; see the class
 
39
        __doc__ string for information on how to handle specific HTTP
 
40
        commands such as GET and POST.
 
41
 
 
42
        """
 
43
        for i in xrange(1,11): # Don't try more than 10 times
 
44
            try:
 
45
                self.raw_requestline = self.rfile.readline()
 
46
            except socket.error, e:
 
47
                if e.args[0] == errno.EAGAIN:
 
48
                    self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
 
49
                    time.sleep(0.01)
 
50
                    continue
 
51
                raise
 
52
            else:
 
53
                break
 
54
        if not self.raw_requestline:
 
55
            self.close_connection = 1
 
56
            return
 
57
        if not self.parse_request(): # An error code has been sent, just exit
 
58
            return
 
59
        mname = 'do_' + self.command
 
60
        if not hasattr(self, mname):
 
61
            self.send_error(501, "Unsupported method (%r)" % self.command)
 
62
            return
 
63
        method = getattr(self, mname)
 
64
        method()
 
65
 
35
66
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
36
67
    def __init__(self, server_address, RequestHandlerClass, test_case):
37
68
        BaseHTTPServer.HTTPServer.__init__(self, server_address,
38
69
                                                RequestHandlerClass)
39
70
        self.test_case = test_case
40
71
 
 
72
 
41
73
class TestCaseWithWebserver(TestCaseInTempDir):
42
74
    """Derived class that starts a localhost-only webserver
43
75
    (in addition to what TestCaseInTempDir does).
70
102
 
71
103
        self._http_base_url = 'http://localhost:%s/' % port
72
104
        self._http_starting.release()
73
 
        httpd.socket.settimeout(1)
 
105
        httpd.socket.settimeout(0.1)
74
106
 
75
107
        while self._http_running:
76
108
            try:
95
127
        return self._http_base_url + remote_path
96
128
 
97
129
    def setUp(self):
98
 
        super(TestCaseWithWebserver, self).setUp()
 
130
        TestCaseInTempDir.setUp(self)
99
131
        import threading, os
100
132
        self._local_path_parts = self.test_dir.split(os.path.sep)
101
133
        self._http_starting = threading.Lock()
109
141
    def tearDown(self):
110
142
        self._http_running = False
111
143
        self._http_thread.join()
112
 
        super(TestCaseWithWebserver, self).tearDown()
 
144
        TestCaseInTempDir.tearDown(self)
 
145