summaryrefslogtreecommitdiff
path: root/tests/test_events.py
blob: df36969db310299dd0388e3df24e6dee8670b4b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sleekxmpp
import time
from . sleektest import *


class TestEvents(SleekTest):

    def setUp(self):
        self.streamStart()

    def tearDown(self):
        self.streamClose()

    def testEventHappening(self):
        """Test handler working"""
        happened = []

        def handletestevent(event):
            happened.append(True)

        self.xmpp.add_event_handler("test_event", handletestevent)
        self.xmpp.event("test_event")
        self.xmpp.event("test_event")

        # Give the event queue time to process.
        time.sleep(0.1)

        msg = "Event was not triggered the correct number of times: %s"
        self.failUnless(happened == [True, True], msg)

    def testDelEvent(self):
        """Test handler working, then deleted and not triggered"""
        happened = []

        def handletestevent(event):
            happened.append(True)

        self.xmpp.add_event_handler("test_event", handletestevent)
        self.xmpp.event("test_event", {})

        self.xmpp.del_event_handler("test_event", handletestevent)

        # Should not trigger because it was deleted
        self.xmpp.event("test_event", {})

        # Give the event queue time to process.
        time.sleep(0.1)

        msg = "Event was not triggered the correct number of times: %s"
        self.failUnless(happened == [True], msg % happened)

    def testDisposableEvent(self):
        """Test disposable handler working, then not being triggered again."""
        happened = []

        def handletestevent(event):
            happened.append(True)

        self.xmpp.add_event_handler("test_event", handletestevent,
                                    disposable=True)
        self.xmpp.event("test_event", {})

        # Should not trigger because it was deleted
        self.xmpp.event("test_event", {})

        # Give the event queue time to process.
        time.sleep(0.1)

        msg = "Event was not triggered the correct number of times: %s"
        self.failUnless(happened == [True], msg % happened)


suite = unittest.TestLoader().loadTestsFromTestCase(TestEvents)