File: test_pubsub.py

package info (click to toggle)
python-netaddr 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,912 kB
  • sloc: xml: 8,264; python: 6,697; makefile: 198; sh: 5
file content (36 lines) | stat: -rw-r--r-- 707 bytes parent folder | download
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
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')