def testSimple(self): """ Run through some valid cases. """ data = [] def msg_added(name, value): data.append((name, value)) def msg_removed(name): data.append((name, )) p = pub.Publisher() p.subscribe(msg_added, 'test.added') p.subscribe(msg_removed, 'test.removed') pd = box.PubDict(RLock(), p.sendMessage, 'test') pd['a'] = 'abc' pd['b'] = 'def' pd['c'] = 'ghi' del pd['a'] pd['a'] = 'jkl' del pd['c'] expected = {'a': 'jkl', 'b': 'def'} expected_data = [('a', 'abc'), ('b', 'def'), ('c', 'ghi'), ('a', ), ('a', 'jkl'), ('c', )] eq_(pd, expected) eq_(data, expected_data)
def testLocked(self): """ Attempt a compound operation. """ pd = box.PubDict(RLock(), pub.Publisher().sendMessage, 'test') pd['a'] = 'abc' with pd.lock: del pd['a'] pd['a'] = 'def' expected = {'a': 'def'} eq_(pd, expected)
def testInvalid(self): """ Try some bad scenarios. """ pd = box.PubDict(RLock(), pub.Publisher().sendMessage, 'test') # Setting None. try: pd['a'] = None except ValueError: pass else: assert False, 'Expected ValueError.' # Overwriting. pd['a'] = 'abc' try: pd['a'] = 'def' except KeyError: pass else: assert False, 'Expected KeyError.'