~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Vincent Ladeuil
  • Date: 2009-10-07 15:17:48 UTC
  • mto: (5247.1.1 first-try)
  • mto: This revision was merged to the branch mainline in revision 5396.
  • Revision ID: v.ladeuil+lp@free.fr-20091007151748-lcg3ehht39963izr
No more leaks in http tests.

* bzrlib/tests/test_http.py:
(RecordingServer.setUp, RecordingServer.TearDown): Reclaim leaked
thread by forcing a connection if none has been issued and
modifying the server to cope with it. Also delete the dead thread.

* bzrlib/tests/http_server.py:
(TestingThreadingHTTPServer.shutdown, HttpServer.tearDown): Delete
dead thread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
228
228
        self._thread = threading.Thread(target=self._accept_read_and_reply)
229
229
        self._thread.setDaemon(True)
230
230
        self._thread.start()
231
 
        self._ready.wait(5)
 
231
        self._ready.wait()
232
232
 
233
233
    def _accept_read_and_reply(self):
234
234
        self._sock.listen(1)
 
235
        self._sock.settimeout(5)
235
236
        self._ready.set()
236
 
        self._sock.settimeout(5)
237
237
        try:
238
238
            conn, address = self._sock.accept()
239
239
            # On win32, the accepted connection will be non-blocking to start
240
240
            # with because we're using settimeout.
241
241
            conn.setblocking(True)
242
 
            while not self.received_bytes.endswith(self._expect_body_tail):
243
 
                self.received_bytes += conn.recv(4096)
244
 
            conn.sendall('HTTP/1.1 200 OK\r\n')
 
242
            if self._expect_body_tail is not None:
 
243
                while not self.received_bytes.endswith(self._expect_body_tail):
 
244
                    self.received_bytes += conn.recv(4096)
 
245
                conn.sendall('HTTP/1.1 200 OK\r\n')
245
246
        except socket.timeout:
246
247
            # Make sure the client isn't stuck waiting for us to e.g. accept.
 
248
            pass
 
249
        try:
247
250
            self._sock.close()
248
251
        except socket.error:
249
252
            # The client may have already closed the socket.
251
254
 
252
255
    def tearDown(self):
253
256
        try:
254
 
            self._sock.close()
 
257
            # Issue a fake connection to wake up the server and allow it to
 
258
            # finish quickly
 
259
            fake_conn = socket.create_connection((self.host, self.port))
 
260
            fake_conn.close()
255
261
        except socket.error:
256
262
            # We might have already closed it.  We don't care.
257
263
            pass
258
264
        self.host = None
259
265
        self.port = None
 
266
        self._thread.join()
 
267
        del self._thread
 
268
        self.thread = None
260
269
 
261
270
 
262
271
class TestAuthHeader(tests.TestCase):