~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_test_server.py

  • Committer: Vincent Ladeuil
  • Date: 2010-06-07 12:55:58 UTC
  • mto: (5247.6.1 http-leaks)
  • mto: This revision was merged to the branch mainline in revision 5396.
  • Revision ID: v.ladeuil+lp@free.fr-20100607125558-5rdbfk21podzot0x
Implement an execption handling mechanism that can be injected in ThreadWithException.

* bzrlib/tests/test_test_server.py:
(TestTCPServerInAThread.test_exception_swallowed_while_serving):
Check that the expected exception is swallowed.

* bzrlib/tests/test_server.py:
(ThreadWithException.set_ignored_exceptions): Allows exception
handlers to be installed.
(ThreadWithException.join): Respect ignored_exceptions.
(TestingTCPServerMixin.set_ignored_exceptions)
(TestingTCPServerMixin._pending_exections): New helpers.
(TestingThreadingTCPServer.process_request)
(TestingThreadingTCPServer.set_ignored_exceptions): Propagate
exception_handler.
(TestingTCPServerInAThread.set_ignored_exceptions): New helper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
148
148
                          self.get_server, server_class=CantStartServer)
149
149
 
150
150
    def test_server_fails_while_serving_or_stoping(self):
151
 
        class ServerFailure(Exception):
 
151
        class CantConnect(Exception):
152
152
            pass
153
153
 
154
154
        class FailingConnectionHandler(TCPConnectionHandler):
155
155
 
156
156
            def handle(self):
157
 
                raise ServerFailure()
 
157
                raise CantConnect()
158
158
 
159
159
        server = self.get_server(
160
160
            connection_handler_class=FailingConnectionHandler)
172
172
        except socket.error:
173
173
            pass
174
174
        # Now the server has raise the exception in its own thread
175
 
        self.assertRaises(ServerFailure, server.stop_server)
176
 
 
 
175
        self.assertRaises(CantConnect, server.stop_server)
177
176
 
178
177
    def test_server_crash_while_responding(self):
179
178
        sync = threading.Event()
180
179
        sync.clear()
181
 
        class ServerFailure(Exception):
 
180
        class FailToRespond(Exception):
182
181
            pass
183
182
 
184
183
        class FailingDuringResponseHandler(TCPConnectionHandler):
186
185
            def handle_connection(self):
187
186
                req = self.rfile.readline()
188
187
                sync.set()
189
 
                raise ServerFailure()
 
188
                raise FailToRespond()
190
189
 
191
190
        server = self.get_server(
192
191
            connection_handler_class=FailingDuringResponseHandler)
194
193
        client.connect(server.server_address)
195
194
        client.write('ping\n')
196
195
        sync.wait()
197
 
        self.assertRaises(ServerFailure, server.pending_exception)
 
196
        self.assertRaises(FailToRespond, server.pending_exception)
 
197
 
 
198
    def test_exception_swallowed_while_serving(self):
 
199
        sync = threading.Event()
 
200
        sync.clear()
 
201
        class CantServe(Exception):
 
202
            pass
 
203
 
 
204
        class FailingWhileServingConnectionHandler(TCPConnectionHandler):
 
205
 
 
206
            def handle(self):
 
207
                sync.set()
 
208
                raise CantServe()
 
209
 
 
210
        server = self.get_server(
 
211
            connection_handler_class=FailingWhileServingConnectionHandler)
 
212
        # Install the exception swallower
 
213
        server.set_ignored_exceptions(CantServe)
 
214
        client = self.get_client()
 
215
        client.connect(server.server_address)
 
216
        sync.wait()
 
217
        # The connection wasn't served properly but the exception should have
 
218
        # been swallowed.
 
219
        server.pending_exception()