##// END OF EJS Templates
removed complexity test
Barry Wark -
Show More
@@ -1,179 +1,171 b''
1 1 # encoding: utf-8
2 2
3 3 """This file contains unittests for the notification.py module."""
4 4
5 5 __docformat__ = "restructuredtext en"
6 6
7 7 #-----------------------------------------------------------------------------
8 8 # Copyright (C) 2008 The IPython Development Team
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 import unittest
19 19 import IPython.kernel.core.notification as notification
20 20 from nose.tools import timed
21 21
22 22 #
23 23 # Supporting test classes
24 24 #
25 25
26 26 class Observer(object):
27 27 """docstring for Observer"""
28 28 def __init__(self, expectedType, expectedSender,
29 29 center=notification.sharedCenter, **kwargs):
30 30 super(Observer, self).__init__()
31 31 self.expectedType = expectedType
32 32 self.expectedSender = expectedSender
33 33 self.expectedKwArgs = kwargs
34 34 self.recieved = False
35 35 center.add_observer(self.callback,
36 36 self.expectedType,
37 37 self.expectedSender)
38 38
39 39
40 40 def callback(self, theType, sender, args={}):
41 41 """callback"""
42 42
43 43 assert(theType == self.expectedType or
44 44 self.expectedType == None)
45 45 assert(sender == self.expectedSender or
46 46 self.expectedSender == None)
47 47 assert(args == self.expectedKwArgs)
48 48 self.recieved = True
49 49
50 50
51 51 def verify(self):
52 52 """verify"""
53 53
54 54 assert(self.recieved)
55 55
56 56 def reset(self):
57 57 """reset"""
58 58
59 59 self.recieved = False
60 60
61 61
62 62
63 63 class Notifier(object):
64 64 """docstring for Notifier"""
65 65 def __init__(self, theType, **kwargs):
66 66 super(Notifier, self).__init__()
67 67 self.theType = theType
68 68 self.kwargs = kwargs
69 69
70 70 def post(self, center=notification.sharedCenter):
71 71 """fire"""
72 72
73 73 center.post_notification(self.theType, self,
74 74 **self.kwargs)
75 75
76 76
77 77 #
78 78 # Test Cases
79 79 #
80 80
81 81 class NotificationTests(unittest.TestCase):
82 82 """docstring for NotificationTests"""
83 83
84 84 def tearDown(self):
85 85 notification.sharedCenter.remove_all_observers()
86 86
87 87 def test_notification_delivered(self):
88 88 """Test that notifications are delivered"""
89 89 expectedType = 'EXPECTED_TYPE'
90 90 sender = Notifier(expectedType)
91 91 observer = Observer(expectedType, sender)
92 92
93 93 sender.post()
94 94
95 95 observer.verify()
96 96
97 97
98 98 def test_type_specificity(self):
99 99 """Test that observers are registered by type"""
100 100
101 101 expectedType = 1
102 102 unexpectedType = "UNEXPECTED_TYPE"
103 103 sender = Notifier(expectedType)
104 104 unexpectedSender = Notifier(unexpectedType)
105 105 observer = Observer(expectedType, sender)
106 106
107 107 sender.post()
108 108 unexpectedSender.post()
109 109
110 110 observer.verify()
111 111
112 112
113 113 def test_sender_specificity(self):
114 114 """Test that observers are registered by sender"""
115 115
116 116 expectedType = "EXPECTED_TYPE"
117 117 sender1 = Notifier(expectedType)
118 118 sender2 = Notifier(expectedType)
119 119 observer = Observer(expectedType, sender1)
120 120
121 121 sender1.post()
122 122 sender2.post()
123 123
124 124 observer.verify()
125 125
126 126
127 127 def test_remove_all_observers(self):
128 128 """White-box test for remove_all_observers"""
129 129
130 130 for i in xrange(10):
131 131 Observer('TYPE', None, center=notification.sharedCenter)
132 132
133 133 self.assert_(len(notification.sharedCenter.observers[('TYPE',None)]) >= 10,
134 134 "observers registered")
135 135
136 136 notification.sharedCenter.remove_all_observers()
137 137
138 138 self.assert_(len(notification.sharedCenter.observers) == 0, "observers removed")
139 139
140 140
141 141 def test_any_sender(self):
142 142 """test_any_sender"""
143 143
144 144 expectedType = "EXPECTED_TYPE"
145 145 sender1 = Notifier(expectedType)
146 146 sender2 = Notifier(expectedType)
147 147 observer = Observer(expectedType, None)
148 148
149 149
150 150 sender1.post()
151 151 observer.verify()
152 152
153 153 observer.reset()
154 154 sender2.post()
155 155 observer.verify()
156 156
157 157
158 158 @timed(.01)
159 159 def test_post_performance(self):
160 160 """Test that post_notification, even with many registered irrelevant
161 161 observers is fast"""
162 162
163 163 for i in xrange(10):
164 164 Observer("UNRELATED_TYPE", None)
165 165
166 166 o = Observer('EXPECTED_TYPE', None)
167 167
168 168 notification.sharedCenter.post_notification('EXPECTED_TYPE', self)
169 169
170 170 o.verify()
171 171
172
173 def test_complexity_with_no_observers(self):
174 """Test that the notification center's algorithmic complexity is O(1)
175 with no registered observers (for the given notification type)
176 """
177
178 self.fail("I'm not sure how to test this.")
179
General Comments 0
You need to be logged in to leave comments. Login now