# Path: aodh/event.py
# LOG = log.getLogger(__name__)
# OPTS = [
#     cfg.StrOpt('event_alarm_topic',
#                default='alarm.all',
#                deprecated_group='DEFAULT',
#                help='The topic that aodh uses for event alarm evaluation.'),
#     cfg.IntOpt('batch_size',
#                default=1,
#                help='Number of notification messages to wait before '
#                'dispatching them.'),
#     cfg.IntOpt('batch_timeout',
#                help='Number of seconds to wait before dispatching samples '
#                'when batch_size is not reached (None means indefinitely).'),
# ]
# class EventAlarmEndpoint(object):
# class EventAlarmEvaluationService(cotyledon.Service):
#     def __init__(self, evaluator):
#     def sample(self, notifications):
#     def __init__(self, worker_id, conf):
#     def terminate(self):
# 
# Path: aodh/service.py
# OPTS = [
#     cfg.IntOpt('http_timeout',
#                default=600,
#                help='Timeout seconds for HTTP requests. Set it to None to '
#                     'disable timeout.'),
#     cfg.IntOpt('evaluation_interval',
#                default=60,
#                help='Period of evaluation cycle, should'
#                ' be >= than configured pipeline interval for'
#                ' collection of underlying meters.'),
# ]
# EVALUATOR_OPTS = [
#     cfg.IntOpt('workers',
#                default=1,
#                min=1,
#                help='Number of workers for evaluator service. '
#                'default value is 1.')
# ]
# NOTIFIER_OPTS = [
#     cfg.IntOpt('workers',
#                default=1,
#                min=1,
#                help='Number of workers for notifier service. '
#                'default value is 1.')
# ]
# LISTENER_OPTS = [
#     cfg.IntOpt('workers',
#                default=1,
#                min=1,
#                help='Number of workers for listener service. '
#                     'default value is 1.')
# ]
# def prepare_service(argv=None, config_files=None):
# 
# Path: aodh/tests/base.py
# class BaseTestCase(base.BaseTestCase):
# class SkipNotImplementedMeta(type):
#     def setup_messaging(self, conf, exchange=None):
#     def assertTimestampEqual(self, first, second, msg=None):
#     def assertIsEmpty(self, obj):
#     def assertIsNotEmpty(self, obj):
#     def assertDictContains(self, parent, child):
#     def path_get(project_file=None):
#     def assert_single_item(self, items, **filters):
#     def assert_multiple_items(self, items, count, **filters):
#         def _matches(item, **props):
# def _skip_decorator(func):
#     def skip_if_not_implemented(*args, **kwargs):
#     def __new__(cls, name, bases, local):


import time
import oslo_messaging
from unittest import mock
from oslo_config import fixture as fixture_config
from aodh import event
from aodh import service
from aodh.tests import base as tests_base
class TestEventAlarmEvaluationService(tests_base.BaseTestCase):

    def setUp(self):
        super(TestEventAlarmEvaluationService, self).setUp()
        conf = service.prepare_service(argv=[], config_files=[])
        self.CONF = self.useFixture(fixture_config.Config(conf)).conf
        self.CONF.set_override("batch_size", 2, 'listener')
        self.setup_messaging(self.CONF)

    @mock.patch('aodh.storage.get_connection_from_config',
                mock.MagicMock())
    @mock.patch('aodh.event.EventAlarmEndpoint.sample')
    def test_batch_event_listener(self, mocked):
        msg_notifier = oslo_messaging.Notifier(
            self.transport, topics=['alarm.all'], driver='messaging',
            publisher_id='test-publisher')

        received_events = []
        mocked.side_effect = lambda msg: received_events.append(msg)
        event1 = {'event_type': 'compute.instance.update',
                  'traits': ['foo', 'bar'],
                  'message_id': '20d03d17-4aba-4900-a179-dba1281a3451',
                  'generated': '2016-04-23T06:50:21.622739'}
        event2 = {'event_type': 'compute.instance.update',
                  'traits': ['foo', 'bar'],
                  'message_id': '20d03d17-4aba-4900-a179-dba1281a3452',
                  'generated': '2016-04-23T06:50:23.622739'}
        msg_notifier.sample({}, 'event', event1)
        msg_notifier.sample({}, 'event', event2)


# Path: aodh/event.py
# LOG = log.getLogger(__name__)
# OPTS = [
#     cfg.StrOpt('event_alarm_topic',
#                default='alarm.all',
#                deprecated_group='DEFAULT',
#                help='The topic that aodh uses for event alarm evaluation.'),
#     cfg.IntOpt('batch_size',
#                default=1,
#                help='Number of notification messages to wait before '
#                'dispatching them.'),
#     cfg.IntOpt('batch_timeout',
#                help='Number of seconds to wait before dispatching samples '
#                'when batch_size is not reached (None means indefinitely).'),
# ]
# class EventAlarmEndpoint(object):
# class EventAlarmEvaluationService(cotyledon.Service):
#     def __init__(self, evaluator):
#     def sample(self, notifications):
#     def __init__(self, worker_id, conf):
#     def terminate(self):
# 
# Path: aodh/service.py
# OPTS = [
#     cfg.IntOpt('http_timeout',
#                default=600,
#                help='Timeout seconds for HTTP requests. Set it to None to '
#                     'disable timeout.'),
#     cfg.IntOpt('evaluation_interval',
#                default=60,
#                help='Period of evaluation cycle, should'
#                ' be >= than configured pipeline interval for'
#                ' collection of underlying meters.'),
# ]
# EVALUATOR_OPTS = [
#     cfg.IntOpt('workers',
#                default=1,
#                min=1,
#                help='Number of workers for evaluator service. '
#                'default value is 1.')
# ]
# NOTIFIER_OPTS = [
#     cfg.IntOpt('workers',
#                default=1,
#                min=1,
#                help='Number of workers for notifier service. '
#                'default value is 1.')
# ]
# LISTENER_OPTS = [
#     cfg.IntOpt('workers',
#                default=1,
#                min=1,
#                help='Number of workers for listener service. '
#                     'default value is 1.')
# ]
# def prepare_service(argv=None, config_files=None):
# 
# Path: aodh/tests/base.py
# class BaseTestCase(base.BaseTestCase):
# class SkipNotImplementedMeta(type):
#     def setup_messaging(self, conf, exchange=None):
#     def assertTimestampEqual(self, first, second, msg=None):
#     def assertIsEmpty(self, obj):
#     def assertIsNotEmpty(self, obj):
#     def assertDictContains(self, parent, child):
#     def path_get(project_file=None):
#     def assert_single_item(self, items, **filters):
#     def assert_multiple_items(self, items, count, **filters):
#         def _matches(item, **props):
# def _skip_decorator(func):
#     def skip_if_not_implemented(*args, **kwargs):
#     def __new__(cls, name, bases, local):


# Path: aodh/tests/unit/test_event.py
import time
import oslo_messaging
from unittest import mock
from oslo_config import fixture as fixture_config
from aodh import event
from aodh import service
from aodh.tests import base as tests_base

class TestEventAlarmEvaluationService(tests_base.BaseTestCase):

    def setUp(self):
        super(TestEventAlarmEvaluationService, self).setUp()
        conf = service.prepare_service(argv=[], config_files=[])
        self.CONF = self.useFixture(fixture_config.Config(conf)).conf
        self.CONF.set_override("batch_size", 2, 'listener')
        self.setup_messaging(self.CONF)

    @mock.patch('aodh.storage.get_connection_from_config',
                mock.MagicMock())
    @mock.patch('aodh.event.EventAlarmEndpoint.sample')
    def test_batch_event_listener(self, mocked):
        msg_notifier = oslo_messaging.Notifier(
            self.transport, topics=['alarm.all'], driver='messaging',
            publisher_id='test-publisher')

        received_events = []
        mocked.side_effect = lambda msg: received_events.append(msg)
        event1 = {'event_type': 'compute.instance.update',
                  'traits': ['foo', 'bar'],
                  'message_id': '20d03d17-4aba-4900-a179-dba1281a3451',
                  'generated': '2016-04-23T06:50:21.622739'}
        event2 = {'event_type': 'compute.instance.update',
                  'traits': ['foo', 'bar'],
                  'message_id': '20d03d17-4aba-4900-a179-dba1281a3452',
                  'generated': '2016-04-23T06:50:23.622739'}
        msg_notifier.sample({}, 'event', event1)
        msg_notifier.sample({}, 'event', event2)


        svc = event.EventAlarmEvaluationService(0, self.CONF)