summaryrefslogtreecommitdiff
path: root/tests/test_events.py
blob: a41ed017a44ad868a1bd0ed3c1c54466b2077dfe (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import time
import unittest
from sleekxmpp.test import SleekTest


class TestEvents(SleekTest):

    def setUp(self):
        self.stream_start()

    def tearDown(self):
        self.stream_close()

    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 testAddDelAddEvent(self):
        """Test adding, then removing, then adding an event handler."""
        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", {})

        self.xmpp.add_event_handler("test_event", handletestevent)
        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 % 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)