From 4e85b1e5c06c27d12a9e7a7f3811226fdff28741 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Mon, 9 Sep 2024 18:33:29 +0200 Subject: [PATCH] Add tests and _z_liveliness_token_t dummy implementation --- CMakeLists.txt | 3 + include/zenoh-pico/api/liveliness.h | 6 +- src/api/liveliness.c | 40 +++++-- tests/z_api_liveliness_test.c | 159 ++++++++++++++++++++++++++++ 4 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 tests/z_api_liveliness_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 877eb404d..8733a535c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -415,6 +415,7 @@ if(UNIX OR MSVC) add_executable(z_api_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_api_bytes_test.c) add_executable(z_api_encoding_test ${PROJECT_SOURCE_DIR}/tests/z_api_encoding_test.c) add_executable(z_api_config_test ${PROJECT_SOURCE_DIR}/tests/z_api_config_test.c) + add_executable(z_api_liveliness_test ${PROJECT_SOURCE_DIR}/tests/z_api_liveliness_test.c) add_executable(z_refcount_test ${PROJECT_SOURCE_DIR}/tests/z_refcount_test.c) target_link_libraries(z_data_struct_test ${Libname}) @@ -434,6 +435,7 @@ if(UNIX OR MSVC) target_link_libraries(z_api_bytes_test ${Libname}) target_link_libraries(z_api_encoding_test ${Libname}) target_link_libraries(z_api_config_test ${Libname}) + target_link_libraries(z_api_liveliness_test ${Libname}) target_link_libraries(z_refcount_test ${Libname}) configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY) @@ -457,6 +459,7 @@ if(UNIX OR MSVC) add_test(z_api_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_bytes_test) add_test(z_api_encoding_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_encoding_test) add_test(z_api_config_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_config_test) + add_test(z_api_liveliness_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_liveliness_test) add_test(z_refcount_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_refcount_test) endif() diff --git a/include/zenoh-pico/api/liveliness.h b/include/zenoh-pico/api/liveliness.h index b2b4595c6..2a259cc65 100644 --- a/include/zenoh-pico/api/liveliness.h +++ b/include/zenoh-pico/api/liveliness.h @@ -26,6 +26,7 @@ typedef struct { } _z_liveliness_token_t; _Z_OWNED_TYPE_VALUE(_z_liveliness_token_t, liveliness_token) +_Z_OWNED_FUNCTIONS_DEF(liveliness_token) /** * The options for `z_liveliness_declare_token()`. @@ -103,11 +104,6 @@ int8_t z_liveliness_get_options_default(z_liveliness_get_options_t *options); int8_t z_liveliness_get(const z_loaned_session_t *session, const z_loaned_keyexpr_t *key_expr, z_moved_closure_reply_t *callback, z_liveliness_get_options_t *options); -/** - * Undeclares liveliness token, frees memory and resets it to a gravestone state. - */ -int8_t z_liveliness_token_drop(z_moved_liveliness_token_t *token); - /** * Destroys a liveliness token, notifying subscribers of its destruction. */ diff --git a/src/api/liveliness.c b/src/api/liveliness.c index c41efb216..95c4f6799 100644 --- a/src/api/liveliness.c +++ b/src/api/liveliness.c @@ -13,8 +13,42 @@ // #include "zenoh-pico/api/liveliness.h" + #include "zenoh-pico/utils/result.h" +_Bool _z_liveliness_token_check(const _z_liveliness_token_t *token) { + // TODO(sashacmc): Implement + (void)token; + return true; +} + +_z_liveliness_token_t _z_liveliness_token_null(void) { + // TODO(sashacmc): Implement + _z_liveliness_token_t s = {0}; + return s; +} + +void _z_liveliness_token_move(_z_liveliness_token_t *dst, _z_liveliness_token_t *src) { + // TODO(sashacmc): Implement + (void)dst; + (void)src; +} + +int8_t _z_liveliness_token_copy(_z_liveliness_token_t *dst, const _z_liveliness_token_t *src) { + // TODO(sashacmc): Implement + (void)dst; + (void)src; + return _Z_RES_OK; +} + +void _z_liveliness_token_clear(_z_liveliness_token_t *token) { + // TODO(sashacmc): Implement + (void)token; +} + +_Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_liveliness_token_t, liveliness_token, _z_liveliness_token_check, + _z_liveliness_token_null, _z_liveliness_token_copy, _z_liveliness_token_clear) + int8_t z_liveliness_subscriber_options_default(z_liveliness_subscriber_options_t *options) { options->__dummy = 0; return _Z_RES_OK; @@ -63,12 +97,6 @@ int8_t z_liveliness_get(const z_loaned_session_t *session, const z_loaned_keyexp return _Z_RES_OK; } -int8_t z_liveliness_token_drop(z_moved_liveliness_token_t *token) { - // TODO(sashacmc): Implement - (void)token; - return _Z_RES_OK; -} - int8_t z_liveliness_undeclare_token(z_moved_liveliness_token_t *token) { // TODO(sashacmc): Implement (void)token; diff --git a/tests/z_api_liveliness_test.c b/tests/z_api_liveliness_test.c new file mode 100644 index 000000000..4d124bac8 --- /dev/null +++ b/tests/z_api_liveliness_test.c @@ -0,0 +1,159 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, + +#include +#include +#include +#include +#include +#include + +#include "zenoh-pico/api/handlers.h" +#include "zenoh-pico/api/liveliness.h" +#include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/api/types.h" + +#undef NDEBUG +#include +typedef struct context_t { + bool token1_put; + bool token2_put; + bool token1_drop; + bool token2_drop; +} context_t; + +const char* token1_expr = "zenoh-pico/liveliness/test/1"; +const char* token2_expr = "zenoh-pico/liveliness/test/2"; + +void on_receive(const z_loaned_sample_t* s, void* context) { + context_t* c = (context_t*)context; + const z_loaned_keyexpr_t* k = z_sample_keyexpr(s); + z_view_string_t ks; + z_keyexpr_as_view_string(k, &ks); + + if (z_sample_kind(s) == Z_SAMPLE_KIND_PUT) { + if (strncmp(token1_expr, z_string_data(z_view_string_loan(&ks)), z_string_len(z_view_string_loan(&ks))) == 0) { + c->token1_put = true; + } else if (strncmp(token2_expr, z_string_data(z_view_string_loan(&ks)), + z_string_len(z_view_string_loan(&ks))) == 0) { + c->token2_put = true; + } + } else if (z_sample_kind(s) == Z_SAMPLE_KIND_DELETE) { + if (strncmp(token1_expr, z_string_data(z_view_string_loan(&ks)), z_string_len(z_view_string_loan(&ks))) == 0) { + c->token1_drop = true; + } else if (strncmp(token2_expr, z_string_data(z_view_string_loan(&ks)), + z_string_len(z_view_string_loan(&ks))) == 0) { + c->token2_drop = true; + } + } +} + +void test_liveliness_sub(void) { + const char* expr = "zenoh-pico/liveliness/test/*"; + + z_owned_session_t s1, s2; + z_owned_config_t c1, c2; + z_config_default(&c1); + z_config_default(&c2); + z_view_keyexpr_t k, k1, k2; + z_view_keyexpr_from_str(&k, expr); + z_view_keyexpr_from_str(&k1, token1_expr); + z_view_keyexpr_from_str(&k2, token2_expr); + + z_open(&s1, z_config_move(&c1)); + z_open(&s2, z_config_move(&c2)); + + z_owned_closure_sample_t closure; + context_t context = {false, false, false, false}; + z_closure_sample(&closure, on_receive, NULL, (void*)(&context)); + + z_owned_subscriber_t sub; + z_liveliness_declare_subscriber(&sub, z_session_loan(&s2), z_view_keyexpr_loan(&k), z_closure_sample_move(&closure), + NULL); + + z_sleep_s(1); + z_owned_liveliness_token_t t1, t2; + z_liveliness_declare_token(&t1, z_session_loan(&s1), z_view_keyexpr_loan(&k1), NULL); + z_liveliness_declare_token(&t2, z_session_loan(&s1), z_view_keyexpr_loan(&k2), NULL); + + z_sleep_s(1); + + assert(context.token1_put); + assert(context.token2_put); + + z_liveliness_undeclare_token(z_liveliness_token_move(&t1)); + z_sleep_s(1); + + assert(context.token1_drop); + assert(!context.token2_drop); + + z_liveliness_undeclare_token(z_liveliness_token_move(&t2)); + z_sleep_s(1); + assert(context.token2_drop); +} + +void test_liveliness_get(void) { + const char* expr = "zenoh-pico/liveliness/test/*"; + + z_owned_session_t s1, s2; + z_owned_config_t c1, c2; + z_config_default(&c1); + z_config_default(&c2); + z_view_keyexpr_t k, k1; + z_view_keyexpr_from_str(&k, expr); + z_view_keyexpr_from_str(&k1, token1_expr); + + z_open(&s1, z_config_move(&c1)); + z_open(&s2, z_config_move(&c2)); + + z_sleep_s(1); + z_owned_liveliness_token_t t1; + z_liveliness_declare_token(&t1, z_session_loan(&s1), z_view_keyexpr_loan(&k1), NULL); + z_sleep_s(1); + + z_owned_fifo_handler_reply_t handler; + z_owned_closure_reply_t cb; + z_fifo_channel_reply_new(&cb, &handler, 3); + + z_liveliness_get(z_session_loan(&s2), z_view_keyexpr_loan(&k), z_closure_reply_move(&cb), NULL); + z_owned_reply_t reply; + assert(z_fifo_handler_reply_recv(z_fifo_handler_reply_loan(&handler), &reply) == Z_OK); + assert(z_reply_is_ok(z_reply_loan(&reply))); + const z_loaned_keyexpr_t* reply_keyexpr = z_sample_keyexpr(z_reply_ok(z_reply_loan(&reply))); + z_view_string_t reply_keyexpr_s; + z_keyexpr_as_view_string(reply_keyexpr, &reply_keyexpr_s); + assert(strncmp(token1_expr, z_string_data(z_view_string_loan(&reply_keyexpr_s)), + z_string_len(z_view_string_loan(&reply_keyexpr_s))) == 0); + + z_reply_drop(z_reply_move(&reply)); + assert(z_fifo_handler_reply_recv(z_fifo_handler_reply_loan(&handler), &reply) == Z_CHANNEL_DISCONNECTED); + + z_liveliness_token_drop(z_liveliness_token_move(&t1)); + z_fifo_handler_reply_drop(z_fifo_handler_reply_move(&handler)); + z_sleep_s(1); + z_fifo_channel_reply_new(&cb, &handler, 3); + + z_liveliness_get(z_session_loan(&s2), z_view_keyexpr_loan(&k), z_closure_reply_move(&cb), NULL); + assert(z_fifo_handler_reply_recv(z_fifo_handler_reply_loan(&handler), &reply) == Z_CHANNEL_DISCONNECTED); + + z_fifo_handler_reply_drop(z_fifo_handler_reply_move(&handler)); + z_session_drop(z_session_move(&s1)); + z_session_drop(z_session_move(&s2)); +} + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + test_liveliness_sub(); + test_liveliness_get(); +}