def test_callback_remove(): """ Test removing of callbacks via the "off" method """ obj = EventMixin() def callback(event_origin=None): return obj.on("test", callback) assert (callback, False) in obj.event_listeners["test"] obj.off("test", callback) assert (callback, False) not in obj.event_listeners["test"] obj.on("test", callback, once=True) assert (callback, True) in obj.event_listeners["test"] assert (callback, False) not in obj.event_listeners["test"] obj.off("test", callback, once=True) assert (callback, True) not in obj.event_listeners["test"]
def __init__(self, host, port, sock=None, temporary=False): EventMixin.__init__(self) asynchat.async_chat.__init__(self, sock, map=make_map(host, port)) self.set_terminator(TERMINATOR) self.received_data = "" self.temporary = temporary self.sock = sock self.dispatcher = None self.time = 0
def __init__(self, transport_content_type="json"): threading.Thread.__init__(self) EventMixin.__init__(self) self.close_when_ready = False self.transport_content_type = transport_content_type # munge codec instance from transport_content_type self.codecClass = munge.get_codec(transport_content_type) self.codec = self.codecClass()
def __init__(self, data, *args, **kwargs): EventMixin.__init__(self) self.meta = {} self.data = data self.args = args self.kwargs = kwargs self.meta.update(id=str(uuid.uuid4())) self.responder = None self.response_received = False self.response_message = None self.responded = False
def __init__(self, receive=None, send=None, respond=None): EventMixin.__init__(self) self.connection_receive = receive self.connection_respond = respond self.connection_send = send self.meta = {} self.name = "" if receive: receive.on("receive", self.on_receive)
def test_callback_once(): """ Test that callbacks attached with the once flag will only be triggered once and removed afterwards """ obj = EventMixin() data = { "a" : 1 } expected = { "a" : 2 } def callback(val, event_origin=None): data["a"] += val obj.on("increase", callback, once=True) obj.trigger("increase", 1) assert data["a"] == expected["a"] # at the second time the event triggers, the callback # is no longer attached, so the value in data.a should # remain the same obj.trigger("increase", 1) assert data["a"] == expected["a"]
def test_callback(): """ Test adding a callback for an event """ obj = EventMixin() data = { "a" : 1 } expected = { "a" : 2, "b": 3 } # the callback will increase the value # of data.a everytime it is triggered def callback(val, event_origin=None): data["a"] += val # attach callback to event obj.on("increase", callback) # trigger event (val=1) obj.trigger("increase", 1) assert data["a"] == expected["a"] # trigger event (val=1) obj.trigger("increase", 1) assert data["a"] == expected["b"]
def __init__(self, name=None, **kwargs): """ Keyword Arguments: - name (str): name for this link, if not provided a unique default value will be used - receive (Connection): if supplied a "main" wire will be established using this connection as a receiver - send (Connection): if supplied a "main" wire will be established using this connection as a sender - respond (Connection): if supplied a "main" wire will be established using this connection as a responder """ EventMixin.__init__(self) self.name = name or get_link_name() self.main = None if "receive" in kwargs or "send" in kwargs or "respond" in kwargs: self.wire("main", receive=kwargs.get("receive"), send=kwargs.get("send"), respond=kwargs.get("respond"))
def test_event_origin(): """ Test that event_origin references is passed correctly to callbacks """ obj = EventMixin() def callback(event_origin=None): assert event_origin == obj obj.on("test", callback) obj.trigger("test")