有没有办法在测试之间保持在线连接?
我试图在运行多个测试集时保持用户登录,但注意到意外行为:每次测试后反应器停止(因此一旦建立连接变得不可用)。
为了探索这一点,我准备了示例测试文件(如下)。
这将创建简单的监听回声服务器并将其存储在类字段中。运行 3 个测试。服务器期望在所有测试期间正常运行(实际上它只在第一次测试结束之前运行)。
例子:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import logging
from twisted.python import log
from twisted.internet import defer, base, reactor
from twisted.trial import unittest
from twisted.internet.protocol import Protocol, Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
observer = log.PythonLoggingObserver()
observer.start()
logging.basicConfig(level=logging.DEBUG)
class Echo(Protocol):
'''Protocol from twistedmatrix examples'''
def connectionMade(self):
log.msg('Got incomming connection')
self.transport.write("An apple a day keeps the doctor away\r\n")
def connectionLost(self, reason):
log.msg('Connection lost due to: %s' % reason)
def dataReceived(self, data):
self.transport.write(data)
log.msg('Got some data: %s' % data)
class EchoFactory(Factory):
'''Factory from twistedmatrix examples'''
def buildProtocol(self, addr):
return Echo()
class SampleTest(unittest.TestCase):
'''Sample test case class derived straight from twisteds TestCase'''
is_a_first_test = True
endppoint = None
def logLater(self, msgg = None):
log.msg('called later message')
@defer.inlineCallbacks
def setUp(self):
if self.__class__.is_a_first_test:
self.__class__.endpoint = TCP4ServerEndpoint(reactor, 8007)
self.__class__.endpoint.listen(EchoFactory())
self.__class__.is_a_first_test = False
log.msg('setting Up ... You may try (re)connecting now!!!')
log.msg('We have endpoint: %s' % self.endpoint)
yield reactor.callLater(5, self.logLater)
log.msg('setUp done')
def tearDown(self):
log.msg('tearDown started')
result = defer.Deferred()
result.addCallback(self.logLater)
reactor.callLater(5, result.callback, 'tearDown msg')
log.msg('leaving tearDown')
return result
@defer.inlineCallbacks
def test_00(self):
log.msg('00 test body')
sample_defer = defer.Deferred()
sample_defer.addCallback(self.logLater)
reactor.callLater(5, sample_defer.callback, 'Some sample action 00')
log.msg('waiting reactor deferred')
yield sample_defer
log.msg('done with test body')
@defer.inlineCallbacks
def test_01(self):
log.msg('01 test body')
sample_defer = defer.Deferred()
sample_defer.addCallback(self.logLater)
reactor.callLater(5, sample_defer.callback, 'Some sample action 01')
log.msg('waiting reactor deferred')
yield sample_defer
@defer.inlineCallbacks
def test_02(self):
log.msg('02 test body')
sample_defer = defer.Deferred()
sample_defer.addCallback(self.logLater)
reactor.callLater(5, sample_defer.callback, 'Some sample action 02')
log.msg('waiting reactor deferred')
yield sample_defer
运行上面的文件:
trial test-file.py
在每次测试结束时显示“主循环终止”。在该端口继续监听所有测试之后(根据 netstat -n4lt)。但是在第 2 和第 3 个测试体期间 telnet 连接时没有回声(仅适用于第 1 个)。
Twisted doc http://twistedmatrix.com/documents/current/core/howto/testing.html#auto3说: “试用在单个进程中运行整个测试套件(超过四千个测试),使用单个反应器” (不幸的是我的 twistedmatrix 注册请求仍未得到验证,因此无法在那里提出问题)。
在我的真实案例中,每次测试都需要很长时间才能重复连接到服务器,所以我想对每一类案例至少执行一次。
那么,有没有办法在测试之间保持在线连接?
PS 使用 python 2.7.1、python-twisted 10.2.0-1、Ubuntu 11.04