def test_select1(self): import select import socket s1, s2 = socket.socketpair() with hub.Timeout(1, MyException): select.select([s2.fileno()], [], [])
def _wait_bpdu_timer(self): time_exceed = False while True: self.wait_timer_event = hub.Event() message_age = (self.designated_times.message_age if self.designated_times else 0) timer = self.port_times.max_age - message_age timeout = hub.Timeout(timer) try: self.wait_timer_event.wait() except hub.Timeout as t: if t is not timeout: err_msg = 'Internal error. Not my timeout.' raise RyuException(msg=err_msg) self.logger.info('[port=%d] Wait BPDU timer is exceeded.', self.ofport.port_no, extra=self.dpid_str) time_exceed = True finally: timeout.cancel() self.wait_timer_event = None if time_exceed: break if time_exceed: # Bridge.recalculate_spanning_tree hub.spawn(self.wait_bpdu_timeout)
def _wait(self): """ Wait until specific OFP message received or timer is exceeded. """ assert self.waiter is None self.waiter = hub.Event() self.rcv_msgs = [] timeout = False timer = hub.Timeout(WAIT_TIMER) try: self.waiter.wait() except hub.Timeout as t: if t is not timer: raise RyuException('Internal error. Not my timeout.') timeout = True finally: timer.cancel() self.waiter = None if timeout: raise TestTimeout(self.state) if (self.rcv_msgs and isinstance(self.rcv_msgs[0], ofproto_v1_3_parser.OFPErrorMsg)): raise TestReceiveError(self.state, self.rcv_msgs[0])
def test_spawn_exception_joinall(self): def _child(): raise Exception("hoge") threads = [] with hub.Timeout(2): threads.append(hub.spawn(_child)) threads.append(hub.spawn(_child)) hub.sleep(0.5) hub.joinall(threads)
def test_spawn_event1(self): def _child(ev, result): hub.sleep(1) result.append(1) ev.set() ev = hub.Event() result = [] with hub.Timeout(2): hub.spawn(_child, ev, result) ev.wait() assert len(result) == 1
def test_spawn_select1(self): import select import socket def _child(s1): hub.sleep(0.5) s1.send("hoge") s1, s2 = socket.socketpair() with hub.Timeout(1): hub.spawn(_child, s1) select.select([s2.fileno()], [], []) select.select([s2.fileno()], [], []) # return immediately
def test_spawn_kill_die_joinall(self): def _child(result): result.append(1) threads = [] result = [] with hub.Timeout(2): threads.append(hub.spawn(_child, result)) threads.append(hub.spawn(_child, result)) hub.sleep(0.5) for t in threads: hub.kill(t) hub.joinall(threads) assert len(result) == 2
def test_spawn_kill_nowait_joinall(self): # XXX this test relies on the scheduling behaviour. # the intention here is, killing threads before they get active. def _child(result): result.append(1) threads = [] result = [] with hub.Timeout(2): threads.append(hub.spawn(_child, result)) for t in threads: hub.kill(t) hub.joinall(threads) assert len(result) == 0
def start(self): # discard received packets before joining multicast membership packet_socket = self.packet_socket packet_socket.setblocking(0) with hub.Timeout(0.1, False): while True: try: packet_socket.recv(1500) except socket.error: break packet_socket.setblocking(1) self._join_multicast_membership(True) self._join_vrrp_group(True) super(VRRPInterfaceMonitorNetworkDevice, self).start() self.threads.append(hub.spawn(self._recv_loop))
def test_spawn_kill_joinall(self): def _child(ev2, result): ev2.wait() result.append(1) ev2 = hub.Event() threads = [] result = [] with hub.Timeout(2): threads.append(hub.spawn(_child, ev2, result)) threads.append(hub.spawn(_child, ev2, result)) hub.sleep(0.5) for t in threads: hub.kill(t) hub.joinall(threads) assert len(result) == 0
def test_spawn_joinall(self): def _child(ev2, result): ev2.wait() hub.sleep(0.5) result.append(1) raise BaseException("this exception should not be propagated") ev2 = hub.Event() threads = [] result = [] with hub.Timeout(2): threads.append(hub.spawn(_child, ev2, result)) threads.append(hub.spawn(_child, ev2, result)) hub.sleep(0.5) ev2.set() # this should wake up the above created two threads hub.joinall(threads) assert len(result) == 2
def test_spawn_event3(self): def _child(ev, ev2, result): ev2.wait() hub.sleep(0.5) result.append(1) ev.set() ev = hub.Event() ev2 = hub.Event() result = [] with hub.Timeout(2): hub.spawn(_child, ev, ev2, result) hub.spawn(_child, ev, ev2, result) hub.sleep(0.5) ev2.set() # this should wake up the above created two threads ev.wait(timeout=1) assert len(result) == 2
def _state_machine(self): """ Port state machine. Change next status when timer is exceeded or _change_status() method is called.""" role_str = { ROOT_PORT: 'ROOT_PORT ', DESIGNATED_PORT: 'DESIGNATED_PORT ', NON_DESIGNATED_PORT: 'NON_DESIGNATED_PORT' } state_str = { PORT_STATE_DISABLE: 'DISABLE', PORT_STATE_BLOCK: 'BLOCK', PORT_STATE_LISTEN: 'LISTEN', PORT_STATE_LEARN: 'LEARN', PORT_STATE_FORWARD: 'FORWARD' } if self.state is PORT_STATE_DISABLE: self.ofctl.set_port_status(self.ofport, self.state) while True: self.logger.info('[port=%d] %s / %s', self.ofport.port_no, role_str[self.role], state_str[self.state], extra=self.dpid_str) self.state_event = hub.Event() timer = self._get_timer() if timer: timeout = hub.Timeout(timer) try: self.state_event.wait() except hub.Timeout as t: if t is not timeout: err_msg = 'Internal error. Not my timeout.' raise RyuException(msg=err_msg) new_state = self._get_next_state() self._change_status(new_state, thread_switch=False) finally: timeout.cancel() else: self.state_event.wait() self.state_event = None
def test_select3(self): import select import socket s1, s2 = socket.socketpair() with hub.Timeout(1, MyException): list = [s1.fileno(), s2.fileno()] rlist, wlist, xlist = select.select(list, list, list) assert not s1.fileno() in rlist assert not s2.fileno() in rlist # the following two assertions are commented out because one of # them fails with eventlet-patched select. # assert s1.fileno() in wlist # assert s2.fileno() in wlist # note: eventlet-patched select returns at most one file. assert (s1.fileno() in wlist) or (s2.fileno() in wlist) assert not s1.fileno() in xlist assert not s2.fileno() in xlist
def test_0_timeout3(self): with hub.Timeout(1): hub.sleep(0.1) # sleep some more to ensure timer cancelation hub.sleep(2)
def test_0_timeout2(self): with hub.Timeout(0.1, MyException): hub.sleep(1)
def test_0_timeout1(self): with hub.Timeout(0.1): hub.sleep(1)
def test_event1(self): ev = hub.Event() ev.set() with hub.Timeout(1): ev.wait() # should return immediately
def test_select2(self): import select with hub.Timeout(1, MyException): select.select([], [], [], 0) # timeout immediately