def emits_not_(gen, unwanted_actions): assert isinstance(unwanted_actions, list) actions = list(flatten(gen)) ok_( not any(any(x == y for y in actions) for x in unwanted_actions), "actions contained the unwanted %r" % (list(set(unwanted_actions).intersection(set(actions))), ))
def _execute(self, fn, *args, **kwargs): g = fn(*args, **kwargs) if g is None: return insock_send, outsock_send, on_receive = self._insock.send_multipart, self._outsock.send_multipart, self._on_receive with self._lock: for action in flatten(g): cmd = action[0] # if cmd not in (NextBeat,): # dbg("%s -> %s: %s" % (fn.__name__.ljust(25), cmd, ", ".join(repr(x) for x in action[1:]))) if cmd is Send: _, use_sock, nid, version, msg_h = action (outsock_send if use_sock == OUT else insock_send)((nid, struct.pack(MSG_HEADER_FORMAT, MIN_VERSION_VALUE + version) + msg_h.serialize())) elif cmd is Receive: _, sender_nid, msg_bytes = action on_receive(sender_nid, msg_bytes) elif cmd is RelaySend: _, use_sock, relay_nid, relayee_nid, msg_h = action (outsock_send if use_sock == OUT else insock_send)((relay_nid, SIG_RELAY_SEND + relayee_nid + '\0' + msg_h.serialize())) elif cmd is RelayForward: _, use_sock, recipient_nid, relayer_nid, relayed_bytes = action (outsock_send if use_sock == OUT else insock_send)((recipient_nid, SIG_RELAY_FORWARDED + relayer_nid + '\0' + relayed_bytes)) elif cmd is Ping: _, use_sock, nid, version = action (outsock_send if use_sock == OUT else insock_send)((nid, struct.pack(MSG_HEADER_FORMAT, MIN_VERSION_VALUE + version))) elif cmd is NextBeat: _, time_to_next = action if self._heartbeater is not _DELETED: self._heartbeater = spawn_later(time_to_next, self._heartbeat) elif cmd is RelaySigNew: _, use_sock, nid = action (outsock_send if use_sock == OUT else insock_send)((nid, SIG_NEW_RELAY)) elif cmd is RelayConnect: _, use_sock, relay_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)((relay_nid, SIG_RELAY_CONNECT + relayee_nid)) elif cmd is RelaySigConnected: _, use_sock, relayer_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)((relayer_nid, SIG_RELAY_CONNECTED + relayee_nid)) elif cmd is RelaySigNodeDown: _, use_sock, relayer_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)((relayer_nid, SIG_RELAY_NODEDOWN + relayee_nid)) elif cmd is RelayNvm: _, use_sock, relay_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)((relay_nid, SIG_RELAY_NVM + relayee_nid)) elif cmd is SendFailed: _, msg_h = action msg_h.send_failed() elif cmd is SigDisconnect: _, use_sock, nid = action (outsock_send if use_sock == OUT else insock_send)([nid, SIG_DISCONNECT]) elif cmd is NodeDown: _, nid = action for watch_handle in self._watched_nodes.pop(nid, []): self._on_node_down(watch_handle, nid) elif cmd is Connect: _, naddr = action if naddr not in self.FAKE_INACCESSIBLE_NADDRS: zmqaddr = naddr_to_zmq_endpoint(naddr) if zmqaddr: self._outsock.connect(zmqaddr) else: pass # TODO: would be nicer if we used this information and notified an immediate disconnect sleep(0.001) elif cmd is Disconnect: _, naddr = action if naddr not in self.FAKE_INACCESSIBLE_NADDRS: zmqaddr = naddr_to_zmq_endpoint(naddr) if zmqaddr: try: self._outsock.disconnect(zmqaddr) except zmq.ZMQError: pass elif cmd is Bind: _, naddr = action zmqaddr = naddr_to_zmq_endpoint(naddr) if not zmqaddr: raise Exception("Failed to bind to %s" % (naddr,)) self._insock.bind(zmqaddr) else: assert False, "unknown command: %r" % (cmd,)
def just_(gen): if gen is None: return for x in flatten(gen): pass
def emits_not_(gen, unwanted_actions): assert isinstance(unwanted_actions, list) actions = list(flatten(gen)) ok_(not any(any(x == y for y in actions) for x in unwanted_actions), "actions contained the unwanted %r" % (list(set(unwanted_actions).intersection(set(actions))),))
def emits_(gen, expected_actions): assert isinstance(expected_actions, list) actions = list(flatten(gen)) if sorted(actions) != sorted(expected_actions): raise AssertionError("actions did not match:\n\tgot: %r\n\texp: %r" % (actions, expected_actions))
def _execute(self, fn, *args, **kwargs): g = fn(*args, **kwargs) if g is None: return insock_send, outsock_send, on_receive = self._insock.send_multipart, self._outsock.send_multipart, self._on_receive with self._lock: for action in flatten(g): cmd = action[0] # if cmd not in (NextBeat,): # dbg("%s -> %s: %s" % (fn.__name__.ljust(25), cmd, ", ".join(repr(x) for x in action[1:]))) if cmd is Send: _, use_sock, nid, version, msg_h = action (outsock_send if use_sock == OUT else insock_send)( (nid, struct.pack(MSG_HEADER_FORMAT, MIN_VERSION_VALUE + version) + msg_h.serialize())) elif cmd is Receive: _, sender_nid, msg_bytes = action on_receive(sender_nid, msg_bytes) elif cmd is RelaySend: _, use_sock, relay_nid, relayee_nid, msg_h = action (outsock_send if use_sock == OUT else insock_send)( (relay_nid, SIG_RELAY_SEND + relayee_nid + '\0' + msg_h.serialize())) elif cmd is RelayForward: _, use_sock, recipient_nid, relayer_nid, relayed_bytes = action (outsock_send if use_sock == OUT else insock_send)( (recipient_nid, SIG_RELAY_FORWARDED + relayer_nid + '\0' + relayed_bytes)) elif cmd is Ping: _, use_sock, nid, version = action (outsock_send if use_sock == OUT else insock_send)( (nid, struct.pack(MSG_HEADER_FORMAT, MIN_VERSION_VALUE + version))) elif cmd is NextBeat: _, time_to_next = action if self._heartbeater is not _DELETED: self._heartbeater = spawn_later( time_to_next, self._heartbeat) elif cmd is RelaySigNew: _, use_sock, nid = action (outsock_send if use_sock == OUT else insock_send)( (nid, SIG_NEW_RELAY)) elif cmd is RelayConnect: _, use_sock, relay_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)( (relay_nid, SIG_RELAY_CONNECT + relayee_nid)) elif cmd is RelaySigConnected: _, use_sock, relayer_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)( (relayer_nid, SIG_RELAY_CONNECTED + relayee_nid)) elif cmd is RelaySigNodeDown: _, use_sock, relayer_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)( (relayer_nid, SIG_RELAY_NODEDOWN + relayee_nid)) elif cmd is RelayNvm: _, use_sock, relay_nid, relayee_nid = action (outsock_send if use_sock == OUT else insock_send)( (relay_nid, SIG_RELAY_NVM + relayee_nid)) elif cmd is SendFailed: _, msg_h = action msg_h.send_failed() elif cmd is SigDisconnect: _, use_sock, nid = action (outsock_send if use_sock == OUT else insock_send)( [nid, SIG_DISCONNECT]) elif cmd is NodeDown: _, nid = action for watch_handle in self._watched_nodes.pop(nid, []): self._on_node_down(watch_handle, nid) elif cmd is Connect: _, naddr = action if naddr not in self.FAKE_INACCESSIBLE_NADDRS: zmqaddr = naddr_to_zmq_endpoint(naddr) if zmqaddr: self._outsock.connect(zmqaddr) sleep(0.001) elif cmd is Disconnect: _, naddr = action if naddr not in self.FAKE_INACCESSIBLE_NADDRS: zmqaddr = naddr_to_zmq_endpoint(naddr) if zmqaddr: try: self._outsock.disconnect(zmqaddr) except zmq.ZMQError: pass elif cmd is Bind: _, naddr = action zmqaddr = naddr_to_zmq_endpoint(naddr) if not zmqaddr: raise Exception("Failed to bind to %s" % (naddr, )) self._insock.bind(zmqaddr) else: assert False, "unknown command: %r" % (cmd, )