Esempio n. 1
0
def test_various_message_types():
    bus = MessageBus()
    chan = bus.root_channel
    subscriber = CollectingSubscriber()
    chan.subscribe(subscriber, async=True)
    messages = ('a', u'asdf', {'a':1234}, 2.2, set((1, 2)), [1, 2], (1, 2))
    for msg in messages:
        chan.send(msg)

    bus.stop()
    assert tuple(subscriber.messages) == messages
 def __init__(self, channel_name='Test'):
     self.bus = MessageBus(
         channel_class=LogChannel,
         use_dedicated_thread_mode=True)
     self.log_channel = self.bus.create_new_channel(channel_name)
     self.messages = []
     self.log_channel.subscribe(lambda msg: self.messages.append(msg))
Esempio n. 3
0
def test_full_message_queue():
    bus = MessageBus(max_queue_size=1)
    bus.turn_on_dedicated_thread_mode()
    chan = bus.root_channel
    subscriber = CollectingSubscriber()

    chan.subscribe(subscriber, async=True)
    for i in range(6):
        chan.send(i)

    slow_subscriber = SlowSubscriber()
    chan.subscribe(slow_subscriber, async=True)
    for i in range(6):
        # will block till the slow subscriber has finished handling
        # the previous one
        chan.send(i)
    bus.stop()
class LoggingContext(object):
    def __init__(self, channel_name='Test'):
        self.bus = MessageBus(
            channel_class=LogChannel,
            use_dedicated_thread_mode=True)
        self.log_channel = self.bus.create_new_channel(channel_name)
        self.messages = []
        self.log_channel.subscribe(lambda msg: self.messages.append(msg))

    def __enter__(self):
        self.bus.start()
        return self

    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
        self.bus.stop()
        formatter = Formatter()
        for msg in self.messages:
            print formatter.format(msg)
Esempio n. 5
0
def test_channel_names():
    bus1 = MessageBus()
    bus2 = MessageBus(channel_name_separator='/')

    for name in ['..foo', '*', '/', '.', '.sub', '98', '98_', 'top.98']:
        get_invalid_channel(bus1, name)
        assert not bus1.is_valid_channel_name(name)

    for name in ['foo', 'foo2', 'foo_a',
                 '_foo','_', 'foo123',
                 'foo.bar', 'foo.bar.asdf',
                 'a.b.c.d'
                 ]:
        assert bus1.is_valid_channel_name(name)
        assert bus2.is_valid_channel_name(
            name.replace('.','/'))

    bus1.stop()
    bus2.stop()
Esempio n. 6
0
def test_channel_management(use_dedicated_thread_mode=True):
    bus = MessageBus()

    initial_channel_count = len(bus.channels) # root channel, internal
                                        # log channel, etc.
    test_channels = {}

    bus.start() # The bus has already been started by __init__, but
                # this extra call should do no damage!

    if use_dedicated_thread_mode:
        bus.turn_on_dedicated_thread_mode()

    attempt_to_create_existing_channel(bus, 'root')
    assert bus.root_channel == bus.get_channel('root')

    def addchan(name, parent):
        get_nonexistent_channel(bus, name)
        test_channels[name] = chan = bus.create_new_channel(name)
        assert chan == bus.get_channel(name)
        assert chan.parent_channel == parent
        assert chan in parent.child_channels
        assert chan.name == name
        return chan

    for i in xrange(5):
        topname = 'top%i'%i
        topchan = addchan(topname, bus.root_channel)
        for j in xrange(5):
            subname = '%s.subchan%i'%(topname, j)
            subchan = addchan(subname, topchan)
            assert len(topchan.child_channels) == j+1
            assert '.'.join(subchan.name.split('.')[:-1])==topchan.name
            for k in xrange(5):
                subsubchan = addchan('%s.subsubchan%i'%(subname, k), subchan)
                assert len(subchan.child_channels) == k+1
                assert '.'.join(subsubchan.name.split('.')[:-1])==subchan.name

    assert len(bus.channels) == (len(test_channels)+initial_channel_count)
    assert len(bus.channels) == len(bus.get_open_channel_names())

    chanX = bus.create_new_channel('X')
    attempt_to_create_existing_channel(bus, 'X')
    assert chanX.parent_channel == bus.root_channel
    assert bus.get_channel('X') == chanX
    assert bus.create_new_channel('x') != chanX
    chanY = bus.create_new_channel('X.Y')
    assert chanY.parent_channel == chanX
    assert chanX.name in bus.channels
    assert chanY.name in bus.channels

    bus.stop()
Esempio n. 7
0
    attempt_to_create_existing_channel(bus, 'X')
    assert chanX.parent_channel == bus.root_channel
    assert bus.get_channel('X') == chanX
    assert bus.create_new_channel('x') != chanX
    chanY = bus.create_new_channel('X.Y')
    assert chanY.parent_channel == chanX
    assert chanX.name in bus.channels
    assert chanY.name in bus.channels

    bus.stop()

def test_channel_management_nonthreaded():
    test_channel_management(use_dedicated_thread_mode=False)

def _test_subscriptions(async=False, threadlocal=False):
    bus = MessageBus(use_dedicated_thread_mode=async)
    bus.start()

    start_time = time_of_day()
    test_thread_id = current_thread_id()

    channels = {}
    channel_names = ('a', 'a.b', 'a.b.c', 'a.b.c.d', 'w.x.y.z')
    message_count = 200
    class Subscriber(object):
        def __init__(self, channel_name):
            self.channel_name = channel_name
            self.messages = []
            self.done = Event()

        def __call__(self, msg):