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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
"""
Except for one test, this file tests with auto-creation of topics
disabled, as it is more rigorous for testing purposes.
:copyright: Copyright since 2006 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.
"""
import gc
import pytest
import pubsub.core.topicargspec
from pubsub import pub
from pubsub.utils.notification import IgnoreNotificationsMixin
from pubsub.core import getListenerID, ListenerMismatchError
topicMgr = pub.getDefaultTopicMgr()
def testDOAListenerPubsub():
# Verify that a 'temporary' listener (one that will be garbage collected
# as soon as subscribe() returns because there are no strong references to
# it) gets immediately unregistered
def listener(): pass
class Wrapper:
def __init__(self, func):
self.func = func
def __call__(self):
pass
pub.subscribe( Wrapper(listener), 'testDOAListenerPubsub')
gc.collect() # for pypy: the gc doesn't work the same as cpython's
assert not topicMgr.getTopic('testDOAListenerPubsub').hasListeners()
assert pub.isValid(listener, 'testDOAListenerPubsub')
def testDeadListener():
# create a listener for listeners that have died
class DeathListener(IgnoreNotificationsMixin):
listenerStr = ''
def notifyDeadListener(self, pubListener, topicObj):
assert topicObj.getName() == 'sadTopic'
#import pdb; pdb.set_trace()
#print 'hi again'
DeathListener.listenerStr = pubListener.name()
dl = DeathListener()
pub.addNotificationHandler( dl )
pub.setNotificationFlags(deadListener=True)
# define a topic, subscribe to it, and kill its listener:
class TempListener:
def __call__(self, **kwargs):
pass
def __del__(self):
# print 'being deleted'
pass
#def tempListener(): pass
tempListener = TempListener()
expectLisrStr, _ = getListenerID(tempListener)
pub.subscribe(tempListener, 'sadTopic')
del tempListener
# verify:
gc.collect() # for pypy: the gc doesn't work the same as cpython's
assert DeathListener.listenerStr.startswith(expectLisrStr), \
'"%s" !~ "%s"' % (DeathListener.listenerStr, expectLisrStr)
pub.addNotificationHandler(None)
pub.clearNotificationHandlers()
def testSubscribe():
topicName = 'testSubscribe'
def proto(a, b, c=None): pass
topicMgr.getOrCreateTopic(topicName, proto)
def listener(a, b, c=None): pass
# verify that pub.isValid() works too
pub.validate(listener, topicName)
assert pub.isValid(listener, topicName)
assert topicMgr.getTopic(topicName).getNumListeners() == 0
assert topicMgr.getTopicsSubscribed(listener) == []
assert not pub.isSubscribed(listener, topicName)
assert pub.subscribe(listener, topicName)
assert pub.isSubscribed(listener, topicName)
def topicNames(listener):
return [t.getName() for t in topicMgr.getTopicsSubscribed(listener)]
assert topicNames(listener) == [topicName]
# should do nothing if already subscribed:
assert not pub.subscribe(listener, topicName)[1]
assert topicMgr.getTopic(topicName).getNumListeners() == 1
# test topicMgr.getTopicsSubscribed()
pub.subscribe(listener, 'lt2', )
assert set(topicNames(listener)) == set([topicName,'lt2'])
pub.subscribe(listener, 'lt1.lst1')
assert set(topicNames(listener)) == set([topicName,'lt2','lt1.lst1'])
# test ALL_TOPICS
def listenToAll(): pass
pub.subscribe(listenToAll, pub.ALL_TOPICS)
assert topicNames(listenToAll) == [pub.ALL_TOPICS]
# test type hints in listeners:
def listenerWithHints(a: int, b: bool, c: str = 2): pass
topicForHintedListeners = 'topicForHints'
topicMgr.getOrCreateTopic(topicForHintedListeners, listenerWithHints)
assert not pub.isSubscribed(listenerWithHints, topicForHintedListeners)
pub.subscribe(listenerWithHints, topicForHintedListeners)
assert pub.subscribe(listenerWithHints, topicForHintedListeners)
assert pub.isSubscribed(listenerWithHints, topicForHintedListeners)
def testMissingReqdArgs():
def proto(a, b, c=None): pass
topicMgr.getOrCreateTopic('missingReqdArgs', proto)
pytest.raises(pubsub.core.topicargspec.SenderMissingReqdMsgDataError, pub.sendMessage, 'missingReqdArgs', a=1)
def testSendTopicWithMessage():
class MyListener:
def __init__(self):
self.count = 0
self.heardTopic = False
self.listen2Topics = []
def listen0(self):
pass
def listen1(self, **kwarg):
self.count += 1
self.heardTopic = True
def listen2(self, msgTopic=pub.AUTO_TOPIC, **kwarg):
self.listen2Topics.append(msgTopic.getName())
my = MyListener()
pub.subscribe(my.listen0, 'testSendTopic')
pub.subscribe(my.listen1, 'testSendTopic')
pub.subscribe(my.listen2, 'testSendTopic')
pub.sendMessage('testSendTopic')
assert my.count == 1
assert my.heardTopic == True
pub.subscribe(my.listen0, 'testSendTopic.subtopic')
pub.subscribe(my.listen1, 'testSendTopic.subtopic')
pub.subscribe(my.listen2, 'testSendTopic.subtopic')
pub.sendMessage('testSendTopic.subtopic')
assert my.count == 3
assert [] == [topic for topic in my.listen2Topics
if topic not in ('testSendTopic', 'testSendTopic.subtopic')]
# type hints on listeners:
result = []
def listenerWithHints(a: int, b: bool, c: str = 2): result.append((a, b, c))
topicForHintedListeners = 'topicForHints'
pub.subscribe(listenerWithHints, topicForHintedListeners)
assert pub.subscribe(listenerWithHints, topicForHintedListeners)
pub.sendMessage(topicForHintedListeners, b=456, a=123, c='hello')
assert result == [(123, 456, 'hello')]
def testOptionalArgs():
# first function registered determines topic MDS (message data spec)
# here we first register a more "permissive" listener, ie one that has default values for args
# that the second listener does not; therefore pubsub should refuse subscribing second listener
# to same topic
def myFunction1(arg1, arg2=2, *, kwarg1, kwarg2=4, **kwargs): pass
def myFunction2(arg1, arg2, *, kwarg1, kwarg2=4, **kwargs): pass
pub.subscribe(myFunction1, 'testKeywordOnlyArgs')
pytest.raises(ListenerMismatchError, pub.subscribe, myFunction2, 'testKeywordOnlyArgs')
def testKeywordOnlyArgsStar():
def capture(funcName, *args):
result[funcName] = args
def myFunction1(arg1, arg2, *, kwarg1, kwarg2=4, **kwargs):
capture('myFunction1', arg1, arg2, kwarg1, kwarg2, kwargs)
def myFunction2(arg1, arg2, *arg3, kwarg1, kwarg2=4, **kwargs):
capture('myFunction2', arg1, arg2, arg3, kwarg1, kwarg2, kwargs)
pub.subscribe(myFunction1, 'testKeywordOnlyArgsStar')
pub.subscribe(myFunction2, 'testKeywordOnlyArgsStar')
result = {}
pub.sendMessage('testKeywordOnlyArgsStar', arg1=1, arg2=2, kwarg1=3)
assert result == dict(myFunction1=(1, 2, 3, 4, {}), myFunction2=(1, 2, (), 3, 4, {}))
def testKeywordOnlyArgsStarAfterOpt():
def capture(funcName, *args):
result[funcName] = args
def myFunction1(arg1, arg2=2, *, kwarg1, kwarg2=4, **kwargs):
capture('myFunction1', arg1, arg2, kwarg1, kwarg2, kwargs)
def myFunction2(arg1, arg2=2, *arg3, kwarg1, kwarg2=4, **kwargs):
capture('myFunction2', arg1, arg2, arg3, kwarg1, kwarg2, kwargs)
pub.subscribe(myFunction1, 'testKeywordOnlyArgsStarAfterOpt')
pub.subscribe(myFunction2, 'testKeywordOnlyArgsStarAfterOpt')
result = dict()
pub.sendMessage('testKeywordOnlyArgsStarAfterOpt', arg1=1, kwarg1=3)
assert result == dict(myFunction1=(1, 2, 3, 4, {}), myFunction2=(1, 2, (), 3, 4, {}))
def testKeywordOnlyArgsNoDefaults():
def capture(funcName, *args):
result[funcName] = args
def myFunction1(*, kwarg1, kwarg2): capture('myFunction1', kwarg1, kwarg2)
def myFunction2(*args, kwarg1, kwarg2): capture('myFunction2', args, kwarg1, kwarg2)
def myFunction3(*, kwarg1, kwarg2, **kwargs): capture('myFunction3', kwarg1, kwarg2, kwargs)
def myFunction4(*args, kwarg1, kwarg2, **kwargs): capture('myFunction4', args, kwarg1, kwarg2, kwargs)
pub.subscribe(myFunction1, 'testKeywordOnlyArgsNoDefaults')
pub.subscribe(myFunction2, 'testKeywordOnlyArgsNoDefaults')
pub.subscribe(myFunction3, 'testKeywordOnlyArgsNoDefaults')
pub.subscribe(myFunction4, 'testKeywordOnlyArgsNoDefaults')
result = {}
pub.sendMessage('testKeywordOnlyArgsNoDefaults', kwarg1=1, kwarg2=2)
assert result == dict(myFunction1=(1, 2), myFunction2=((), 1, 2), myFunction3=(1, 2, {}), myFunction4=((), 1, 2, {}))
def testAcceptAllArgs():
def listen(arg1=None): pass
def listenAllArgs(arg1=None, **kwargs): pass
def listenAllArgs2(arg1=None, msgTopic=pub.AUTO_TOPIC, **kwargs): pass
pub.subscribe(listen, 'testAcceptAllArgs')
pub.subscribe(listenAllArgs, 'testAcceptAllArgs')
pub.subscribe(listenAllArgs2, 'testAcceptAllArgs')
pub.subscribe(listenAllArgs2, 'testAcceptAllArgs.subtopic')
pub.subscribe(listenAllArgs, 'testAcceptAllArgs.subtopic')
def testSubscribeCurriedListeners():
result = []
def proto_listener(a, b, c=1, d=2):
pass
def listener1(a, b, c=1, d=2, nonTopicArg=3):
result.append((a, b, c, d))
# no currying:
pub.subscribe(proto_listener, 'topic1')
pub.subscribe(listener1, 'topic1')
# ok:
pub.subscribe(proto_listener, 'topic2')
pub.subscribe(listener1, 'topic2', nonTopicArg=4)
# curried arg typo:
pub.subscribe(proto_listener, 'topic3')
pytest.raises(ListenerMismatchError, pub.subscribe, listener1, 'topic3', invalidArg=4)
# curried arg is in topic args:
pub.subscribe(proto_listener, 'topic4')
pytest.raises(ListenerMismatchError, pub.subscribe, listener1, 'topic4', a=4)
def testSendWhenCurriedArgs():
result = []
def listen(a, b, c, d=0, e=0):
result.append((a, b, c, d, e))
wrapped = pub.subscribe(listen, 'testCurriedArgs', b=2, d=4)
assert wrapped[0].curriedArgs
pub.sendMessage('testCurriedArgs', a=1, c=3)
assert result[0] == (1, 2, 3, 4, 0)
def testUnsubAll():
def lisnr1(): pass
def lisnr2(): pass
class MyListener:
def __call__(self): pass
def meth(self): pass
def __hash__(self): return 123
lisnr3 = MyListener()
lisnr4 = lisnr3.meth
def lisnrSub(listener=None, topic=None, newSub=None): pass
pub.subscribe(lisnrSub, 'pubsub.subscribe')
assert topicMgr.getTopic('pubsub.subscribe').getNumListeners() == 1
def subAll():
pub.subscribe(lisnr1, 'testUnsubAll')
pub.subscribe(lisnr2, 'testUnsubAll')
pub.subscribe(lisnr3, 'testUnsubAll')
pub.subscribe(lisnr4, 'testUnsubAll')
assert topicMgr.getTopic('testUnsubAll').getNumListeners() == 4
def filter(lisnr):
passes = str(lisnr).endswith('meth')
return passes
# test unsub many non-pubsub topic listeners
subAll()
pub.unsubAll('testUnsubAll')
assert topicMgr.getTopic('testUnsubAll').getNumListeners() == 0
assert topicMgr.getTopic('pubsub.subscribe').getNumListeners() == 1
# now same but with filter:
subAll()
unsubed = pub.unsubAll('testUnsubAll', listenerFilter=filter)
assert topicMgr.getTopic('testUnsubAll').getNumListeners() == 3
assert topicMgr.getTopic('pubsub.subscribe').getNumListeners() == 1
# test unsub all listeners of all topics
subAll()
assert topicMgr.getTopic('testUnsubAll').getNumListeners() == 4
unsubed = pub.unsubAll(listenerFilter=filter)
assert unsubed == [lisnr4]
assert topicMgr.getTopic('testUnsubAll').getNumListeners() == 3
assert topicMgr.getTopic('pubsub.subscribe').getNumListeners() == 1
unsubed = set( pub.unsubAll() )
expect = set([lisnr1, lisnrSub, lisnr3, lisnr2])
# at least all the 'expected' ones were unsub'd; will be others if this
# test is run after other unit tests in same py.test run
assert unsubed >= expect
def testSendForUndefinedTopic():
pub.sendMessage('testSendForUndefinedTopic')
assert topicMgr.getTopic('testSendForUndefinedTopic')
assert topicMgr.getTopic('testSendForUndefinedTopic').getArgs() == (None, None)
# must also check for subtopics if parents have listeners since
# filtering of args is affected
def listener(): pass
pub.subscribe(listener, 'testSendForUndefinedTopic')
pub.sendMessage('testSendForUndefinedTopic.subtopic', msg='something')
def testTopicUnspecifiedError():
pytest.raises(pub.TopicDefnError, pub.setTopicUnspecifiedFatal)
pub.setTopicUnspecifiedFatal(checkExisting=False)
def fn(): pass
LSI = pub.TopicDefnError
pytest.raises(LSI, pub.sendMessage, 'testTopicUnspecifiedError')
pytest.raises(LSI, pub.subscribe, fn, 'testTopicUnspecifiedError')
pub.setTopicUnspecifiedFatal(False)
pub.sendMessage('testTopicUnspecifiedError')
pub.subscribe(fn, 'testTopicUnspecifiedError')
def testArgSpecDerivation():
def ok_0(): pass
def ok_1(arg1): pass
def err_11(arg1=None): pass # required can't become optional!
def err_12(arg2): pass # parent's arg1 missing
def ok_2(arg1=None): pass
def ok_21(arg1): pass # optional can become required
def err_22(arg2): pass # parent's arg1 missing
# with getOrCreateTopic(topic, proto), the 'required args' set
# is garanteed to be a subset of 'all args'
topicMgr.getOrCreateTopic('tasd', ok_0)
topicMgr.getOrCreateTopic('tasd.t_1', ok_1)
pytest.raises( pub.MessageDataSpecError, topicMgr.getOrCreateTopic, 'tasd.t_1.t_11', err_11)
pytest.raises( pub.MessageDataSpecError, topicMgr.getOrCreateTopic, 'tasd.t_1.t_12', err_12)
topicMgr.getOrCreateTopic('tasd.t_2', ok_2)
topicMgr.getOrCreateTopic('tasd.t_2.t_21', ok_21)
pytest.raises( pub.MessageDataSpecError, topicMgr.getOrCreateTopic, 'tasd.t_2.t_22', err_22)
print()
def testListenerChangesListenerList():
"""pubsub supports un/subscribing of listeners while sendMessage
in progress. This requires that the TopicManager instance
properly loop over listeners, via a copy of list instead of
iterator on list (since lists can't be modified while iteration
in progress. This test verifies that listener receiving message
can subscribe another listener to same topic, and can unsubscribe
self while handling message. """
class Listeners:
callCountForNewListener = 0
callCountForChanger = 0
def newListener(self):
self.callCountForNewListener += 1
pub.unsubscribe(self.newListener, 'test.change-listeners')
def changer(self):
# first time, subscribe new listener; if don't have this, will fail in
# py3 because order of listeners opposite, so unsub will happen before
# the sub (which succeeds even if no listeners) and newListener will
# remain subscribed.
if self.callCountForChanger == 0:
pub.subscribe(self.newListener, 'test.change-listeners')
self.callCountForChanger += 1
testListeners = Listeners()
pub.subscribe(testListeners.changer, 'test.change-listeners')
topic = pub.getDefaultTopicMgr().getTopic('test.change-listeners')
pub.sendMessage('test.change-listeners')
assert testListeners.callCountForChanger == 1
assert testListeners.callCountForNewListener == 0
assert topic.getNumListeners() == 2
pub.sendMessage('test.change-listeners')
assert testListeners.callCountForChanger == 2
assert testListeners.callCountForNewListener == 1
assert topic.getNumListeners() == 1
pub.sendMessage('test.change-listeners')
assert testListeners.callCountForChanger == 3
assert testListeners.callCountForNewListener == 1
|