Exemplo n.º 1
0
class TestWeakMethodSlot(object):
    def setup_method(self, method):
        class MyObject(object):
            def __init__(self):
                self.called = False

            def slot(self, **kwargs):
                self.called = True

        self.obj_ref = MyObject()
        self.slot = Slot(self.obj_ref.slot, weak=True)
        self.signal = Signal()
        self.signal.connect(self.slot)

    def test_alive(self):
        assert self.slot.is_alive

    def test_call(self):
        self.signal.emit(testing=1234)
        assert self.obj_ref.called

    def test_gc(self):
        self.obj_ref = None
        assert not self.slot.is_alive
        self.signal.emit(testing=1234)
Exemplo n.º 2
0
class TestWeakMethodSlot(object):
    def setup_method(self, method):

        class MyObject(object):

            def __init__(self):
                self.called = False

            def slot(self, **kwargs):
                self.called = True

        self.obj_ref = MyObject()
        self.slot = Slot(self.obj_ref.slot, weak=True)
        self.signal = Signal()
        self.signal.connect(self.slot)

    def test_alive(self):
        assert self.slot.is_alive

    def test_call(self):
        self.signal.emit(testing=1234)
        assert self.obj_ref.called

    def test_gc(self):
        self.obj_ref = None
        assert not self.slot.is_alive
        self.signal.emit(testing=1234)
Exemplo n.º 3
0
class TestSignalConnect(object):
    def setup_method(self, method):
        self.signal = Signal()

    def test_connect_with_kwargs(self):
        def cb(**kwargs):
            pass

        self.signal.connect(cb)

    def test_connect_without_kwargs(self):
        def cb():
            pass

        with pytest.raises(SlotMustAcceptKeywords):
            self.signal.connect(cb)
Exemplo n.º 4
0
class TestSignalConnect(object):
    def setup_method(self, method):
        self.signal = Signal()

    def test_connect_with_kwargs(self):
        def cb(**kwargs):
            pass

        self.signal.connect(cb)

    def test_connect_without_kwargs(self):
        def cb():
            pass

        with pytest.raises(SlotMustAcceptKeywords):
            self.signal.connect(cb)
Exemplo n.º 5
0
class TestException(object):
    def setup_method(self, method):
        self.signal = Signal(threadsafe=False)
        self.seen_exception = False

        def failing_slot(**args):
            raise MyTestError('die!')

        self.signal.connect(failing_slot)

    def test_emit_exception(self):
        try:
            self.signal.emit()
        except MyTestError:
            self.seen_exception = True

        assert self.seen_exception
Exemplo n.º 6
0
class TestException(object):
    def setup_method(self, method):
        self.signal = Signal(threadsafe=False)
        self.seen_exception = False

        def failing_slot(**args):
            raise MyTestError('die!')

        self.signal.connect(failing_slot)

    def test_emit_exception(self):
        try:
            self.signal.emit()
        except MyTestError:
            self.seen_exception = True

        assert self.seen_exception
Exemplo n.º 7
0
    def test_semaphore(self, inspect):
        slot = mock.Mock()
        slot.side_effect = lambda **k: time.sleep(.3)

        signal = Signal('tost')
        signal.connect(slot)

        x = Task.get_or_create(signal, dict(some_kwarg='foo'))
        y = Task.get_or_create(signal, dict(some_kwarg='foo'))

        eventlet.spawn(x)
        time.sleep(.1)
        eventlet.spawn(y)
        time.sleep(.1)

        assert slot.call_count == 1
        time.sleep(.4)
        assert slot.call_count == 2
Exemplo n.º 8
0
    def test_semaphore(self, inspect):
        slot = mock.Mock()
        slot.side_effect = lambda **k: time.sleep(.3)

        signal = Signal('tost')
        signal.connect(slot)

        x = Task.get_or_create(signal, dict(some_kwarg='foo'))
        y = Task.get_or_create(signal, dict(some_kwarg='foo'))

        eventlet.spawn(x)
        time.sleep(.1)
        eventlet.spawn(y)
        time.sleep(.1)

        assert slot.call_count == 1
        time.sleep(.4)
        assert slot.call_count == 2
Exemplo n.º 9
0
class TestSignal(object):
    def setup_method(self, method):
        self.signal_a = Signal(threadsafe=True)
        self.signal_b = Signal(args=['foo'])

        self.slot_a = mock.Mock(spec=lambda **kwargs: None)
        self.slot_a.return_value = None
        self.slot_b = mock.Mock(spec=lambda **kwargs: None)
        self.slot_b.return_value = None

    def test_is_connected(self, inspect):
        self.signal_a.connect(self.slot_a)

        assert self.signal_a.is_connected(self.slot_a)
        assert not self.signal_a.is_connected(self.slot_b)
        assert not self.signal_b.is_connected(self.slot_a)
        assert not self.signal_b.is_connected(self.slot_b)

    def test_emit_one_slot(self, inspect):
        self.signal_a.connect(self.slot_a)

        self.signal_a.emit()

        self.slot_a.assert_called_once_with()
        assert self.slot_b.call_count == 0

    def test_emit_two_slots(self, inspect):
        self.signal_a.connect(self.slot_a)
        self.signal_a.connect(self.slot_b)

        self.signal_a.emit()

        self.slot_a.assert_called_once_with()
        self.slot_b.assert_called_once_with()

    def test_emit_one_slot_with_arguments(self, inspect):
        self.signal_b.connect(self.slot_a)

        self.signal_b.emit(foo='bar')

        self.slot_a.assert_called_once_with(foo='bar')
        assert self.slot_b.call_count == 0

    def test_emit_two_slots_with_arguments(self, inspect):
        self.signal_b.connect(self.slot_a)
        self.signal_b.connect(self.slot_b)

        self.signal_b.emit(foo='bar')

        self.slot_a.assert_called_once_with(foo='bar')
        self.slot_b.assert_called_once_with(foo='bar')

    def test_reconnect_does_not_duplicate(self, inspect):
        self.signal_a.connect(self.slot_a)
        self.signal_a.connect(self.slot_a)
        self.signal_a.emit()

        self.slot_a.assert_called_once_with()

    def test_disconnect_does_not_fail_on_not_connected_slot(self, inspect):
        self.signal_a.disconnect(self.slot_b)
Exemplo n.º 10
0
class WAEventHandler(object):

    def __init__(self, conn):
        self.connecting = Signal()
        self.connected = Signal()
        self.sleeping = Signal()
        self.disconnected = Signal()
        self.reconnecting = Signal()

        '''(remote)'''
        self.typing = Signal()
        '''(remote)'''
        self.typingPaused = Signal()


        '''(remote)'''
        self.available = Signal()
        '''(remote)'''
        self.unavailable = Signal()

        self.messageSent = Signal()

        self.messageDelivered = Signal()

        '''(fmsg)'''
        self.messageReceived = Signal()
        '''messageReceived with fmsg.author'''
        self.groupMessageReceived = Signal()


        '''
        (fmsg)
        -> media_type: image, audio, video, location, vcard
        -> data
            image:
            -> url
            -> preview
            audio, video:
            -> url
            location:
            -> latitude
            -> longitude
            -> preview
            vcard:
            -> contact (vcard format)
        '''
        self.mediaReceived = Signal()

        '''mediaReceived with fmsg.author'''
        self.groupMediaReceived = Signal()

        '''(group, author, subject)'''
        self.newGroupSubject = Signal()

        '''(group, who)'''
        self.groupAdd = Signal()

        '''(group, who)'''
        self.groupRemove = Signal()

        ''' (groups: [{subject, id, owner}]) '''
        self.groupsReceived = Signal()

        self.groupListReceived = Signal()

        self.lastSeenUpdated = Signal()

        self.sendTyping = Signal()
        self.sendPaused = Signal()
        self.getLastOnline = Signal()

        self.disconnectRequested = Signal()
        self.disconnectRequested.connect(self.onDisconnectRequested)

        self.loginFailed = Signal()
        self.loginSuccess = Signal()
        self.connectionError = Signal()
        self.conn = conn

        self.sendTyping.connect(self.conn.sendTyping)
        self.sendPaused.connect(self.conn.sendPaused)
        self.getLastOnline.connect(self.conn.getLastOnline)

        self.startPingTimer()

    def onDirty(self, categories):
        '''Receive groups??'''
        pass

    def onAccountChanged(self, account_kind, expire):
        pass

    def onRelayRequest(
        self,
        pin,
        timeoutSeconds,
        idx,
        ):
        pass

    def sendPing(self):
        self.startPingTimer()
        self.conn.sendPing()

    def startPingTimer(self):
        self.pingTimer = threading.Timer(180, self.sendPing)
        self.pingTimer.start()

    def onDisconnectRequested(self):
        self.pingTimer.cancel()

    def onPing(self, idx):
        self.conn.sendPong(idx)

    def networkAvailable(self):
        pass

    def networkDisconnected(self):
        self.sleeping.emit()

    def networkUnavailable(self):
        self.disconnected.emit()

    def onUnavailable(self):
        self.conn.sendUnavailable()

    def conversationOpened(self, jid):
        pass

    def onAvailable(self):
        self.conn.sendAvailable()

    def message_received(self, fmsg):
        if hasattr(fmsg, 'type'):
            if fmsg.type == "chat":
                if fmsg.remote.endswith('@g.us'):
                    self.groupMessageReceived.emit(fmsg)
                else:
                    self.messageReceived.emit(fmsg)
            elif fmsg.type == "media":
                if fmsg.remote.endswith('@g.us'):
                    self.groupMediaReceived.emit(fmsg)
                else:
                    self.mediaReceived.emit(fmsg)
        if fmsg.wants_receipt:
            self.conn.sendMessageReceived(fmsg)

    def subjectReceiptRequested(self, to, idx):
        self.conn.sendSubjectReceived(to, idx)

    def presence_available_received(self, remote):
        if remote == self.conn.jid:
            return
        self.available.emit(remote)

    def presence_unavailable_received(self, remote):
        if remote == self.conn.jid:
            return
        self.unavailable.emit(remote)

    def typing_received(self, remote):
        self.typing.emit(remote)

    def paused_received(self, remote):
        self.typingPaused.emit(remote)

    def message_error(self, fmsg, errorCode):
        pass

    def message_status_update(self, fmsg):
        pass
Exemplo n.º 11
0
class TestSignal(object):
    def setup_method(self, method):
        self.signal_a = Signal(threadsafe=True)
        self.signal_b = Signal(args=['foo'])

        self.slot_a = mock.Mock(spec=lambda **kwargs: None)
        self.slot_a.return_value = None
        self.slot_b = mock.Mock(spec=lambda **kwargs: None)
        self.slot_b.return_value = None

    def test_is_connected(self, inspect):
        self.signal_a.connect(self.slot_a)

        assert self.signal_a.is_connected(self.slot_a)
        assert not self.signal_a.is_connected(self.slot_b)
        assert not self.signal_b.is_connected(self.slot_a)
        assert not self.signal_b.is_connected(self.slot_b)

    def test_emit_one_slot(self, inspect):
        self.signal_a.connect(self.slot_a)

        self.signal_a.emit()

        self.slot_a.assert_called_once_with()
        assert self.slot_b.call_count == 0

    def test_emit_two_slots(self, inspect):
        self.signal_a.connect(self.slot_a)
        self.signal_a.connect(self.slot_b)

        self.signal_a.emit()

        self.slot_a.assert_called_once_with()
        self.slot_b.assert_called_once_with()

    def test_emit_one_slot_with_arguments(self, inspect):
        self.signal_b.connect(self.slot_a)

        self.signal_b.emit(foo='bar')

        self.slot_a.assert_called_once_with(foo='bar')
        assert self.slot_b.call_count == 0

    def test_emit_two_slots_with_arguments(self, inspect):
        self.signal_b.connect(self.slot_a)
        self.signal_b.connect(self.slot_b)

        self.signal_b.emit(foo='bar')

        self.slot_a.assert_called_once_with(foo='bar')
        self.slot_b.assert_called_once_with(foo='bar')

    def test_reconnect_does_not_duplicate(self, inspect):
        self.signal_a.connect(self.slot_a)
        self.signal_a.connect(self.slot_a)
        self.signal_a.emit()

        self.slot_a.assert_called_once_with()

    def test_disconnect_does_not_fail_on_not_connected_slot(self, inspect):
        self.signal_a.disconnect(self.slot_b)
Exemplo n.º 12
0
class WAEventHandler(object):
    def __init__(self, conn):
        self.connecting = Signal()
        self.connected = Signal()
        self.sleeping = Signal()
        self.disconnected = Signal()
        self.reconnecting = Signal()
        '''(remote)'''
        self.typing = Signal()
        '''(remote)'''
        self.typingPaused = Signal()
        '''(remote)'''
        self.available = Signal()
        '''(remote)'''
        self.unavailable = Signal()

        self.messageSent = Signal()

        self.messageDelivered = Signal()
        '''(fmsg)'''
        self.messageReceived = Signal()
        '''messageReceived with fmsg.author'''
        self.groupMessageReceived = Signal()
        '''
        (fmsg)
        -> media_type: image, audio, video, location, vcard
        -> data
            image:
            -> url
            -> preview
            audio, video:
            -> url
            location:
            -> latitude
            -> longitude
            -> preview
            vcard:
            -> contact (vcard format)
        '''
        self.mediaReceived = Signal()
        '''mediaReceived with fmsg.author'''
        self.groupMediaReceived = Signal()
        '''(group, author, subject)'''
        self.newGroupSubject = Signal()
        '''(group, who)'''
        self.groupAdd = Signal()
        '''(group, who)'''
        self.groupRemove = Signal()
        ''' (groups: [{subject, id, owner}]) '''
        self.groupsReceived = Signal()

        self.groupListReceived = Signal()

        self.lastSeenUpdated = Signal()

        self.sendTyping = Signal()
        self.sendPaused = Signal()
        self.getLastOnline = Signal()

        self.disconnectRequested = Signal()
        self.disconnectRequested.connect(self.onDisconnectRequested)

        self.loginFailed = Signal()
        self.loginSuccess = Signal()
        self.connectionError = Signal()
        self.conn = conn

        self.sendTyping.connect(self.conn.sendTyping)
        self.sendPaused.connect(self.conn.sendPaused)
        self.getLastOnline.connect(self.conn.getLastOnline)

        self.startPingTimer()

    def onDirty(self, categories):
        '''Receive groups??'''
        pass

    def onAccountChanged(self, account_kind, expire):
        pass

    def onRelayRequest(
        self,
        pin,
        timeoutSeconds,
        idx,
    ):
        pass

    def sendPing(self):
        self.startPingTimer()
        self.conn.sendPing()

    def startPingTimer(self):
        self.pingTimer = threading.Timer(180, self.sendPing)
        self.pingTimer.start()

    def onDisconnectRequested(self):
        self.pingTimer.cancel()

    def onPing(self, idx):
        self.conn.sendPong(idx)

    def networkAvailable(self):
        pass

    def networkDisconnected(self):
        self.sleeping.emit()

    def networkUnavailable(self):
        self.disconnected.emit()

    def onUnavailable(self):
        self.conn.sendUnavailable()

    def conversationOpened(self, jid):
        pass

    def onAvailable(self):
        self.conn.sendAvailable()

    def message_received(self, fmsg):
        if hasattr(fmsg, 'type'):
            if fmsg.type == "chat":
                if fmsg.remote.endswith('@g.us'):
                    self.groupMessageReceived.emit(fmsg)
                else:
                    self.messageReceived.emit(fmsg)
            elif fmsg.type == "media":
                if fmsg.remote.endswith('@g.us'):
                    self.groupMediaReceived.emit(fmsg)
                else:
                    self.mediaReceived.emit(fmsg)
        if fmsg.wants_receipt:
            self.conn.sendMessageReceived(fmsg)

    def subjectReceiptRequested(self, to, idx):
        self.conn.sendSubjectReceived(to, idx)

    def presence_available_received(self, remote):
        if remote == self.conn.jid:
            return
        self.available.emit(remote)

    def presence_unavailable_received(self, remote):
        if remote == self.conn.jid:
            return
        self.unavailable.emit(remote)

    def typing_received(self, remote):
        self.typing.emit(remote)

    def paused_received(self, remote):
        self.typingPaused.emit(remote)

    def message_error(self, fmsg, errorCode):
        pass

    def message_status_update(self, fmsg):
        pass