Esempio n. 1
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()
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. 3
0
    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):
            self.messages.append((msg, current_thread_id()))