私のクライアント (twisted に基づく) は、接続が失われたときにサーバーに自動的に再接続することになっています。この機能をテストする必要があります。@todo コメントが予想される動作について非常に明確である私のテスト方法を次に示します。
@defer.inlineCallbacks
def test_reconnect_on_connection_loss(self):
client = SMPPClientFactory(self.config)
client.reConnect = mock.Mock(wraps=client.reConnect)
# Connect
smpp = yield client.connect()
# Bind
yield smpp.bindAsTransmitter()
# @todo: A connection loss is expected here
# the client is supposed to try reconnections
# for a while, the server then shall start
# again and the client will get connected.
# Unbind & Disconnect
yield smpp.unbindAndDisconnect()
##############
# Assertions :
# Protocol verification
self.assertNotEqual(0, client.reConnect.call_count)
サーバー側では、bindAsTransmitter リクエストを受信した直後に接続を中止しようとしています:
class LooseConnectionOnBindSMSC(SMSC):
def handleBindAsTransmitter(self, reqPDU):
self.sendSuccessResponse(reqPDU)
# Connection is aborted here:
self.transport.abortConnection()
接続は正常に中止され、クライアントは期待どおりに再接続を試み始めますが、サーバーを再び起動する方法が見つかりません。