1

我的客户端(基于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()

连接成功中止,我的客户端开始尝试按预期重新连接,但它永远无法让我的服务器再次启动。

4

1 回答 1

2

您的服务器仍在运行(据任何人都可以从您问题中的代码中看出)。关闭与客户端的一个连接不会阻止服务器接受新连接。

停止监听端口监听的方法是使用port.stopListening()(注意它返回 a Deferred)。reactor.listenTCP您可以使用另一个(或您第一次开始侦听的任何 API)调用再次开始侦听端口。

于 2011-12-08T15:25:11.630 回答