Skip to content

Commit

Permalink
Add tests iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jan 28, 2025
1 parent 9015085 commit 1ac5d0b
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions tests/z_api_matching_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
#undef NDEBUG
#include <assert.h>

static const char* PUB_EXPR = "zenoh-pico/matching/test/val";
static const char* SUB_EXPR = "zenoh-pico/matching/test/*";
static const char* SUB_EXPR_WRONG = "zenoh-pico/matching/test_wrong/*";

static const char* QUERIABLE_EXPR = "zenoh-pico/matching/query_test/val";
static const char* QUERY_EXPR = "zenoh-pico/matching/query_test/*";

static unsigned long DEFAULT_TIMEOUT_S = 10;
static int SUBSCRIBER_TESTS_COUNT = 3;

typedef enum { NONE, MATCH, UNMATCH, DROP } context_state_t;

typedef struct context_t {
Expand All @@ -44,7 +54,7 @@ static void _context_drop(context_t* c) {
z_mutex_drop(z_mutex_move(&c->m));
}

static void _context_wait(context_t* c, context_state_t state, int timeout_s) {
static void _context_wait(context_t* c, context_state_t state, unsigned long timeout_s) {
z_mutex_lock(z_mutex_loan_mut(&c->m));
if (c->state != state) {
printf("Waiting for state %d...\n", state);
Expand Down Expand Up @@ -90,13 +100,6 @@ static void _context_notify(context_t* c, context_state_t state) {
} \
}

static const char* pub_expr = "zenoh-pico/matching/test/val";
static const char* sub_expr = "zenoh-pico/matching/test/*";
static const char* sub_expr_wrong = "zenoh-pico/matching/test_wrong/*";

static const char* queriable_expr = "zenoh-pico/matching/query_test/val";
static const char* query_expr = "zenoh-pico/matching/query_test/*";

void on_receive(const z_matching_status_t* s, void* context) {
context_t* c = (context_t*)context;
_context_notify(c, s->matching ? MATCH : UNMATCH);
Expand All @@ -118,8 +121,8 @@ void test_matching_publisher_sub(bool background) {
z_config_default(&c1);
z_config_default(&c2);
z_view_keyexpr_t k_sub, k_pub;
z_view_keyexpr_from_str(&k_sub, sub_expr);
z_view_keyexpr_from_str(&k_pub, pub_expr);
z_view_keyexpr_from_str(&k_sub, SUB_EXPR);
z_view_keyexpr_from_str(&k_pub, PUB_EXPR);

assert_ok(z_open(&s1, z_config_move(&c1), NULL));
assert_ok(z_open(&s2, z_config_move(&c2), NULL));
Expand All @@ -144,26 +147,27 @@ void test_matching_publisher_sub(bool background) {
z_closure_matching_status_move(&closure)));
}

z_owned_subscriber_t sub;
z_owned_closure_sample_t callback;
z_closure_sample(&callback, NULL, NULL, NULL);
assert_ok(z_declare_subscriber(z_session_loan(&s2), &sub, z_view_keyexpr_loan(&k_sub),
z_closure_sample_move(&callback), NULL));
for (int i = 0; i != SUBSCRIBER_TESTS_COUNT; i++) {
z_owned_subscriber_t sub;
z_owned_closure_sample_t callback;
z_closure_sample(&callback, NULL, NULL, NULL);
assert_ok(z_declare_subscriber(z_session_loan(&s2), &sub, z_view_keyexpr_loan(&k_sub),
z_closure_sample_move(&callback), NULL));

_context_wait(&context, MATCH, 10);
_context_wait(&context, MATCH, DEFAULT_TIMEOUT_S);

z_subscriber_drop(z_subscriber_move(&sub));
z_subscriber_drop(z_subscriber_move(&sub));

_context_wait(&context, UNMATCH, 10);
_context_wait(&context, UNMATCH, DEFAULT_TIMEOUT_S);
}

z_publisher_drop(z_publisher_move(&pub));

_context_wait(&context, DROP, 10);
_context_wait(&context, DROP, DEFAULT_TIMEOUT_S);

if (!background) {
z_matching_listener_drop(z_matching_listener_move(&matching_listener));
}

assert_ok(zp_stop_read_task(z_loan_mut(s1)));
assert_ok(zp_stop_read_task(z_loan_mut(s2)));
assert_ok(zp_stop_lease_task(z_loan_mut(s1)));
Expand All @@ -186,8 +190,8 @@ void test_matching_querier_sub(bool background) {
z_config_default(&c1);
z_config_default(&c2);
z_view_keyexpr_t k_queryable, k_querier;
z_view_keyexpr_from_str(&k_queryable, queriable_expr);
z_view_keyexpr_from_str(&k_querier, query_expr);
z_view_keyexpr_from_str(&k_queryable, QUERIABLE_EXPR);
z_view_keyexpr_from_str(&k_querier, QUERY_EXPR);

assert_ok(z_open(&s1, z_config_move(&c1), NULL));
assert_ok(z_open(&s2, z_config_move(&c2), NULL));
Expand All @@ -212,21 +216,23 @@ void test_matching_querier_sub(bool background) {
z_closure_matching_status_move(&closure)));
}

z_owned_queryable_t queryable;
z_owned_closure_query_t callback;
z_closure_query(&callback, NULL, NULL, NULL);
assert_ok(z_declare_queryable(z_session_loan(&s2), &queryable, z_view_keyexpr_loan(&k_queryable),
z_closure_query_move(&callback), NULL));
for (int i = 0; i != SUBSCRIBER_TESTS_COUNT; i++) {
z_owned_queryable_t queryable;
z_owned_closure_query_t callback;
z_closure_query(&callback, NULL, NULL, NULL);
assert_ok(z_declare_queryable(z_session_loan(&s2), &queryable, z_view_keyexpr_loan(&k_queryable),
z_closure_query_move(&callback), NULL));

_context_wait(&context, MATCH, 10);
_context_wait(&context, MATCH, DEFAULT_TIMEOUT_S);

z_queryable_drop(z_queryable_move(&queryable));
z_queryable_drop(z_queryable_move(&queryable));

_context_wait(&context, UNMATCH, 10);
_context_wait(&context, UNMATCH, DEFAULT_TIMEOUT_S);
}

z_querier_drop(z_querier_move(&querier));

_context_wait(&context, DROP, 10);
_context_wait(&context, DROP, DEFAULT_TIMEOUT_S);

if (!background) {
z_matching_listener_drop(z_matching_listener_move(&matching_listener));
Expand All @@ -247,7 +253,7 @@ static void _check_status(z_owned_publisher_t* pub, bool expected) {
z_matching_status_t status;
status.matching = !expected;
z_clock_t clock = z_clock_now();
while (status.matching != expected && z_clock_elapsed_s(&clock) < 10) {
while (status.matching != expected && z_clock_elapsed_s(&clock) < DEFAULT_TIMEOUT_S) {
assert_ok(z_publisher_get_matching_status(z_publisher_loan(pub), &status));
z_sleep_ms(100);
}
Expand All @@ -263,9 +269,9 @@ void test_matching_publisher_get(void) {
z_config_default(&c1);
z_config_default(&c2);
z_view_keyexpr_t k_sub, k_pub, k_sub_wrong;
z_view_keyexpr_from_str(&k_sub, sub_expr);
z_view_keyexpr_from_str(&k_pub, pub_expr);
z_view_keyexpr_from_str(&k_sub_wrong, sub_expr_wrong);
z_view_keyexpr_from_str(&k_sub, SUB_EXPR);
z_view_keyexpr_from_str(&k_pub, PUB_EXPR);
z_view_keyexpr_from_str(&k_sub_wrong, SUB_EXPR_WRONG);

assert_ok(z_open(&s1, z_config_move(&c1), NULL));
assert_ok(z_open(&s2, z_config_move(&c2), NULL));
Expand Down

0 comments on commit 1ac5d0b

Please sign in to comment.