def test_reload(self):
     """Tests if module is reload safe."""
     b = B()
     m = messenger.Messenger()
     orig_len = len(m._signals)
     reload(messenger)
     m = messenger.Messenger()
     self.assertEqual(len(m._signals), orig_len)
     b.send(1, test=1)
     self.assertEqual(b.a.event, 'method')
     self.assertEqual(ret[0], 'function')
     self.assertEqual(b.a.args, (1, ))
     self.assertEqual(ret[1], (1, ))
     self.assertEqual(b.a.kw, {'test': 1})
     self.assertEqual(ret[2], {'test': 1})
    def test_send_on_dead_ref(self):
        """Test if sending to a gc'd callback works gracefully."""
        class C:
            def foo(self, o, e):
                pass

        c = C()
        c1 = C()
        messenger.connect(c1, 'foo', c.foo)
        messenger.send(c1, 'foo')

        # Test if things behave sanely if a message was sent and one
        # of the callbacks has been gc'd.
        m = messenger.Messenger()
        l1 = len(m._signals[hash(c1)]['foo'])
        #
        del c
        messenger.send(c1, 'foo')
        #
        l2 = len(m._signals[hash(c1)]['foo'])
        # Since 'c' is gc'd this callback should have been cleared
        # out.
        self.assertEqual(l2, l1 - 1)

        # Clean up.
        messenger.disconnect(c1)
 def test_basic(self):
     """Test basic functionality of the messenger."""
     m = messenger.Messenger()
     orig_len = len(m._signals)
     b = B()
     b.send(1, test=1)
     self.assertEqual(b.a.event, 'method')
     self.assertEqual(ret[0], 'function')
     self.assertEqual(b.a.args, (1, ))
     self.assertEqual(ret[1], (1, ))
     self.assertEqual(b.a.kw, {'test': 1})
     self.assertEqual(ret[2], {'test': 1})
     # Ensures that disconnect works and also that there are no
     # reference cycles.
     self.assertEqual(len(m._signals) > orig_len, True)
     del b
     self.assertEqual(len(m._signals), orig_len)