~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

  • Committer: John Arbash Meinel
  • Date: 2011-09-23 16:40:32 UTC
  • mto: This revision was merged to the branch mainline in revision 6170.
  • Revision ID: john@arbash-meinel.com-20110923164032-15duj1g3hj49ww53
Now we've implemented the basic structure.

I tested it manually, and it works as we wanted, the server waits for the current
request to stop, but keeps running until it does so. Once the current request
has finished, it closes the connection to the client.

There are now a couple of race conditions in the test suite. I caught a couple
of them, like the connection thread was being started before we added it to
the list of active connections.
The next race that I know of, is the test suite expects that a request&response
finish is enough for us to have run _poll_active_connections.
However, there is no actual interlocking there.
So we actually need to do a new connection, which may give us 1-or-2 active
connections, but then we can check if the old handler is still around.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
# all of this deals with byte strings so this is safe
20
20
from cStringIO import StringIO
 
21
import doctest
21
22
import os
22
23
import socket
23
24
import sys
24
25
import threading
25
26
import time
26
27
 
 
28
from testtools.matchers import DocTestMatches
 
29
 
27
30
import bzrlib
28
31
from bzrlib import (
29
32
        bzrdir,
1131
1134
        server._stop_gracefully()
1132
1135
        self.connect_to_server_and_hangup(server)
1133
1136
        server._stopped.wait()
 
1137
        server._fully_stopped.wait()
1134
1138
        server_thread.join()
1135
1139
 
1136
1140
    def test_get_error_unexpected(self):
1187
1191
        self.say_hello(client_sock)
1188
1192
        self.assertEqual(1, len(server._active_connections))
1189
1193
        # Grab a handle to the thread that is processing our request
1190
 
        _, _, server_side_thread = server._active_connections[0]
 
1194
        _, server_side_thread = server._active_connections[0]
1191
1195
        # Close the connection, ask the server to stop, and wait for the
1192
1196
        # server to stop, as well as the thread that was servicing the
1193
1197
        # client request.
1208
1212
        # results.
1209
1213
        self.say_hello(client_sock1)
1210
1214
        self.assertEqual(1, len(server._active_connections))
1211
 
        _, _, server_side_thread1 = server._active_connections[0]
 
1215
        _, server_side_thread1 = server._active_connections[0]
1212
1216
        client_sock1.close()
1213
1217
        server_side_thread1.join()
1214
1218
        # By waiting until the first connection is fully done, the server
1216
1220
        client_sock2 = self.connect_to_server(server)
1217
1221
        self.say_hello(client_sock2)
1218
1222
        self.assertEqual(1, len(server._active_connections))
1219
 
        _, _, server_side_thread2 = server._active_connections[0]
 
1223
        _, server_side_thread2 = server._active_connections[0]
1220
1224
        client_sock2.close()
1221
1225
        server_side_thread2.join()
1222
1226
        self.shutdown_server_cleanly(server, server_thread)
1225
1229
        server, server_thread = self.make_server()
1226
1230
        client_sock = self.connect_to_server(server)
1227
1231
        self.say_hello(client_sock)
1228
 
        _, _, server_side_thread = server._active_connections[0]
 
1232
        _, server_side_thread = server._active_connections[0]
1229
1233
        # Ask the server to stop gracefully, and wait for it.
1230
1234
        server._stop_gracefully()
1231
1235
        self.connect_to_server_and_hangup(server)
1240
1244
        server_thread.join()
1241
1245
        self.assertTrue(server._fully_stopped.isSet())
1242
1246
        log = self.get_log()
1243
 
        self.assertEqual('    INFO  Requested to stop gracefully\n'
1244
 
                         '    INFO  Waiting for 1 client(s) to finish\n',
1245
 
                         log)
 
1247
        self.assertThat(log, DocTestMatches("""\
 
1248
    INFO  Requested to stop gracefully
 
1249
... Stopping <bzrlib.smart.medium.SmartServerSocketStreamMedium ...
 
1250
    INFO  Waiting for 1 client(s) to finish
 
1251
... Stopped while waiting for request: ...
 
1252
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
 
1253
 
 
1254
    def test_stop_gracefully_tells_handlers_to_stop(self):
 
1255
        server, server_thread = self.make_server()
 
1256
        client_sock = self.connect_to_server(server)
 
1257
        self.say_hello(client_sock)
 
1258
        server_handler, server_side_thread = server._active_connections[0]
 
1259
        self.assertFalse(server_handler.finished)
 
1260
        server._stop_gracefully()
 
1261
        self.assertTrue(server_handler.finished)
 
1262
        client_sock.close()
 
1263
        self.connect_to_server_and_hangup(server)
 
1264
        server_thread.join()
1246
1265
 
1247
1266
 
1248
1267
class SmartTCPTests(tests.TestCase):