def test_receive_image_by_2_step(self):
        """utest"""
        server_address = get_socket_server_addr()
        print(server_address)
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        #test image
        channel_name = "image_2_step"
        CHANNEL_MANAGER.register_one_channel(channel_name)
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, True)

        ret = send_one_image_message_two_step(client, None)
        self.assertEqual(ret, True)

        #clean
        client.close()
        time.sleep(0.1)
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        CHANNEL_MANAGER.unregister_one_channel(channel_name)
        server.stop_thread()
    def test_receive_video_flow(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        #test video
        channel_name = "video"
        media_type = PB_CHANNEL_CONTENT_TYPE_VIDEO

        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)

        ret = response_open_channel(client)
        self.assertEqual(ret, True)

        ret = send_one_image_message(client, None)
        self.assertEqual(ret, True)

        #clean
        client.close()
        time.sleep(0.1)
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        server.stop_thread()
    def test_open_channel_fail_for_channel_busy(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        #channel busy
        channel_name = "image_busy"
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = CHANNEL_MANAGER.register_one_channel(channel_name)
        self.assertEqual(ret, 0)
        handler = channel_handler.ChannelHandler(channel_name, media_type)
        CHANNEL_MANAGER.create_channel_resource(channel_name, 0, media_type,
                                                handler)
        CHANNEL_MANAGER.create_channel_resource(channel_name, 0, media_type,
                                                handler)
        ret = CHANNEL_MANAGER.is_channel_busy(channel_name)
        self.assertEqual(ret, True)

        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, PB_OPEN_CHANNEL_ERROR_CHANNEL_ALREADY_OPENED)

        #clean
        client.close()
        CHANNEL_MANAGER.unregister_one_channel(channel_name)
        server.stop_thread()
    def test_not_recognize_message_type(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        # test heartbeat
        channel_name = "not_recognize_message_type"
        media_type = PB_CHANNEL_CONTENT_TYPE_VIDEO
        CHANNEL_MANAGER.register_one_channel(channel_name)
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, True)

        ret = send_one_not_recognize_message(client)
        self.assertEqual(ret, True)

        # clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        CHANNEL_MANAGER.unregister_one_channel(channel_name)
        server.stop_thread()
    def test_accept_socket(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        server.stop_thread()
    def test_receive_epoll_out(self, mock_epoll):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)
        epoll = mock_epoll.return_value
        epoll.poll.side_effect = [[[0, select.EPOLLOUT]]]
        time.sleep(0.5)

        server.stop_thread()
    def test_epollhup(self, mock_epoll):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)
        epoll = mock_epoll.return_value
        epoll.poll.side_effect = [[[0, select.EPOLLHUP]]]
        time.sleep(1)

        # clean
        server.stop_thread()
    def test_accept_sockt_error(self, mock_epoll):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)
        epoll = mock_epoll.return_value
        epoll.poll.side_effect = [[[
            server._sock_server.fileno(), select.EPOLLIN
        ]]]
        time.sleep(0.1)

        server.stop_thread()
    def test_read_socket(self, mock_socket_recv):
        """utest"""
        mock_socket_recv.side_effect = mock_recv
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        # test _read_socket
        ret, _ = server._read_socket(client, 10)
        self.assertEqual(ret, False)

        # clean
        client.close()
        server.stop_thread()
    def test_create_presenter_agent_server(self):
        """utest"""
        ret = os.system("netstat  -nl | grep 10086")
        if ret != 0:
            port = 10086
        else:
            port = 30086

        server_address = ("127.0.0.1", port)
        server = FaceDetectionServer(server_address)
        port = server_address[1]
        if port == 10086:
            ret = os.system("netstat  -nap | grep 10086")
        else:
            ret = os.system("netstat  -nap | grep 30086")

        self.assertEqual(ret, 0)
        server.stop_thread()
    def test_read_msg_body_error(self, mock_read_msg_body):
        mock_read_msg_body.return_value = False
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        #test image
        channel_name = "image"
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)

        #clean
        client.close()
        time.sleep(0.1)
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        server.stop_thread()
    def test_receive_message_body_none(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        # test image
        channel_name = "image"
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel_with_size_error(client, channel_name, media_type)
        self.assertEqual(ret, True)
        time.sleep(0.5)

        # clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        server.stop_thread()
    def test_open_channel_with_decode_exception(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        #not exist channel name
        channel_name = "image"
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel_with_encode_message_name_exception(
            client, channel_name, media_type)
        self.assertEqual(ret, True)

        #clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        server.stop_thread()
    def test_read_msg_name(self, mock_socket_recv):
        """utest"""
        mock_socket_recv.side_effect = mock_recv
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        # prepare para
        fd = 1
        conns = {1: client}
        msg_body_len = 10

        # test _read_socket
        ret, _ = server._read_msg_name(fd, conns, msg_body_len)
        self.assertEqual(ret, False)

        # clean
        client.close()
        server.stop_thread()
    def test_open_channel_with_protobuf_exception(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        #not exist channel name
        channel_name = "image"
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel_with_protobuf_error(client)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, PB_OPEN_CHANNEL_ERROR_OTHER)

        #clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        server.stop_thread()
    def test_socket_recv_error(self, mock_read_and_process_msg):
        """utest"""
        mock_read_and_process_msg.side_effect = socket_error

        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        # test image
        channel_name = "image"
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, False)

        # clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        server.stop_thread()
    def test_open_channel_fail_for_media_type(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        #not exist media type
        channel_name = "error_media_type"
        media_type = 100
        CHANNEL_MANAGER.register_one_channel(channel_name)
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, PB_OPEN_CHANNEL_ERROR_OTHER)

        #clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        CHANNEL_MANAGER.unregister_one_channel(channel_name)
        server.stop_thread()
    def test_image_request_error(self):
        """utest"""
        server_address = get_socket_server_addr()
        server = FaceDetectionServer(server_address)

        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)

        # test PRESENT_DATA_ERROR_UNSUPPORTED_FORMAT
        channel_name = "image_error"
        CHANNEL_MANAGER.register_one_channel(channel_name)
        media_type = PB_CHANNEL_CONTENT_TYPE_IMAGE
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, True)

        ret = send_one_image_message(client, 1)
        self.assertEqual(ret, True)

        ret = response_image_request(client)
        self.assertEqual(ret, PB_PRESENT_DATA_ERROR_UNSUPPORTED_FORMAT)

        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)

        # test PRESENT_DATA_ERROR_OTHER
        # have been clean by FaceDetectionServer(), reopen the channel
        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, True)

        for i in CHANNEL_MANAGER.channel_fds:
            CHANNEL_MANAGER.channel_fds[i].handler = None
        ret = send_one_image_message(client, None)
        self.assertEqual(ret, True)

        ret = response_image_request(client)
        self.assertEqual(ret, PB_PRESENT_DATA_ERROR_OTHER)
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)

        # test ParseFromString Exception
        # have been clean by FaceDetectionServer(), reopen the channel
        client = create_sock_client(server_address)
        self.assertNotEqual(client, None)
        ret = open_channel(client, channel_name, media_type)
        self.assertEqual(ret, True)
        ret = response_open_channel(client)
        self.assertEqual(ret, True)
        ret = send_one_image_message_protobuf_format_err(client)
        self.assertEqual(ret, True)

        ret = response_image_request(client)
        self.assertEqual(ret, PB_PRESENT_DATA_ERROR_OTHER)

        # clean
        client.close()
        CHANNEL_MANAGER.clean_channel_resource_by_name(channel_name)
        CHANNEL_MANAGER.unregister_one_channel(channel_name)
        server.stop_thread()