Exemplo n.º 1
0
    def __init__(self):
        """
        init method
        """
        self.channel_mgr = channel_manager.ChannelManager()

        self.request_list = set()

        self.lock = threading.Lock()
Exemplo n.º 2
0
    def __init__(self):
        """
        init method
        """
        self.channel_mgr = channel_manager.ChannelManager()

        self.facial_recognize_manage = facial_recognition_server.FacialRecognitionManager(
        )

        self.request_list = set()

        self.lock = threading.Lock()

        self.videostate = {"ret": 0, "msg": {"name": "", "req": 0}}
 def test_single_instance(self):
     """test_single_instance"""
     cm_new = channel_manager.ChannelManager(["image", "video"])
     self.assertEqual(cm_new, self.manager)
class TestChannelManager(unittest.TestCase):
    """TestChannelManager"""
    manager = channel_manager.ChannelManager(["image", "video"])

    def test_single_instance(self):
        """test_single_instance"""
        cm_new = channel_manager.ChannelManager(["image", "video"])
        self.assertEqual(cm_new, self.manager)

    def test_create_channel_resource(self):
        """test_create_channel_resource"""
        channel_name = "video"
        channel_fd = 100
        media_type = "video"
        self.manager.register_one_channel(channel_name)
        handler = presenter_socket_server.ChannelHandler(channel_name, media_type)
        self.assertEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))
        self.manager.create_channel_resource(channel_name, channel_fd, media_type, handler)
        self.assertNotEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertNotEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))
        self.assertEqual(True, self.manager.is_channel_busy(channel_name))


        self.manager.clean_channel_resource_by_name(channel_name)
        self.assertEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))
        self.assertEqual(False, self.manager.is_channel_busy(channel_name))

    @patch('common.channel_handler.ChannelHandler')
    def test_close_all_thread(self, mock_class):
        """test_close_all_thread"""
        #prepare
        mock_handler = mock_class.return_value
        mock_handler.close_thread.return_value = None

        channel_name = "video"
        channel_fd = 100
        media_type = "video"
        handler = mock_handler
        self.manager.register_one_channel(channel_name)
        self.assertEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))
        self.manager.create_channel_resource(channel_name, channel_fd, media_type, handler)
        self.assertNotEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertNotEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))

        #test
        self.manager.close_all_thread()

        #clean
        self.manager.clean_channel_resource_by_name(channel_name)

    @patch('common.channel_handler.ChannelHandler')
    def test_list_channel_paths(self, mock_class):
        """test_list_channel_paths"""
        #prepare
        mock_handler = mock_class.return_value
        mock_handler.close_thread.return_value = None

        channel_name = "video"
        channel_fd = 100
        media_type = "video"
        handler = mock_handler
        self.manager.register_one_channel(channel_name)
        self.assertEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))
        self.manager.create_channel_resource(channel_name, channel_fd, media_type, handler)
        self.manager.create_channel_resource(channel_name, channel_fd, media_type, handler)
        self.assertNotEqual(None, self.manager.get_channel_handler_by_name(channel_name))
        self.assertNotEqual(None, self.manager.get_channel_handler_by_fd(channel_fd))

        #test
        self.manager.list_channels()

        #clean
        self.manager.clean_channel_resource_by_name(channel_name)

    def test_register_one_channel_path(self):
        """test_register_one_channel_path"""
        #test
        for i in self.manager.list_channels():
            channel_name = i["name"]
            self.manager.unregister_one_channel(channel_name)

        channel_name = "video1"
        self.assertEqual(False, self.manager.is_channel_exist(channel_name))

        for i in range(1, 10):
            channel_name = "video{}".format(i)
            ret = self.manager.register_one_channel(channel_name)
            self.assertEqual(ret, self.manager.err_code_ok)
            self.assertEqual(True, self.manager.is_channel_exist(channel_name))

        channel_name = "video9"
        ret = self.manager.register_one_channel(channel_name)
        self.assertEqual(ret, self.manager.err_code_repeat_channel)

        channel_name = "video10"
        ret = self.manager.register_one_channel(channel_name)
        self.assertEqual(ret, self.manager.err_code_ok)
        self.assertEqual(True, self.manager.is_channel_exist(channel_name)) 

        channel_name = "video11"
        ret = self.manager.register_one_channel(channel_name)
        self.assertEqual(ret, self.manager.err_code_too_many_channel)

        for i in self.manager.list_channels():
            channel_name = i["name"]
            self.manager.unregister_one_channel(channel_name)

    def test_save_channel_path_image(self):
        """test_save_channel_path_image"""
        channel_name = "image"
        image_data = "image"
        self.manager.register_one_channel(channel_name)

        self.manager.clean_channel_image(channel_name)
        ret = self.manager.get_channel_image(channel_name)
        self.assertEqual(ret, None)

        #test
        self.manager.save_channel_image(channel_name, image_data)

        ret = self.manager.get_channel_image(channel_name)
        self.assertNotEqual(ret, None)

        self.manager.clean_channel_image(channel_name)

        ret = self.manager.get_channel_image(channel_name)
        self.assertEqual(ret, None)

    def test_get_channel_image_null(self):
        """test_get_channel_image_null"""
        channel_name = "abcd"

        ret = self.manager.get_channel_image(channel_name)
        self.assertEqual(ret, None)
PB_HEART_BEAT_MESSAGE_FULL_NAME = pb._HEARTBEATMESSAGE.full_name
PB_PRESENT_IMAGE_REQUEST_FULL_NAME = pb._PRESENTIMAGEREQUEST.full_name

PB_OPEN_CHANNEL_ERROR_NONE = pb.kOpenChannelErrorNone
PB_OPEN_CHANNEL_ERROR_NO_SUCH_CHANNEL = pb.kOpenChannelErrorNoSuchChannel
PB_OPEN_CHANNEL_ERROR_CHANNEL_ALREADY_OPENED = pb.kOpenChannelErrorChannelAlreadyOpened
PB_OPEN_CHANNEL_ERROR_OTHER = pb.kOpenChannelErrorOther
PB_CHANNEL_CONTENT_TYPE_IMAGE = pb.kChannelContentTypeImage
PB_CHANNEL_CONTENT_TYPE_VIDEO = pb.kChannelContentTypeVideo
PB_IMAGE_FORMAT_JPEG = pb.kImageFormatJpeg
PB_PRESENT_DATA_ERROR_NONE = pb.kPresentDataErrorNone
PB_PRESENT_DATA_ERROR_UNSUPPORTED_TYPE = pb.kPresentDataErrorUnsupportedType
PB_PRESENT_DATA_ERROR_UNSUPPORTED_FORMAT = pb.kPresentDataErrorUnsupportedFormat
PB_PRESENT_DATA_ERROR_OTHER = pb.kPresentDataErrorOther

CHANNEL_MANAGER = channel_manager.ChannelManager(["image", "video"])
HOST = "127.127.0.1"
PORT_BEGIN = 20000


def get_socket_server_addr():
    """func"""
    global PORT_BEGIN
    server_addr = (HOST, PORT_BEGIN)
    for i in range(PORT_BEGIN, 65535):
        ret = os.system("netstat  -nl | grep {}".format(PORT_BEGIN))
        if ret != 0:
            server_addr = (HOST, PORT_BEGIN)
            break
        else:
            print("port conflict:%u", PORT_BEGIN)
class TestAppManager(unittest.TestCase):
    """TestChannelManager"""
    channel_manager = channel_manager.ChannelManager(["image", "video"])
    manager = app_manager.AppManager()

    def test_single_instance(self):
        """test_single_instance"""
        manager_new = app_manager.AppManager()
        self.assertEqual(manager_new, self.manager)

    @patch("socket.socket.fileno", return_value=1)
    @patch("socket.socket.settimeout", return_value=True)
    def test_register_and_unregister_app(self, mock1, mock2):
        """test_single_instance"""
        app_id = "app1"
        app_id_2 = "app2"
        sock_fd = 1
        sock_fd_2 = 2
        sock = create_sock_client()
        ret = TestAppManager.manager.register_app(app_id, sock)
        self.assertEqual(ret, True)

        ret = TestAppManager.manager.register_app(app_id, sock)
        self.assertEqual(ret, False)

        ret = TestAppManager.manager.get_socket_by_app_id(app_id)
        self.assertEqual(ret, sock)

        ret = TestAppManager.manager.get_socket_by_app_id(app_id_2)
        self.assertEqual(ret, None)

        ret = TestAppManager.manager.get_app_id_by_socket(sock_fd)
        self.assertEqual(ret, app_id)

        ret = TestAppManager.manager.get_app_id_by_socket(sock_fd_2)
        self.assertEqual(ret, None)

        ret = TestAppManager.manager.is_app_exist(app_id)
        self.assertEqual(ret, True)

        ret = TestAppManager.manager.is_app_exist(app_id_2)
        self.assertEqual(ret, False)

        ret = TestAppManager.manager.get_app_num()
        self.assertEqual(ret, 1)

        ret = TestAppManager.manager.list_app()
        self.assertEqual(ret, [app_id])

        TestAppManager.manager.set_heartbeat(sock_fd)
        TestAppManager.manager.unregister_app_by_fd(sock_fd)
        TestAppManager.manager.set_thread_switch()
        time.sleep(1.5)

    @patch("socket.socket.fileno", return_value=1)
    @patch("socket.socket.settimeout", return_value=True)
    def test_app_heartbeat_timeout(self, mock1, mock2):
        """test_single_instance"""
        app_id = "app1"
        sock_fd = 1
        sock = create_sock_client()
        backup = app_manager.HEARTBEAT_TIMEOUT
        app_manager.HEARTBEAT_TIMEOUT = 1
        ret = TestAppManager.manager.register_app(app_id, sock)
        self.assertEqual(ret, True)
        time.sleep(2)
        TestAppManager.manager.unregister_app_by_fd(sock_fd)
        app_manager.HEARTBEAT_TIMEOUT = backup