Skip to content

Commit

Permalink
[TEST] Add unit test for custom connection
Browse files Browse the repository at this point in the history
Add unit test for custom connection
*linnnstreamer-edge-cusom.so is not real implementation just for test

Signed-off-by: gichan2-jang <[email protected]>
  • Loading branch information
gichan-jang committed Aug 20, 2024
1 parent 017173f commit b3d23eb
Show file tree
Hide file tree
Showing 6 changed files with 478 additions and 47 deletions.
2 changes: 1 addition & 1 deletion include/nnstreamer-edge-custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct _NnsEdgeCustomDef
int (*nns_edge_custom_send_data) (void *priv, nns_edge_data_h data_h);
int (*nns_edge_custom_set_option) (void *priv, const char *key, const char *value);
char *(*nns_edge_custom_get_option) (void *priv, const char *key);
} NnsEdgeCustomDef;
} nns_edge_custom_s;

void* nns_edge_custom_get_handle ();

Expand Down
3 changes: 3 additions & 0 deletions packaging/nnstreamer-edge.spec
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ popd

%if 0%{?unit_test}
LD_LIBRARY_PATH=./src bash %{test_script} ./tests/unittest_nnstreamer-edge
LD_LIBRARY_PATH=./src:./tests bash %{test_script} ./tests/unittest_nnstreamer-edge-custom

%if 0%{?mqtt_support}
LD_LIBRARY_PATH=./src bash %{test_script} ./tests/unittest_nnstreamer-edge-mqtt
Expand Down Expand Up @@ -181,6 +182,8 @@ rm -rf %{buildroot}
%manifest nnstreamer-edge.manifest
%defattr(-,root,root,-)
%{_bindir}/unittest_nnstreamer-edge
%{_bindir}/unittest_nnstreamer-edge-custom
%{_libdir}/libnnstreamer-edge-custom.so*

%if 0%{?mqtt_support}
%{_bindir}/unittest_nnstreamer-edge-mqtt
Expand Down
152 changes: 106 additions & 46 deletions src/libnnstreamer-edge/nnstreamer-edge-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ typedef struct

/* MQTT or AITT handle */
void *broker_h;
void *custom_h;
void *custom_lib;
void *custom_instance;
} nns_edge_handle_s;

/**
Expand Down Expand Up @@ -924,10 +925,10 @@ _nns_edge_send_thread (void *thread_data)
break;
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
{
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
ret = custom_h->nns_edge_custom_send_data (eh->broker_h, data_h);
if (NNS_EDGE_ERROR_NONE != ret)
nns_edge_loge ("Failed to send data via MCF connection.");
nns_edge_loge ("Failed to send data via nns edge custom connection.");
break;
}
default:
Expand Down Expand Up @@ -1297,7 +1298,8 @@ _nns_edge_create_handle (const char *id, nns_edge_node_type_e node_type,
eh->sending = false;
eh->listener_fd = -1;
eh->caps_str = nns_edge_strdup ("");
eh->custom_h = NULL;
eh->custom_lib = NULL;
eh->custom_instance = NULL;

ret = nns_edge_metadata_create (&eh->metadata);
if (ret != NNS_EDGE_ERROR_NONE) {
Expand All @@ -1319,6 +1321,31 @@ _nns_edge_create_handle (const char *id, nns_edge_node_type_e node_type,
return ret;
}

/**
* @brief Load custom lib and get edge custom instance.
*/
static int
_nns_edge_load_custom_library (nns_edge_h edge_h, const char *lib_path) {
nns_edge_handle_s *eh = (nns_edge_handle_s *) edge_h;

eh->custom_lib = dlopen (lib_path, RTLD_LAZY);
if (NULL == eh->custom_lib) {
nns_edge_loge ("Failed to open custom lib: %s", dlerror());
return NNS_EDGE_ERROR_UNKNOWN;
}

void *(*getCustomHandle)() = (void *(*)()) dlsym (eh->custom_lib, "nns_edge_custom_get_handle");
if (!getCustomHandle) {
nns_edge_loge ("Failed to find nns_edge_custom_get_handle: %s", dlerror());
dlclose (eh->custom_lib);
eh->custom_lib = NULL;
return NNS_EDGE_ERROR_UNKNOWN;
}

eh->custom_instance = (nns_edge_custom_s *) getCustomHandle ();
return 0;
}

/**
* @brief Create edge custom handle.
*/
Expand All @@ -1328,8 +1355,7 @@ nns_edge_custom_create_handle (const char *id, const char *lib_path,
{
int ret = NNS_EDGE_ERROR_NONE;
nns_edge_handle_s *eh;
void *custom_handle;
NnsEdgeCustomDef *custom_h;
nns_edge_custom_s *custom_h;

if (node_type < 0 || node_type >= NNS_EDGE_NODE_TYPE_UNKNOWN) {
nns_edge_loge ("Invalid param, set exact node type.");
Expand All @@ -1354,22 +1380,13 @@ nns_edge_custom_create_handle (const char *id, const char *lib_path,
eh = (nns_edge_handle_s *) * edge_h;
eh->connect_type = NNS_EDGE_CONNECT_TYPE_CUSTOM;

custom_handle = dlopen (lib_path, RTLD_LAZY);
if (custom_handle) {
void *(*getCustomHandle) () =
(void *(*)()) dlsym (custom_handle, "nns_edge_custom_get_handle");
if (!getCustomHandle) {
nns_edge_loge ("Failed to find nns_edge_custom_get_handle: %s",
dlerror ());
return NNS_EDGE_ERROR_UNKNOWN;
}
eh->custom_h = getCustomHandle ();
} else {
nns_edge_loge ("Failed to open custom handle: %s]\n", dlerror ());
return NNS_EDGE_ERROR_INVALID_PARAMETER;;
ret = _nns_edge_load_custom_library (*edge_h, lib_path);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to load custom lib. Please check the custom lib path or permission.");
return ret;
}

custom_h = (NnsEdgeCustomDef *) eh->custom_h;
custom_h = (nns_edge_custom_s *) eh->custom_instance;
ret = custom_h->nns_edge_custom_create (&eh->broker_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to create custom connection handle.");
Expand Down Expand Up @@ -1431,6 +1448,33 @@ nns_edge_create_handle (const char *id, nns_edge_connect_type_e connect_type,
return ret;
}

/**
* @brief Start the nnstreamer edge custom.
*/
static int
_nns_edge_custom_start (nns_edge_handle_s * eh)
{
int ret = NNS_EDGE_ERROR_NONE;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;

ret = custom_h->nns_edge_custom_start (eh->broker_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to start edge custom connection");
return ret;
}

ret = custom_h->nns_edge_custom_set_event_cb (eh->broker_h, eh->event_cb,
eh->user_data);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to set event callback to custom connection.");
return ret;
}

ret = _nns_edge_create_send_thread (eh);

return ret;
}

/**
* @brief Start the nnstreamer edge.
*/
Expand All @@ -1454,18 +1498,10 @@ nns_edge_start (nns_edge_h edge_h)
nns_edge_lock (eh);

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
ret = custom_h->nns_edge_custom_start (eh->broker_h);
ret = _nns_edge_custom_start (edge_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to start edge custom connection");
}
ret = custom_h->nns_edge_custom_set_event_cb (eh->broker_h, eh->event_cb,
eh->user_data);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to set event callback to custom connection.");
goto done;
}
ret = _nns_edge_create_send_thread (eh);
goto done;
}

Expand Down Expand Up @@ -1556,6 +1592,21 @@ nns_edge_start (nns_edge_h edge_h)
return ret;
}

/**
* @brief Stop the nnstreamer edge custom connection.
*/
static int
_nns_edge_custom_stop (nns_edge_handle_s *eh) {
int ret = NNS_EDGE_ERROR_NONE;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
ret = custom_h->nns_edge_custom_stop (eh->broker_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to stop nns edge custom connection");
}

return ret;
}

/**
* @brief Stop the nnstreamer edge.
*/
Expand All @@ -1577,20 +1628,35 @@ nns_edge_stop (nns_edge_h edge_h)
}

nns_edge_lock (eh);
if (!eh->is_started) {
nns_edge_logi ("Edge is not started yet. Nothing to stop.");
goto done;
}

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
ret = custom_h->nns_edge_custom_stop (eh->broker_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to stop MCF");
}
ret = _nns_edge_custom_stop (eh);
}

eh->is_started = FALSE;

done:
nns_edge_unlock (eh);
return ret;
}

static void
_nns_edge_custom_release (nns_edge_handle_s * eh)
{
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
int ret = custom_h->nns_edge_custom_close (eh->broker_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to stop nns edge custom connection");
}
dlclose (eh->custom_lib);
eh->custom_instance = NULL;
eh->custom_lib = NULL;
}

/**
* @brief Release the given handle.
*/
Expand All @@ -1610,8 +1676,7 @@ nns_edge_release_handle (nns_edge_h edge_h)
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

if (eh->is_started)
nns_edge_stop (eh);
nns_edge_stop (eh);

nns_edge_lock (eh);

Expand All @@ -1629,12 +1694,7 @@ nns_edge_release_handle (nns_edge_h edge_h)
break;
case NNS_EDGE_CONNECT_TYPE_CUSTOM:
{
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
int ret = custom_h->nns_edge_custom_close (eh->broker_h);
if (NNS_EDGE_ERROR_NONE != ret) {
nns_edge_loge ("Failed to stop MCF");
}
dlclose (eh->custom_h);
_nns_edge_custom_release (eh);
break;
}
default:
Expand Down Expand Up @@ -1879,7 +1939,7 @@ nns_edge_connect (nns_edge_h edge_h, const char *dest_host, int dest_port)
goto done;
}
} else if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
ret = custom_h->nns_edge_custom_connect (eh->broker_h);
if (ret != NNS_EDGE_ERROR_NONE) {
nns_edge_loge ("Failed to connect to custom connection.");
Expand Down Expand Up @@ -1952,7 +2012,7 @@ nns_edge_is_connected (nns_edge_h edge_h)
return NNS_EDGE_ERROR_NONE;

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
return custom_h->nns_edge_custom_is_connected (eh->broker_h);
}

Expand Down Expand Up @@ -2127,7 +2187,7 @@ nns_edge_set_info (nns_edge_h edge_h, const char *key, const char *value)
}

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
ret = custom_h->nns_edge_custom_set_option (eh->broker_h, key, value);
}

Expand Down Expand Up @@ -2195,7 +2255,7 @@ nns_edge_get_info (nns_edge_h edge_h, const char *key, char **value)
}

if (NNS_EDGE_CONNECT_TYPE_CUSTOM == eh->connect_type) {
NnsEdgeCustomDef *custom_h = (NnsEdgeCustomDef *) eh->custom_h;
nns_edge_custom_s *custom_h = (nns_edge_custom_s *) eh->custom_instance;
*value = custom_h->nns_edge_custom_get_option (eh->broker_h, key);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ TARGET_INCLUDE_DIRECTORIES(unittest_nnstreamer-edge PRIVATE ${EDGE_REQUIRE_PKGS_
TARGET_LINK_LIBRARIES(unittest_nnstreamer-edge ${TEST_REQUIRE_PKGS_LDFLAGS} ${NNS_EDGE_LIB_NAME})
INSTALL (TARGETS unittest_nnstreamer-edge DESTINATION ${BIN_INSTALL_DIR})

# Custom connection lib
SET(NNS_EDGE_CUSTOM_LIB_NAME nnstreamer-edge-custom)
SET(NNS_EDGE_CUSTOM_SRCS ${NNS_EDGE_SRCS} nnstreamer-edge-custom.c)
ADD_LIBRARY(${NNS_EDGE_CUSTOM_LIB_NAME} SHARED ${NNS_EDGE_CUSTOM_SRCS})
SET_TARGET_PROPERTIES(${NNS_EDGE_CUSTOM_LIB_NAME} PROPERTIES VERSION ${SO_VERSION})
TARGET_INCLUDE_DIRECTORIES(${NNS_EDGE_CUSTOM_LIB_NAME} PRIVATE ${EDGE_REQUIRE_PKGS_INCLUDE_DIRS} ${INCLUDE_DIR} ${NNS_EDGE_SRC_DIR})
TARGET_LINK_LIBRARIES(${NNS_EDGE_CUSTOM_LIB_NAME} ${TEST_REQUIRE_PKGS_LDFLAGS} ${NNS_EDGE_LIB_NAME})
INSTALL (TARGETS ${NNS_EDGE_CUSTOM_LIB_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})

ADD_EXECUTABLE(unittest_nnstreamer-edge-custom unittest_nnstreamer-edge-custom.cc)
TARGET_INCLUDE_DIRECTORIES(unittest_nnstreamer-edge-custom PRIVATE ${EDGE_REQUIRE_PKGS_INCLUDE_DIRS} ${INCLUDE_DIR} ${NNS_EDGE_SRC_DIR})
TARGET_LINK_LIBRARIES(unittest_nnstreamer-edge-custom ${TEST_REQUIRE_PKGS_LDFLAGS} ${NNS_EDGE_LIB_NAME} ${NNS_EDGE_CUSTOM_LIB_NAME})
INSTALL (TARGETS unittest_nnstreamer-edge-custom DESTINATION ${BIN_INSTALL_DIR})

# AITT test
IF(AITT_SUPPORT)
ADD_EXECUTABLE(unittest_nnstreamer-edge-aitt unittest_nnstreamer-edge-aitt.cc)
Expand Down
Loading

0 comments on commit b3d23eb

Please sign in to comment.