def test_multi_processor(self): config = Config(processors_tag_regex=['.+\.event_a','.+\.event_b']) app = App(config) class EventA(Event): __tag__ = 'test.event_a' pass class EventB(Event): __tag__ = 'test.event_b' pass @app.handler(EventA) def fooA(context, event): pass @app.handler(EventB) def fooB(context, event): pass app.emit(EventA()) app.emit(EventB()) self.assertEqual(app.processors[0].tag_regex, '.+\.event_a') self.assertEqual(app.processors[1].tag_regex, '.+\.event_b') self.assertEqual(app.processors[2].tag_regex, '.*') processor_event_a = app.get_processor_by_tag_regex('.+\.event_a') processor_event_b = app.get_processor_by_tag_regex('.+\.event_b') processor_default = app.get_processor_by_tag_regex('.*') self.assertEqual(processor_event_a.process_count, 1) self.assertEqual(processor_event_b.process_count, 1) self.assertEqual(processor_default.process_count, 0)
def test_allowed_fire(self): app = App('test_allowed_fire', config) def fire(self, names): names.append('fire') event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) boo_handler = Handler(boo) eoo_handler = Handler(eoo) fire_handler = Handler(fire, ['display']) app.on('show', foo_handler) app.on('show', boo_handler, True) app.on('show', fire_handler, True) app.on('display', eoo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertListEqual( names, ['foo-A', 'boo-A', 'fire', 'eoo-B']) self.assertEqual(runtime_tree.event_count, 2) self.assertEqual(runtime_tree.handler_runtime_count, 4) self.assertEqual(runtime_tree.exception_count, 1) self.assertNotEqual(runtime_tree.begin_time, None) self.assertNotEqual(runtime_tree.end_time, None) self.assertNotEqual(runtime_tree.time_cost, -1)
def test_save_and_find(self): storage = RuntimeTreeStorage(db_path) app = App('test_save_and_find') foo_handler = Handler(foo) app.on('show', foo_handler) show_event = Event('show', name='A') runtime_tree = app.fire(show_event, False) storage.save(runtime_tree) self.assertEqual(runtime_tree.dict, storage.find(runtime_tree.id))
def test_handler_decorator(self): app = App('test_handler_decorator', config) @app.handler('show', ['display'], True) def koo(self, names, name): names.append('koo-%s' % (name, )) event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) app.on('display', foo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertListEqual(names, ['koo-A', 'foo-B']) self.assertEqual(runtime_tree.event_count, 2) self.assertEqual(runtime_tree.handler_runtime_count, 2) self.assertEqual(runtime_tree.exception_count, 0) self.assertNotEqual(runtime_tree.begin_time, None) self.assertNotEqual(runtime_tree.end_time, None) self.assertNotEqual(runtime_tree.time_cost, -1)
def test_pickle(self): app = App('test_pickle', config) def fire(self): names.append('fire') event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) boo_handler = Handler(boo) eoo_handler = Handler(eoo) fire_handler = Handler(fire, ['display']) app.on('show', foo_handler) app.on('show', boo_handler, True) app.on('show', fire_handler, True) app.on('display', eoo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertDictEqual( RuntimeTree.loads( runtime_tree.dumps()), runtime_tree.dict)
def test_handler_decorator(self): app = App('test_handler_decorator', config) @app.handler('show', ['display'], True) def koo(self, names, name): names.append('koo-%s' % (name,)) event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) app.on('display', foo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertListEqual( names, ['koo-A', 'foo-B']) self.assertEqual(runtime_tree.event_count, 2) self.assertEqual(runtime_tree.handler_runtime_count, 2) self.assertEqual(runtime_tree.exception_count, 0) self.assertNotEqual(runtime_tree.begin_time, None) self.assertNotEqual(runtime_tree.end_time, None) self.assertNotEqual(runtime_tree.time_cost, -1)
def test_on_and_find(self): app = App('test_on_and_find', config) handlers = app.find_handlers('show') self.assertListEqual(handlers, list()) foo_handler = Handler(foo) boo_handler = Handler(boo) app.on('show', foo_handler) app.on('show', boo_handler, True) handlers = app.find_handlers('show') self.assertListEqual(handlers, [foo_handler, boo_handler]) self.assertListEqual(app._App__global.event_handler_map['show'], [foo_handler]) self.assertListEqual(app._App__local.event_handler_map['show'], [boo_handler])
def __init__(self, *args, **kwargs): super(TestDashboard, self).__init__(*args, **kwargs) self.config = Config( source_event_cls=(AEvent,), processors_tag_regex=['a\..+',] ) self.app = App(self.config) @self.app.handler(AEvent) def foo(context, event): pass self.dashboard = Dashboard(self.app) self.client = self.dashboard.flask_app.test_client() self.dashboard.flask_app.debug = True
def test_on_and_find(self): app = App('test_on_and_find', config) handlers = app.find_handlers('show') self.assertListEqual(handlers, list()) foo_handler = Handler(foo) boo_handler = Handler(boo) app.on('show', foo_handler) app.on('show', boo_handler, True) handlers = app.find_handlers('show') self.assertListEqual(handlers, [foo_handler, boo_handler]) self.assertListEqual( app._App__global.event_handler_map['show'], [foo_handler]) self.assertListEqual( app._App__local.event_handler_map['show'], [boo_handler])
def test_not_allowed_fire(self): app = App('test_not_allowed_fire', config) def fire(self, names): names.append('fire') event = Event('display', names=names, name='B') self.fire(event) eoo_handler = Handler(eoo) fire_handler = Handler(fire) app.on('show', fire_handler, True) app.on('display', eoo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertListEqual(names, ['fire']) self.assertEqual(runtime_tree.event_count, 1) self.assertEqual(runtime_tree.handler_runtime_count, 1) self.assertEqual(runtime_tree.exception_count, 1) self.assertNotEqual(runtime_tree.begin_time, None) self.assertNotEqual(runtime_tree.end_time, None) self.assertNotEqual(runtime_tree.time_cost, -1)
def test_start_and_stop(self): app = App('test_start_and_stop', config) app.start() foo_handler = Handler(foo) boo_handler = Handler(boo) app.on('show', foo_handler) app.on('show', boo_handler, True) names = list() show_event = Event('show', names=names, name='background') app.fire(show_event, False) time.sleep(2) app.stop() self.assertListEqual( names, ['foo-background', 'boo-background'])
def test_local(self): app = App('test_local', config) self.assertDictEqual(app._App__local.event_handler_map, dict()) self.assertEqual(app._App__local.unknown, None)
def test_config(self): app = App('test_config', config) self.assertEqual(True, app.config.debug) self.assertEqual('/tmp/test.log', app.config.log_path) self.assertEqual(None, app.config.unknown)
def test_start_and_stop(self): app = App('test_start_and_stop', config) app.start() foo_handler = Handler(foo) boo_handler = Handler(boo) app.on('show', foo_handler) app.on('show', boo_handler, True) names = list() show_event = Event('show', names=names, name='background') app.fire(show_event, False) time.sleep(2) app.stop() self.assertListEqual(names, ['foo-background', 'boo-background'])
def test_pickle(self): app = App('test_pickle', config) def fire(self): names.append('fire') event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) boo_handler = Handler(boo) eoo_handler = Handler(eoo) fire_handler = Handler(fire, ['display']) app.on('show', foo_handler) app.on('show', boo_handler, True) app.on('show', fire_handler, True) app.on('display', eoo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertDictEqual(RuntimeTree.loads(runtime_tree.dumps()), runtime_tree.dict)
__tag__ = 'my_event_tag' __description__ = 'my_event_description' my_field_one = Field(str, 'test') my_field_two = Field(int, 100) class MyEventB(Event): pass config = Config( app_name='My First App', source_event_cls=(MyEventA,), processors_tag_regex=['^my_event_tag$', '.*'], dashboard_host='0.0.0.0', dashboard_port=9527) app = App(config) @app.handler(MyEventA, derivative_events=[MyEventB]) def my_event_a_handler(context, event): time.sleep(random.random()) print event. my_field_one print event. my_field_two choice = random.choice([0, 1]) if choice == 0: return Emittion(MyEventB()) else: return NoEmittion(MyEventB , 'choice == 1')
class TestDashboard(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestDashboard, self).__init__(*args, **kwargs) self.config = Config( source_event_cls=(AEvent,), processors_tag_regex=['a\..+',] ) self.app = App(self.config) @self.app.handler(AEvent) def foo(context, event): pass self.dashboard = Dashboard(self.app) self.client = self.dashboard.flask_app.test_client() self.dashboard.flask_app.debug = True # self.dashboard.run(daemon=False) def setUp(self): pass def tearDown(self): pass def test_index(self): rv = self.client.get('/') self.assertTrue('Earo Dashboard' in rv.data) def test_configuration(self): rv = self.client.get('/configuration') result = json.loads(rv.data) self.assertEqual(result['c'], 0) self.assertTrue('app_name' in result['d']) def test_source_event_cls_list(self): rv = self.client.get('/source_event_cls_list') result = json.loads(rv.data) self.assertEqual(result['c'], 0) self.assertDictEqual( result['d'][0], {'source_event_cls': 'test_dashboard.AEvent', 'source_event_tag': 'a.event', 'source_event_description': 'This is AEvent\'s description.'}) def test_processor_list(self): self.app.emit(AEvent()) rv = self.client.get('/processor_list') result = json.loads(rv.data) self.assertEqual(result['c'], 0) self.assertSequenceEqual(result['d'][0]['tag_regex'], 'a\..+') self.assertSequenceEqual(result['d'][1]['tag_regex'], '.*') self.assertTrue('process_count' in result['d'][0]) self.assertTrue('exception_count' in result['d'][0]) self.assertTrue('event_statistics' in result['d'][0]) self.assertTrue('test_dashboard.AEvent' in result[ 'd'][0]['event_statistics']) self.assertTrue( 'process_count' in result['d'][0]['event_statistics'] ['test_dashboard.AEvent']) self.assertTrue('exception_count' in result['d'][0][ 'event_statistics']['test_dashboard.AEvent']) self.assertTrue( 'min_time_cost' in result['d'][0]['event_statistics'] ['test_dashboard.AEvent']) self.assertTrue( 'max_time_cost' in result['d'][0]['event_statistics'] ['test_dashboard.AEvent']) def test_preview_process_flow(self): rv = self.client.get('/preview_process_flow/test_dashboard.AEvent') self.assertTrue('Preview Process Flow' in rv.data) def test_latest_process_flow(self): self.app._source_event_cls_to_latest_active_process_flow.clear() rv = self.client.get('/latest_process_flow/test_dashboard.AEvent') result = json.loads(rv.data) self.assertEqual(result['c'], -1) self.app.emit(AEvent()) rv = self.client.get('/latest_process_flow/test_dashboard.AEvent') self.assertTrue('Latest Process Flow' in rv.data)