Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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.'