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()
Esempio n. 2
0
        assert subscription.message_count == 0
        assert subscription.timestamp >= start_time
        assert subscription.is_active
        assert subscription.async == async
        assert subscription.thread_id == (threadlocal and test_thread_id or 0)

        assert channel.has_subscriptions, channel_name
        assert channel.has_async_subscriptions == async, channel_name
        assert channel.has_synchronous_subscriptions != async, channel_name
        assert bool(channel.asynchronous_subscriptions) == async
        assert bool(channel.synchronous_subscriptions) != async


    for channel_name, (channel, subscription) in channels.iteritems():
        # pull the channel and subscription from the bus again
        assert channel == bus.get_channel(channel_name)
        if async:
            assert subscription == channel.asynchronous_subscriptions[0]
        else:
            assert subscription == channel.synchronous_subscriptions[0]

        ## send a batch of messages followed by a sentinel terminator message
        for i in xrange(message_count):
            channel.send((channel_name, i))
            if not async:
                assert channel.message_count == subscription.message_count == i+1
        channel.send((channel_name, message_count)) # terminator
                                        # message, which sets
                                        # subscriber.done event

        if threadlocal: