def setUp(self): # Clean up the event file try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() config = Config( __engine__={ # rules can either be a JSON file or a Python object # 'rules': 'tests/rules/test_mock_component_rules.json', 'rules': [{ # Display name for the rule 'label': 'Test rule 1', 'when': [{ # React when receiving a MockEvent with id=1 'class': 'MockEvent', 'attributes': { 'id': 1 } }], 'then': [ # Trigger an EventFileWriter action that will write # the received event to the filepath argument { 'class': 'EventFileWriter', 'arguments': { 'filepath': 'tests/events.bin' } } ] }], 'db_path': 'tests/main.db', # Stop the engine after receiving the first event # (default: listen for event indefinitely) 'events_to_process': '1', # Spawn 3 workers for running the action 'workers': '3', }, my_mock_component={ # Module name for the component to be spawned 'module': 'evesp.component.mock_component', }) self.engine = Engine( config=config, # Callback to be invoked when the engine has terminated on_exit=self.__on_engine_exit) self.engine.start()
class TestMockComponent(unittest.TestCase): comp_name = 'My Mock Component' event_bin_file = os.path.join('tests', 'events.bin') n_events = 10 def setUp(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join( basedir, 'conf', 'test_mock_component_random_response_delay.conf') self.engine = Engine(config=Config(config_file), on_exit=self.__on_engine_exit) self.engine.start() def __on_engine_exit(self): self.engine_stopped.set() def test_mock_component(self): self.engine_stopped.wait() self.assertTrue(self.comp_name in self.engine.components) component = self.engine.components[self.comp_name] self.assertTrue(isinstance(component, RandomDelayMockComponent)) self.assertTrue(os.path.isfile(self.event_bin_file)) with open(self.event_bin_file, 'rb') as fp: events = [] while True: try: evt = pickle.load(fp) events.append(evt) except EOFError: break self.assertEqual(len(events), self.n_events) for evt in events: self.assertTrue(isinstance(evt, MockEvent)) self.assertEqual(evt.id, 1) self.assertEqual(evt.name, 'Test event') actions = self.engine._get_actions() self.assertTrue(len(actions) == self.n_events) for action_response in actions.values(): self.assertTrue(isinstance(action_response, SuccessActionResponse)) def tearDown(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass
class TestMockComponentMultipleEvents(unittest.TestCase): comp_name = 'My Mock Component' event_bin_file = os.path.join('tests', 'events.bin') n_events = 10 def setUp(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join(basedir, 'conf', 'test_mock_component_multiple_events.conf') self.engine = Engine( config = Config(config_file), on_exit = self.__on_engine_exit ) self.engine.start() def __on_engine_exit(self): self.engine_stopped.set() def test_mock_component(self): self.engine_stopped.wait() self.assertTrue(self.comp_name in self.engine.components) component = self.engine.components[self.comp_name] self.assertTrue(isinstance(component, MockComponent)) self.assertTrue(os.path.isfile(self.event_bin_file)) with open(self.event_bin_file, 'rb') as fp: events = [] while True: try: evt = pickle.load(fp) events.append(evt) except EOFError: break self.assertEqual(len(events), self.n_events) for evt in events: self.assertTrue(isinstance(evt, MockEvent)) self.assertEqual(evt.id, 1) self.assertEqual(evt.name, 'Test event') actions = self.engine._get_actions() self.assertTrue(len(actions) == self.n_events) for action_response in actions.values(): self.assertTrue(isinstance(action_response, SuccessActionResponse)) def tearDown(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass
def setUp(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join( basedir, 'conf', 'test_mock_component_random_response_delay.conf') self.engine = Engine(config=Config(config_file), on_exit=self.__on_engine_exit) self.engine.start()
def setUp(self): self.__cleanup() self.__create_mock_file() # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join(basedir, 'conf', 'test_file_system_event_component.conf') self.engine = Engine(config=Config(config_file), on_exit=self.__on_engine_exit) threading.Thread(target=self.engine.start).start() self.engine.wait_running() time.sleep(0.1) threading.Thread(target=self.__create_mock_file).start()
def setUp(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join(basedir, 'conf', 'test_mock_component_multiple_events.conf') self.engine = Engine( config = Config(config_file), on_exit = self.__on_engine_exit ) self.engine.start()
def setUp(self): self.__cleanup() self.__create_mock_file() # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join( basedir, 'conf', 'test_file_system_event_component.conf' ) self.engine = Engine( config=Config(config_file), on_exit=self.__on_engine_exit ) threading.Thread(target=self.engine.start).start() self.engine.wait_running() time.sleep(0.1) threading.Thread(target=self.__create_mock_file).start()
class TestFileSystemEventComponent(unittest.TestCase): comp_name = 'File system event component' event_bin_file = os.path.join('tests', 'events.bin') watched_file = os.path.join('tests', 'mock_file') db_file = os.path.join('tests', 'main.db') def __create_mock_file(self): with open(self.watched_file, 'w') as fp: fp.write('original content') def __cleanup(self): for f in [self.event_bin_file, self.watched_file, self.db_file]: try: os.unlink(f) except FileNotFoundError: pass def setUp(self): self.__cleanup() self.__create_mock_file() # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join( basedir, 'conf', 'test_file_system_event_component.conf' ) self.engine = Engine( config=Config(config_file), on_exit=self.__on_engine_exit ) threading.Thread(target=self.engine.start).start() self.engine.wait_running() time.sleep(0.1) threading.Thread(target=self.__create_mock_file).start() def __on_engine_exit(self): self.engine_stopped.set() def test_file_system_event_component(self): self.engine_stopped.wait() self.assertTrue(self.comp_name in self.engine.components) component = self.engine.components[self.comp_name] self.assertTrue(isinstance(component, FileSystemEventComponent)) self.assertTrue(os.path.isfile(self.event_bin_file)) with open(self.event_bin_file, 'rb') as fp: events = [] while True: try: evt = pickle.load(fp) events.append(evt) except EOFError: break self.assertEqual(len(events), 1) evt = events[0] self.assertTrue(isinstance(evt, FileSystemEvent)) def tearDown(self): self.__cleanup()
class TestMockComponent(unittest.TestCase): comp_name = 'my_mock_component' event_bin_file = os.path.join('tests', 'events.bin') def setUp(self): # Clean up the event file try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() config = Config( __engine__={ # rules can either be a JSON file or a Python object # 'rules': 'tests/rules/test_mock_component_rules.json', 'rules': [{ # Display name for the rule 'label': 'Test rule 1', 'when': [{ # React when receiving a MockEvent with id=1 'class': 'MockEvent', 'attributes': { 'id': 1 } }], 'then': [ # Trigger an EventFileWriter action that will write # the received event to the filepath argument { 'class': 'EventFileWriter', 'arguments': { 'filepath': 'tests/events.bin' } } ] }], 'db_path': 'tests/main.db', # Stop the engine after receiving the first event # (default: listen for event indefinitely) 'events_to_process': '1', # Spawn 3 workers for running the action 'workers': '3', }, my_mock_component={ # Module name for the component to be spawned 'module': 'evesp.component.mock_component', }) self.engine = Engine( config=config, # Callback to be invoked when the engine has terminated on_exit=self.__on_engine_exit) self.engine.start() def __on_engine_exit(self): self.engine_stopped.set() def test_mock_component(self): # Wait for the engine to stop self.engine_stopped.wait() self.assertTrue(self.comp_name in self.engine.components) component = self.engine.components[self.comp_name] self.assertTrue(isinstance(component, MockComponent)) self.assertTrue(os.path.isfile(self.event_bin_file)) with open(self.event_bin_file, 'rb') as fp: events = [] while True: try: evt = pickle.load(fp) events.append(evt) except EOFError: break self.assertEqual(len(events), 1) evt = events[0] self.assertTrue(isinstance(evt, MockEvent)) self.assertEqual(evt.id, 1) self.assertEqual(evt.name, 'Test event') def tearDown(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass
def setUp(self): # Clean up the event file try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() config = Config( __engine__={ # rules can either be a JSON file or a Python object # 'rules': 'tests/rules/test_mock_component_rules.json', 'rules': [ { # Display name for the rule 'label': 'Test rule 1', 'when': [ { # React when receiving a MockEvent with id=1 'class': 'MockEvent', 'attributes': { 'id': 1 } } ], 'then': [ # Trigger an EventFileWriter action that will write # the received event to the filepath argument { 'class': 'EventFileWriter', 'arguments': { 'filepath': 'tests/events.bin' } } ] } ], 'db_path': 'tests/main.db', # Stop the engine after receiving the first event # (default: listen for event indefinitely) 'events_to_process': '1', # Spawn 3 workers for running the action 'workers': '3', }, my_mock_component={ # Module name for the component to be spawned 'module': 'evesp.component.mock_component', } ) self.engine = Engine( config=config, # Callback to be invoked when the engine has terminated on_exit=self.__on_engine_exit) self.engine.start()
class TestMockComponent(unittest.TestCase): comp_name = 'my_mock_component' event_bin_file = os.path.join('tests', 'events.bin') def setUp(self): # Clean up the event file try: os.unlink(self.event_bin_file) except FileNotFoundError: pass # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() config = Config( __engine__={ # rules can either be a JSON file or a Python object # 'rules': 'tests/rules/test_mock_component_rules.json', 'rules': [ { # Display name for the rule 'label': 'Test rule 1', 'when': [ { # React when receiving a MockEvent with id=1 'class': 'MockEvent', 'attributes': { 'id': 1 } } ], 'then': [ # Trigger an EventFileWriter action that will write # the received event to the filepath argument { 'class': 'EventFileWriter', 'arguments': { 'filepath': 'tests/events.bin' } } ] } ], 'db_path': 'tests/main.db', # Stop the engine after receiving the first event # (default: listen for event indefinitely) 'events_to_process': '1', # Spawn 3 workers for running the action 'workers': '3', }, my_mock_component={ # Module name for the component to be spawned 'module': 'evesp.component.mock_component', } ) self.engine = Engine( config=config, # Callback to be invoked when the engine has terminated on_exit=self.__on_engine_exit) self.engine.start() def __on_engine_exit(self): self.engine_stopped.set() def test_mock_component(self): # Wait for the engine to stop self.engine_stopped.wait() self.assertTrue(self.comp_name in self.engine.components) component = self.engine.components[self.comp_name] self.assertTrue(isinstance(component, MockComponent)) self.assertTrue(os.path.isfile(self.event_bin_file)) with open(self.event_bin_file, 'rb') as fp: events = [] while True: try: evt = pickle.load(fp) events.append(evt) except EOFError: break self.assertEqual(len(events), 1) evt = events[0] self.assertTrue(isinstance(evt, MockEvent)) self.assertEqual(evt.id, 1) self.assertEqual(evt.name, 'Test event') def tearDown(self): try: os.unlink(self.event_bin_file) except FileNotFoundError: pass
class TestFileSystemEventComponent(unittest.TestCase): comp_name = 'File system event component' event_bin_file = os.path.join('tests', 'events.bin') watched_file = os.path.join('tests', 'mock_file') db_file = os.path.join('tests', 'main.db') def __create_mock_file(self): with open(self.watched_file, 'w') as fp: fp.write('original content') def __cleanup(self): for f in [self.event_bin_file, self.watched_file, self.db_file]: try: os.unlink(f) except FileNotFoundError: pass def setUp(self): self.__cleanup() self.__create_mock_file() # Synchronize on this event to wait the engine stop self.engine_stopped = threading.Event() basedir = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join(basedir, 'conf', 'test_file_system_event_component.conf') self.engine = Engine(config=Config(config_file), on_exit=self.__on_engine_exit) threading.Thread(target=self.engine.start).start() self.engine.wait_running() time.sleep(0.1) threading.Thread(target=self.__create_mock_file).start() def __on_engine_exit(self): self.engine_stopped.set() def test_file_system_event_component(self): self.engine_stopped.wait() self.assertTrue(self.comp_name in self.engine.components) component = self.engine.components[self.comp_name] self.assertTrue(isinstance(component, FileSystemEventComponent)) self.assertTrue(os.path.isfile(self.event_bin_file)) with open(self.event_bin_file, 'rb') as fp: events = [] while True: try: evt = pickle.load(fp) events.append(evt) except EOFError: break self.assertEqual(len(events), 1) evt = events[0] self.assertTrue(isinstance(evt, FileSystemEvent)) def tearDown(self): self.__cleanup()