# Path: mcloud/events.py
# class EventBus(object):
#     redis = None
#     protocol = None
# 
#     def __init__(self, redis_connection):
#         super(EventBus, self).__init__()
#         self.redis = redis_connection
# 
#     def fire_event(self, event_name, data=None,  *args, **kwargs):
#         if not data:
#             if kwargs:
#                 data = kwargs
#             elif args:
#                 data = args
# 
#         if not isinstance(data, basestring):
#             data = 'j:' + json.dumps(data)
#         else:
#             data = 'b:' + str(data)
# 
#         return self.redis.publish(event_name, data)
# 
# 
#     def connect(self, host="127.0.0.1", port=6379):
#         log.msg('Event bus connected')
#         d = defer.Deferred()
#         reactor.connectTCP(host, port, EventBusFactory(d, self))
#         return d
# 
#     def on(self, pattern, callback):
#         if not self.protocol:
#             raise Exception('Event bus is not connected yet!')
#         self.protocol.on(pattern, callback)
#         log.msg('Registered %s for channel: %s' % (callback, pattern))
# 
#     def cancel(self, pattern, callback):
#         if not self.protocol:
#             raise Exception('Event bus is not connected yet!')
#         self.protocol.cancel(pattern, callback)
#         log.msg('unRegistered %s for channel: %s' % (callback, pattern))
# 
#     def once(self, pattern, callback):
#         if not self.protocol:
#             raise Exception('Event bus is not connected yet!')
# 
#         def _once_and_remove(*args, **kwargs):
#             self.protocol.cancel(pattern, _once_and_remove)
#             callback(*args, **kwargs)
# 
#         self.protocol.on(pattern, _once_and_remove)
#         log.msg('Registered %s for single invocation on channel: %s' % (callback, pattern))
# 
#     def wait_for_event(self, pattern, timeout=False):
#         d = defer.Deferred()
# 
#         def _on_message(channel, message):
#             if not d.called:
#                 d.callback(message)
# 
#         self.on(pattern, _on_message)
# 
#         if not timeout == 0:
#             return txtimeout(d, timeout, lambda: d.callback(None))
#         else:
#             return d


import inject
import pytest
import txredisapi as redis
from mcloud.events import EventBus
from twisted.internet import reactor
    inject.clear()
    rc = yield redis.Connection(dbid=2)
    yield rc.flushdb()

    eb = EventBus(rc)
    yield eb.connect()

    test_events_pattern.test = None

    def boo(pattern, message):
        assert message == 'hoho'
        assert pattern == 'foo.baz'
        test_events_pattern.test = message

    eb.on('foo.*', boo)

    yield eb.fire_event('foo.baz', 'hoho')

    def check_results():
        assert test_events_pattern.test == 'hoho'

    reactor.callLater(50, check_results)


@pytest.inlineCallbacks
def test_events_pattern_wrong():
    inject.clear()
    rc = yield redis.Connection(dbid=2)
    yield rc.flushdb()


# Path: mcloud/events.py
# class EventBus(object):
#     redis = None
#     protocol = None
# 
#     def __init__(self, redis_connection):
#         super(EventBus, self).__init__()
#         self.redis = redis_connection
# 
#     def fire_event(self, event_name, data=None,  *args, **kwargs):
#         if not data:
#             if kwargs:
#                 data = kwargs
#             elif args:
#                 data = args
# 
#         if not isinstance(data, basestring):
#             data = 'j:' + json.dumps(data)
#         else:
#             data = 'b:' + str(data)
# 
#         return self.redis.publish(event_name, data)
# 
# 
#     def connect(self, host="127.0.0.1", port=6379):
#         log.msg('Event bus connected')
#         d = defer.Deferred()
#         reactor.connectTCP(host, port, EventBusFactory(d, self))
#         return d
# 
#     def on(self, pattern, callback):
#         if not self.protocol:
#             raise Exception('Event bus is not connected yet!')
#         self.protocol.on(pattern, callback)
#         log.msg('Registered %s for channel: %s' % (callback, pattern))
# 
#     def cancel(self, pattern, callback):
#         if not self.protocol:
#             raise Exception('Event bus is not connected yet!')
#         self.protocol.cancel(pattern, callback)
#         log.msg('unRegistered %s for channel: %s' % (callback, pattern))
# 
#     def once(self, pattern, callback):
#         if not self.protocol:
#             raise Exception('Event bus is not connected yet!')
# 
#         def _once_and_remove(*args, **kwargs):
#             self.protocol.cancel(pattern, _once_and_remove)
#             callback(*args, **kwargs)
# 
#         self.protocol.on(pattern, _once_and_remove)
#         log.msg('Registered %s for single invocation on channel: %s' % (callback, pattern))
# 
#     def wait_for_event(self, pattern, timeout=False):
#         d = defer.Deferred()
# 
#         def _on_message(channel, message):
#             if not d.called:
#                 d.callback(message)
# 
#         self.on(pattern, _on_message)
# 
#         if not timeout == 0:
#             return txtimeout(d, timeout, lambda: d.callback(None))
#         else:
#             return d


# Path: tests/test_events.py
import inject
import pytest
import txredisapi as redis
from mcloud.events import EventBus
from twisted.internet import reactor

    inject.clear()
    rc = yield redis.Connection(dbid=2)
    yield rc.flushdb()

    eb = EventBus(rc)
    yield eb.connect()

    test_events_pattern.test = None

    def boo(pattern, message):
        assert message == 'hoho'
        assert pattern == 'foo.baz'
        test_events_pattern.test = message

    eb.on('foo.*', boo)

    yield eb.fire_event('foo.baz', 'hoho')

    def check_results():
        assert test_events_pattern.test == 'hoho'

    reactor.callLater(50, check_results)


@pytest.inlineCallbacks
def test_events_pattern_wrong():
    inject.clear()
    rc = yield redis.Connection(dbid=2)
    yield rc.flushdb()


    eb = EventBus(rc)