Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Queue] set magic in handle #220

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/libnnstreamer-edge/nnstreamer-edge-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct _nns_edge_queue_data_s
*/
typedef struct
{
uint32_t magic;
pthread_mutex_t lock;
pthread_cond_t cond;

Expand Down Expand Up @@ -97,6 +98,7 @@ nns_edge_queue_create (nns_edge_queue_h * handle)

nns_edge_lock_init (q);
nns_edge_cond_init (q);
nns_edge_handle_set_magic (q, NNS_EDGE_MAGIC);
q->leaky = NNS_EDGE_QUEUE_LEAK_NEW;

*handle = q;
Expand All @@ -111,14 +113,15 @@ nns_edge_queue_destroy (nns_edge_queue_h handle)
{
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

/* Stop waiting and clear all data. */
nns_edge_queue_clear (handle);

nns_edge_handle_set_magic (q, NNS_EDGE_MAGIC_DEAD);
nns_edge_cond_destroy (q);
nns_edge_lock_destroy (q);
SAFE_FREE (q);
Expand All @@ -134,8 +137,8 @@ nns_edge_queue_get_length (nns_edge_queue_h handle, unsigned int *length)
{
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

Expand All @@ -160,8 +163,8 @@ nns_edge_queue_set_limit (nns_edge_queue_h handle, unsigned int limit,
{
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

Expand All @@ -184,8 +187,8 @@ nns_edge_queue_push (nns_edge_queue_h handle, void *data, nns_size_t size,
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;
nns_edge_queue_data_s *qdata;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

Expand Down Expand Up @@ -246,8 +249,8 @@ nns_edge_queue_pop (nns_edge_queue_h handle, void **data, nns_size_t * size)
bool popped = false;
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

Expand Down Expand Up @@ -282,8 +285,8 @@ nns_edge_queue_wait_pop (nns_edge_queue_h handle, unsigned int timeout,
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;
bool popped = false;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

Expand Down Expand Up @@ -320,8 +323,8 @@ nns_edge_queue_clear (nns_edge_queue_h handle)
{
nns_edge_queue_s *q = (nns_edge_queue_s *) handle;

if (!q) {
nns_edge_loge ("[Queue] Invalid param, queue is null.");
if (!nns_edge_handle_is_valid (q)) {
nns_edge_loge ("[Queue] Invalid param, queue is invalid.");
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}

Expand Down
77 changes: 77 additions & 0 deletions tests/unittest_nnstreamer-edge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3553,6 +3553,16 @@ TEST_F(edgeQueue, destroyInvalidParam01_n)
EXPECT_EQ (nns_edge_queue_destroy (NULL), NNS_EDGE_ERROR_INVALID_PARAMETER);
}

/**
* @brief Destroy queue - invalid param.
*/
TEST_F(edgeQueue, destroyInvalidParam02_n)
{
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC_DEAD);
EXPECT_EQ (nns_edge_queue_destroy (queue_h), NNS_EDGE_ERROR_INVALID_PARAMETER);
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC);
}

/**
* @brief Get length of queue - invalid param.
*/
Expand All @@ -3571,6 +3581,18 @@ TEST_F(edgeQueue, getLengthInvalidParam02_n)
EXPECT_EQ (nns_edge_queue_get_length (queue_h, NULL), NNS_EDGE_ERROR_INVALID_PARAMETER);
}

/**
* @brief Get length of queue - invalid param.
*/
TEST_F(edgeQueue, getLengthInvalidParam03_n)
{
unsigned int len = 0U;

nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC_DEAD);
EXPECT_EQ (nns_edge_queue_get_length (queue_h, &len), NNS_EDGE_ERROR_INVALID_PARAMETER);
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC);
}

/**
* @brief Set limit of queue.
*/
Expand Down Expand Up @@ -3678,6 +3700,16 @@ TEST_F(edgeQueue, setLimitInvalidParam01_n)
EXPECT_EQ (nns_edge_queue_set_limit (NULL, 5U, NNS_EDGE_QUEUE_LEAK_NEW), NNS_EDGE_ERROR_INVALID_PARAMETER);
}

/**
* @brief Set limit of queue - invalid param.
*/
TEST_F(edgeQueue, setLimitInvalidParam02_n)
{
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC_DEAD);
EXPECT_EQ (nns_edge_queue_set_limit (queue_h, 5U, NNS_EDGE_QUEUE_LEAK_NEW), NNS_EDGE_ERROR_INVALID_PARAMETER);
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC);
}

/**
* @brief Push data into queue - invalid param.
*/
Expand Down Expand Up @@ -3721,6 +3753,25 @@ TEST_F(edgeQueue, pushInvalidParam03_n)
SAFE_FREE (data);
}

/**
* @brief Push data into queue - invalid param.
*/
TEST_F(edgeQueue, pushInvalidParam04_n)
{
void *data;
nns_size_t dsize;

dsize = 5 * sizeof (unsigned int);
data = malloc (dsize);
ASSERT_TRUE (data != NULL);

nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC_DEAD);
EXPECT_EQ (nns_edge_queue_push (queue_h, data, dsize, NULL), NNS_EDGE_ERROR_INVALID_PARAMETER);
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC);

SAFE_FREE (data);
}

/**
* @brief Pop data from queue - invalid param.
*/
Expand Down Expand Up @@ -3752,6 +3803,19 @@ TEST_F(edgeQueue, popInvalidParam03_n)
EXPECT_EQ (nns_edge_queue_pop (queue_h, &data, NULL), NNS_EDGE_ERROR_INVALID_PARAMETER);
}

/**
* @brief Pop data from queue - invalid param.
*/
TEST_F(edgeQueue, popInvalidParam04_n)
{
void *data;
nns_size_t size;

nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC_DEAD);
EXPECT_EQ (nns_edge_queue_pop (queue_h, &data, &size), NNS_EDGE_ERROR_INVALID_PARAMETER);
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC);
}

/**
* @brief Wait and pop data from queue, timed out.
*/
Expand Down Expand Up @@ -3794,6 +3858,19 @@ TEST_F(edgeQueue, waitPopInvalidParam03_n)
EXPECT_EQ (nns_edge_queue_wait_pop (queue_h, 10U, &data, NULL), NNS_EDGE_ERROR_INVALID_PARAMETER);
}

/**
* @brief Wait and pop data from queue - invalid param.
*/
TEST_F(edgeQueue, waitPopInvalidParam04_n)
{
void *data;
nns_size_t size;

nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC_DEAD);
EXPECT_EQ (nns_edge_queue_wait_pop (queue_h, 10U, &data, &size), NNS_EDGE_ERROR_INVALID_PARAMETER);
nns_edge_handle_set_magic (queue_h, NNS_EDGE_MAGIC);
}

/**
* @brief Util to get the version.
*/
Expand Down
Loading