def test_event_raises_with_too_few_args_global_method(self): event1 = Event() with self.assertRaises(InvalidApi): @event(event1) def ev(): return 1
def test_connect_one(self): event = Event() @event.connect def test1(): pass self.assertSetEqual(event.receivers, {test1})
def test_event_raises_with_too_few_args(self): event1 = Event() with self.assertRaises(InvalidApi): class TestClass(object): @event(event1) def ev(self): return 1
def test_event_has_arg_names_global_method(self): event1 = Event() @event(event1) def ev(data, a): return data self.assertListEqual(getattr(ev, '__arg_names'), ['data', 'a'])
def test_event_is_tagged_as_action(self): event1 = Event() class TestClass(object): @event(event1) def ev(self, data): return data self.assertTrue(getattr(TestClass.ev, 'action'))
def test_disconnect(self): event = Event() @event.connect def test1(): pass event.disconnect(test1) self.assertSetEqual(event.receivers, set())
def test_event_has_arg_names(self): event1 = Event() class TestClass(object): @event(event1) def ev(self, data, a): return data self.assertListEqual(getattr(TestClass.ev, '__arg_names'), ['self', 'data', 'a'])
def test_trigger(self): event = Event() result = {} @event.connect def test1(data): result['x'] = data event.trigger({1: 2}) self.assertDictEqual(result, {'x': {1: 2}})
def test_disconnect_invalid(self): event = Event() @event.connect def test1(): pass def test2(a, b, c): pass event.disconnect(test2) self.assertSetEqual(event.receivers, {test1})
def test_connect_redefined_function(self): event = Event() @event.connect def test1(): pass @event.connect def test2(a, b, c): pass self.assertSetEqual(event.receivers, {test1, test2})
def test_connect_two(self): event = Event() @event.connect def test1(data): pass @event.connect def test2(data): pass self.assertSetEqual(event.receivers, {test1, test2})
def test_multiple_triggers(self): event = Event() result = {} @event.connect def test1(data): result['x'] = data @event.connect def test2(a): result['y'] = a event.trigger({1: 2}) self.assertDictEqual(result, {'x': {1: 2}, 'y': {1: 2}})
def test_event_execution_with_timeout(self): event1 = Event('Event1') class TestClass(object): @event(event1, timeout=0) def ev(self, data): return data b = TestClass() test_data = {1: 2} def sender(): sleep(0.1) event1.trigger(test_data) spawn(sender) result = b.ev() self.assertEqual( result, ('Getting event Event1 timed out at 0 seconds', 'EventTimedOut')) self.assertSetEqual(event1.receivers, set())
def test_event_execution(self): event1 = Event('Event1') class TestClass(object): @event(event1) def ev(self, data): return data b = TestClass() test_data = {1: 2} def sender(): sleep(0.1) event1.trigger(test_data) start = default_timer() spawn(sender) result = b.ev() duration = default_timer() - start self.assertTupleEqual(result, (test_data, 'Success')) self.assertSetEqual(event1.receivers, set()) self.assertTrue(duration > 0.1)
def test_event_execution(self): event1 = Event('Event1') class TestClass(object): @event(event1) def ev(self, data): return data b = TestClass() test_data = {1: 2} def sender(): time.sleep(0.1) event1.trigger(test_data) thread = threading.Thread(target=sender) start = default_timer() thread.start() result = b.ev() duration = default_timer() - start thread.join() self.assertEqual(result, ActionResult(test_data, 'Success')) self.assertSetEqual(event1.receivers, set()) self.assertGreater(duration, 0.1)
def test_event_execution_with_timeout(self): event1 = Event('Event1') class TestClass(object): @event(event1, timeout=0) def ev(self, data): return data b = TestClass() test_data = {1: 2} def sender(): time.sleep(0.1) event1.trigger(test_data) thread = threading.Thread(target=sender) thread.start() result = b.ev() thread.join() self.assertEqual( result, ActionResult('Getting event Event1 timed out at 0 seconds', 'EventTimedOut')) self.assertSetEqual(event1.receivers, set())
from apps import Event, AppBlueprint from flask import Blueprint blueprint = AppBlueprint(blueprint=Blueprint('UtilitiesPage', __name__)) wait = Event('Wait') @blueprint.blueprint.route('/resume') def resume(): wait.trigger(None)
def test_constructor_with_name(self): event = Event('test') self.assertSetEqual(event.receivers, set()) self.assertEqual(event.name, 'test')
def test_constructor_default(self): event = Event() self.assertSetEqual(event.receivers, set()) self.assertEqual(event.name, '')
from apps import Event event1 = Event('Event1')
from apps import Event, AppBlueprint from flask import Blueprint, request blueprint = AppBlueprint(blueprint=Blueprint('TheHiveEvents', __name__)) hive_event = Event('TheHiveWebHook') @blueprint.blueprint.route('/thehive_webhook', methods=['POST']) def thehive_webhook(): data = request.get_json() print("Received data") print(data) hive_event.trigger(data) return 'Success'