/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_test_server.py

  • Committer: Vincent Ladeuil
  • Date: 2011-10-07 15:49:08 UTC
  • mto: (6015.33.11 2.4)
  • mto: This revision was merged to the branch mainline in revision 6206.
  • Revision ID: v.ladeuil+lp@free.fr-20111007154908-51te99s60r7hsisd
Less code, more explanations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
169
169
        self.assertRaises(CantConnect, server.stop_server)
170
170
 
171
171
    def test_server_crash_while_responding(self):
172
 
        # We need to ensure the exception will be raised
173
 
        will_raise = threading.Event()
174
 
        will_raise.clear()
175
 
        # We need to ensure the exception has been caught
 
172
        # We want to ensure the exception has been caught
176
173
        caught = threading.Event()
177
174
        caught.clear()
178
 
        # The thread that will serve the client
 
175
        # The thread that will serve the client, this needs to be an attribute
 
176
        # so the handler below can modify it when it's executed (it's
 
177
        # instantiated when the request is processed)
179
178
        self.connection_thread = None
 
179
 
180
180
        class FailToRespond(Exception):
181
181
            pass
182
182
 
185
185
            def handle_connection(request):
186
186
                req = request.rfile.readline()
187
187
                # Capture the thread and make it use 'caught' so we can wait on
188
 
                # the even set when the exception is caught
 
188
                # the even that will be set when the exception is caught. We
 
189
                # also capture the thread to know where to look.
189
190
                self.connection_thread = threading.currentThread()
190
191
                self.connection_thread.set_sync_event(caught)
191
 
                will_raise.set()
192
192
                raise FailToRespond()
193
193
 
194
194
        server = self.get_server(
196
196
        client = self.get_client()
197
197
        client.connect((server.host, server.port))
198
198
        client.write('ping\n')
199
 
        # Wait for the connection to succeed and be processed
200
 
        will_raise.wait()
201
199
        # Wait for the exception to be caught
202
200
        caught.wait()
203
201
        # Check that the connection thread did catch the exception,
205
203
        # works for TestingTCPServer where the connection is handled in the
206
204
        # same thread than the server one but is racy for
207
205
        # TestingThreadingTCPServer where the server thread may be in a
208
 
        # blocking accept() call.
 
206
        # blocking accept() call (or not).
209
207
        try:
210
208
            self.connection_thread.pending_exception()
211
209
        except FailToRespond:
213
211
            pass
214
212
        else:
215
213
            # If the exception is not in the connection thread anymore, it's in
216
 
            # the server. 
 
214
            # the server's one. 
217
215
            server.server.stopped.wait()
218
216
            # The exception is available now
219
217
            self.assertRaises(FailToRespond, server.pending_exception)
220
218
 
221
219
    def test_exception_swallowed_while_serving(self):
222
 
        # We need to ensure the exception will be raised
223
 
        will_raise = threading.Event()
224
 
        will_raise.clear()
225
220
        # We need to ensure the exception has been caught
226
221
        caught = threading.Event()
227
222
        caught.clear()
228
 
        # The thread that will serve the client
 
223
        # The thread that will serve the client, this needs to be an attribute
 
224
        # so the handler below can access it when it's executed (it's
 
225
        # instantiated when the request is processed)
229
226
        self.connection_thread = None
230
227
        class CantServe(Exception):
231
228
            pass
234
231
 
235
232
            def handle(request):
236
233
                # Capture the thread and make it use 'caught' so we can wait on
237
 
                # the even set when the exception is caught
 
234
                # the even that will be set when the exception is caught. We
 
235
                # also capture the thread to know where to look.
238
236
                self.connection_thread = threading.currentThread()
239
237
                self.connection_thread.set_sync_event(caught)
240
 
                will_raise.set()
241
238
                raise CantServe()
242
239
 
243
240
        server = self.get_server(
247
244
        client = self.get_client()
248
245
        # Connect to the server so the exception is raised there
249
246
        client.connect((server.host, server.port))
250
 
        # Wait for the connection to succeed and be processed
251
 
        will_raise.wait()
252
247
        # Wait for the exception to be caught
253
248
        caught.wait()
254
249
        # The connection wasn't served properly but the exception should have