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

サーバーはまだ実行されています(質問のコードから誰でもわかる限り)。クライアントへの 1 つの接続を閉じても、サーバーが新しい接続を受け入れるのを止めることはありません。

リッスンしているポートのリッスンを停止する方法はport.stopListening()( を返すことに注意してくださいDeferred) です。reactor.listenTCP別の(または最初にリッスンを開始するために使用した API) 呼び出しを使用して、ポートでリッスンを再開できます。

于 2011-12-08T15:25:11.630 に答える