本文整理汇总了Python中minitwisted.ThreadedReactor.stop_and_get_capture方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadedReactor.stop_and_get_capture方法的具体用法?Python ThreadedReactor.stop_and_get_capture怎么用?Python ThreadedReactor.stop_and_get_capture使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类minitwisted.ThreadedReactor
的用法示例。
在下文中一共展示了ThreadedReactor.stop_and_get_capture方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _main_loop
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop_and_get_capture [as 别名]
class TestSend:
def _main_loop(self):
return time.time() + MAIN_LOOP_DELAY, [DATAGRAM1]
def _callback(self, value):
self.callback_values.append(value)
return [DATAGRAM2]
def _on_datagram_received(self, datagram):
self.datagrams_received.append(datagram)
return time.time() + MAIN_LOOP_DELAY, [DATAGRAM3]
def _crashing_callback(self):
raise CrashError, 'Crash testing'
def setup(self):
self.main_loop_call_counter = 0
self.callback_values = []
self.datagrams_received = []
self.reactor = ThreadedReactor(self._main_loop,
tc.CLIENT_PORT,
self._on_datagram_received,
task_interval=tc.TASK_INTERVAL)
self.reactor.s = _SocketMock()
def test_main_loop_send_data(self):
eq_(self.reactor.s.get_datagrams_sent(), [])
self.reactor.run_one_step()
# main_loop sends DATAGRAM1
eq_(self.reactor.s.get_datagrams_sent(), [DATAGRAM1])
def test_call_asap_send_data(self):
self.reactor.run_one_step()
eq_(self.reactor.s.get_datagrams_sent(), [DATAGRAM1])
self.reactor.call_asap(self._callback, 1)
self.reactor.run_one_step()
eq_(self.reactor.s.get_datagrams_sent(), [DATAGRAM1, DATAGRAM2])
def test_on_datagram_received_send_data(self):
self.reactor.run_one_step()
eq_(self.reactor.s.get_datagrams_sent(), [DATAGRAM1])
self.reactor.s.put_datagram_received(Datagram(DATA1, tc.SERVER_ADDR))
self.reactor.run_one_step()
eq_(self.reactor.s.get_datagrams_sent(), [DATAGRAM1, DATAGRAM3])
def test_capture(self):
self.reactor.start_capture()
ts1 = time.time()
time.sleep(tc.TASK_INTERVAL/2)
# out > DATAGRAM1 (main_loop)
self.reactor.run_one_step()
ts2 = time.time()
incoming_datagram = Datagram(DATA1, tc.SERVER_ADDR)
self.reactor.s.put_datagram_received(incoming_datagram)
time.sleep(tc.TASK_INTERVAL/2)
self.reactor.run_one_step()
# in < incoming_datagram (socket)
# out > DATAGRAM3 (on_datagram_received)
captured_msgs = self.reactor.stop_and_get_capture()
eq_(len(captured_msgs), 3)
for msg in captured_msgs:
print msg
assert ts1 < captured_msgs[0][0] < ts2
eq_(captured_msgs[0][1], tc.SERVER_ADDR)
eq_(captured_msgs[0][2], True) #outgoing
eq_(captured_msgs[0][3], DATA1)
assert captured_msgs[1][0] > ts2
eq_(captured_msgs[1][1], DATAGRAM1.addr)
eq_(captured_msgs[1][2], False) #incoming
eq_(captured_msgs[1][3], DATAGRAM1.data)
assert captured_msgs[2][0] > captured_msgs[1][0]
eq_(captured_msgs[2][1], DATAGRAM3.addr)
eq_(captured_msgs[2][2], True) #outgoing
eq_(captured_msgs[2][3], DATAGRAM3.data)
def teardown(self):
return