File: test_pubsub.py

package info (click to toggle)
python-netaddr 0.7.18-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 5,732 kB
  • sloc: python: 9,050; xml: 6,348; makefile: 84
file content (29 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (5)
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
import pytest
from netaddr.core import Publisher, Subscriber
import pprint

class Subject(Publisher):
    pass

class Observer(Subscriber):
    def __init__(self, id):
        self.id = id

    def update(self, data):
        return repr(self), pprint.pformat(data)

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, self.id)

def test_pubsub():
    s = Subject()
    s.attach(Observer('foo'))
    s.attach(Observer('bar'))

    data = [{'foo': 42}, {'list': [1,'2', list(range(10))]}, {'strings': ['foo', 'bar', 'baz', 'quux']}]
    s.notify(data)

    s.notify(['foo', 'bar', 'baz'])

    with pytest.raises(TypeError):
        s.attach('foo')