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)
        def __init__(self, channel_name):
            self.channel_name = channel_name
            self.messages = []
            self.done = Event()

        def __call__(self, msg):
            if not self.done.isSet():
                # only expecting a single message
                self.messages.append((msg, current_thread_id()))
                self.done.set()
            else:
                raise Exception('Was only expecting a single message')

    ## setup non-wildcard subscriptions
    for channel_name in channel_names:
        channel = bus.create_new_channel(channel_name)
        subscriber = Subscriber(channel_name)
        subscription=bus.subscribe(
            channel_name, subscriber,
            async=async, thread_id=(threadlocal and test_thread_id or 0))
        channels[channel_name] = (channel, subscription)

        assert subscription.channel == channel
        assert subscription.subscriber == subscriber
        assert not subscription.include_subchannels
        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)