def test_create_pool(self, mock_r): mock_r.connect = MagicMock() mock_r.close = MagicMock() from repool.pool import ConnectionPool p = ConnectionPool(cleanup=1) self.assertFalse(p.empty()) p.release_pool()
def __init__(self, config): self.config = config print(self.config) self.name = self.config['name'] self.id = self.config['id'] self.db_name = self.config['db_name'] self.rdb = r.connect(host=self.config['db_host'], port=self.config['db_port']) try: r.db_create(self.db_name).run(self.rdb) r.db(self.db_name)\ .table_create('incidents', primary_key='slack_channel')\ .run(self.rdb) print('Database setup completed.') except RqlRuntimeError: print('App database already exists.') self.rdb.close() self.pool = ConnectionPool(host=self.config['db_host'], port=self.config['db_port'], db=self.db_name)
def test_connect(self, mock_r): mock_r.connect = MagicMock() mock_r.close = MagicMock() from repool.pool import ConnectionPool p = ConnectionPool(pool_size=1) with p.connect() as conn: self.assertEqual(0, p._pool.qsize()) self.assertEqual(1, p._pool.qsize()) p.release_pool()
def __init__(self, config): self.config = config print(self.config) self.name = self.config['name'] self.id = self.config['id'] self.db_name = self.config['db_name'] self.rdb = r.connect( host=self.config['db_host'], port=self.config['db_port'] ) try: r.db_create(self.db_name).run(self.rdb) r.db(self.db_name)\ .table_create('incidents', primary_key='slack_channel')\ .run(self.rdb) print('Database setup completed.') except RqlRuntimeError: print('App database already exists.') self.rdb.close() self.pool = ConnectionPool( host=self.config['db_host'], port=self.config['db_port'], db=self.db_name )
def test_acquire(self, mock_r): mock_r.connect = MagicMock() mock_r.close = MagicMock() from repool.pool import ConnectionPool p = ConnectionPool(cleanup=1) conn = p.acquire() self.assertIsInstance(conn, MagicMock) p.release(conn) p.release_pool()
def test_release_pool(self, mock_r): mock_r.connect = MagicMock() mock_r.close = MagicMock() from repool.pool import ConnectionPool, PoolException p = ConnectionPool(cleanup=1) conn1 = p.acquire() conn2 = p.acquire() p.release(conn1) with self.assertRaises(PoolException): p.release_pool()
def test_acquire_one(self, mock_r): mock_r.connect = MagicMock() mock_r.close = MagicMock() from repool.pool import ConnectionPool p = ConnectionPool(cleanup=1) nb_init = p._pool.qsize() conn = p.acquire() nb_term = p._pool.qsize() self.assertEqual(nb_init-1, nb_term) p.release(conn) p.release_pool()
class CommanderBase: """ Incident commander main class """ def __init__(self, config): self.config = config print(self.config) self.name = self.config['name'] self.id = self.config['id'] self.db_name = self.config['db_name'] self.rdb = r.connect( host=self.config['db_host'], port=self.config['db_port'] ) try: r.db_create(self.db_name).run(self.rdb) r.db(self.db_name)\ .table_create('incidents', primary_key='slack_channel')\ .run(self.rdb) print('Database setup completed.') except RqlRuntimeError: print('App database already exists.') self.rdb.close() self.pool = ConnectionPool( host=self.config['db_host'], port=self.config['db_port'], db=self.db_name ) def pre_message(self): try: self.rdb = self.pool.acquire() except RqlDriverError: print("Could not connect to db") def post_message(self): self.pool.release(self.rdb) def process_message(self, message): self.pre_message() return_val = self.parse_message(message) self.post_message() return return_val def parse_message(self, message): if not self.valid_message(message): return "" stripped_message = message.get('text') if stripped_message is None: return "" else: stripped_message = stripped_message.strip() name_match = re.match(r'<@?{}>:?\s*(.*)'.format(self.id), stripped_message, flags=re.IGNORECASE) if name_match: commands = name_match.groups()[0] return self.parse_commands(commands, channel=message['channel'], user=message['user']) if message['channel'].startswith('D'): return self.parse_commands(stripped_message, channel=message['channel'], user=message.get('user')) def valid_message(self, message): return message.get('user') != self.id def parse_commands(self, commands, channel): return NotImplementedError
class CommanderBase: """ Incident commander main class """ def __init__(self, config): self.config = config print(self.config) self.name = self.config['name'] self.id = self.config['id'] self.db_name = self.config['db_name'] self.rdb = r.connect(host=self.config['db_host'], port=self.config['db_port']) try: r.db_create(self.db_name).run(self.rdb) r.db(self.db_name)\ .table_create('incidents', primary_key='slack_channel')\ .run(self.rdb) print('Database setup completed.') except RqlRuntimeError: print('App database already exists.') self.rdb.close() self.pool = ConnectionPool(host=self.config['db_host'], port=self.config['db_port'], db=self.db_name) def pre_message(self): try: self.rdb = self.pool.acquire() except RqlDriverError: print("Could not connect to db") def post_message(self): self.pool.release(self.rdb) def process_message(self, message): self.pre_message() return_val = self.parse_message(message) self.post_message() return return_val def parse_message(self, message): if not self.valid_message(message): return "" stripped_message = message.get('text') if stripped_message is None: return "" else: stripped_message = stripped_message.strip() name_match = re.match(r'<@?{}>:?\s*(.*)'.format(self.id), stripped_message, flags=re.IGNORECASE) if name_match: commands = name_match.groups()[0] return self.parse_commands(commands, channel=message['channel'], user=message['user']) if message['channel'].startswith('D'): return self.parse_commands(stripped_message, channel=message['channel'], user=message.get('user')) def valid_message(self, message): return message.get('user') != self.id def parse_commands(self, commands, channel): return NotImplementedError