def test_request_with_missing_mandatory_arguments(self): request = msgpack.packb({ 'uid': '123-456-789', 'cmd': 'GET', }) request = Request(request)
def request_message(self, command, args, db_uid=None): db_uid = db_uid or self.default_db_uid return Request( msgpack.packb({ 'uid': db_uid, 'cmd': command, 'args': args, }))
def test_connect_to_invalid_database(self): message = Request( msgpack.packb({ 'uid': None, 'cmd': 'DBCONNECT', 'args': ['dadaislikeadad'] })) header, content = self.handler.command(message) plain_header = msgpack.unpackb(header) self.assertEqual(plain_header['status'], FAILURE_STATUS) self.assertEqual(plain_header['err_code'], DATABASE_ERROR)
def test_connect_to_valid_database(self): message = Request( msgpack.packb({ 'uid': None, 'cmd': 'DBCONNECT', 'args': ['default'], })) header, content = self.handler.command(message) plain_header = msgpack.unpackb(header) plain_content = msgpack.unpackb(content) self.assertEqual(plain_header['status'], SUCCESS_STATUS) self.assertIsNotNone(plain_content)
def test_valid_request_without_meta(self): request = msgpack.packb({ 'uid': '123-456-789', 'cmd': 'PUT', 'args': ['key', 'value'] }) request = Request(request) self.assertIsNotNone(request) self.assertTrue(hasattr(request, 'meta')) self.assertTrue(hasattr(request, 'db_uid')) self.assertTrue(hasattr(request, 'command')) self.assertTrue(hasattr(request, 'data')) self.assertEqual(request.db_uid, '123-456-789') self.assertEqual(request.command, 'PUT') self.assertEqual(request.data, ('key', 'value'))
def test_connect_automatically_mounts_and_unmounted_db(self): # Unmount by hand the database db_uid = self.handler.databases.index['name_to_uid']['default'] self.handler.databases[ db_uid].status = self.handler.databases.STATUSES.UNMOUNTED self.handler.databases[db_uid].connector = None message = Request( msgpack.packb({ 'db_uid': None, 'cmd': 'DBCONNECT', 'args': ['default'] })) header, content = self.handler.command(message) plain_header = msgpack.unpackb(header) self.assertEqual(plain_header['status'], SUCCESS_STATUS) self.assertEqual(self.handler.databases[db_uid].status, self.handler.databases.STATUSES.MOUNTED) self.assertIsInstance(self.handler.databases[db_uid].connector, plyvel.DB)
def handle_command(self): """Handles incoming command messages from backend socket Receives incoming messages in a non blocking way, sets it's set accordingly to IDLE or PROCESSING, and sends the responses in a non-blocking way. """ msg = None try: sender_id, msg = self.backend_socket.recv_multipart( copy=False, flags=zmq.NOBLOCK) except zmq.ZMQError as e: if e.errno == zmq.EAGAIN: return self.state = self.STATES.PROCESSING try: message = Request(msg) except MessageFormatError as e: errors_logger.exception(e.value) header = ResponseHeader(status=FAILURE_STATUS, err_code=REQUEST_ERROR, err_msg=e.value) content = ResponseContent(datas={}) self.backend_socket.send_multipart([sender_id, header, content], copy=False) return # Handle message, and execute the requested # command in leveldb header, response = self.handler.command(message) self.last_operation = (time.time(), message.db_uid) self.backend_socket.send_multipart([sender_id, header, response], flags=zmq.NOBLOCK, copy=False) self.state = self.STATES.IDLE return