嗨,我是Twisted的新用户,我想将其用作我的客户端/服务器升级。
我有一个进行一些单元测试的软件,我想添加Twisted。
有2台计算机,1台用于控制测试(客户端),1台用于测试特定单元(服务器)
对于服务器部分,使用双绞线没有问题。因为激活 react 堆时,服务器正在监听并等待客户端的请求。
在客户端部分,我有一些问题。
凝视客户时
软件进入事件驱动模式并等待事件...(连接,发送...)
问题是我想做很多事情,而不仅仅是看我的交流。
可以说我有一个功能:
我想让UnitTest方法将请求发送给扭曲的客户端,这可能吗?
最佳答案
如果用于测试,请查看文档:unit test with trial
要编写测试,您可以采用经典的回调方法:
from twisted.internet import defer
class SomeWarningsTests(TestCase):
def setUp(self):
#init your connection , return the deferred that callback when is ready
def tearDown(self):
# disconnect from the server
def getVoltage(self):
#connect to you serveur and get the voltage , so return deferred
def test_voltage(self):
def testingVoltage(result):
if not result:
raise Exception("this not normal")
return result
return self.getVoltage.addCallback(testingVoltage)
#other way
def getCurrent(self):
#connect to you serveur and get the current(?) , so return deferred
@defer.inlineCallbacks
def test_current(self):
current = yield self.getCurrent()
if not current:
raise Exception("this not normal")
defer.returnValue(current)