我的客户端(基于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()
连接成功中止,我的客户端开始尝试按预期重新连接,但它永远无法让我的服务器再次启动。