From ab9d13208f4462ff8e676d4293057eaa40cd9d09 Mon Sep 17 00:00:00 2001 From: HowJMay Date: Tue, 14 Jul 2020 21:38:42 +0800 Subject: [PATCH] fix(test): Notify initialization by domain socket Passing signal `SIGUSR1` to parent process to notify parent process that tangle-accelerator has been successfully initialized would cauase tangle-accelerator terminate immediatrly. To fix this, UNIX domain socket is used to notify regression test script that tangle-accelerator has been successfully initialized. --- accelerator/config.c | 34 +++++++++++++++++++++++++++ accelerator/config.h | 17 +++++++++----- accelerator/main.c | 5 ++-- endpoint/unit-test/driver.sh | 24 ++++++++----------- tests/regression/common.sh | 4 +++- tests/regression/router-sanitizer.sh | 27 ++++++++++----------- tests/regression/run-api-with-mqtt.sh | 21 ++++++++++------- tests/regression/run-api.sh | 27 ++++++++++----------- 8 files changed, 96 insertions(+), 63 deletions(-) diff --git a/accelerator/config.c b/accelerator/config.c index 9078f2ca..cdd8a70c 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -9,6 +9,9 @@ #include "config.h" #include #include +#include +#include +#include #include "utils/macros.h" #include "yaml.h" @@ -517,3 +520,34 @@ void ta_core_destroy(ta_core_t* const core) { logger_destroy_json_serializer(); br_logger_release(); } + +#define DOMAIN_SOCKET "/tmp/tangle-accelerator-socket" +#define START_NOTIFICATION "TA-START" +void notification_trigger() { + int connect_fd; + static struct sockaddr_un srv_addr; + + // Create UNIX domain socket + connect_fd = socket(PF_UNIX, SOCK_STREAM, 0); + if (connect_fd < 0) { + ta_log_error("Can't create communication socket.\n"); + goto done; + } + + srv_addr.sun_family = AF_UNIX; + strncpy(srv_addr.sun_path, DOMAIN_SOCKET, strlen(DOMAIN_SOCKET)); + + // Connect to UNIX domain socket server + if (connect(connect_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr)) == -1) { + ta_log_error("Can't connect to UNIX domain socket server.\n"); + goto done; + } + + // Send notification to UNIX domain socket + if (write(connect_fd, START_NOTIFICATION, strlen(START_NOTIFICATION)) == -1) { + ta_log_error("Can't write message to UNIX domain socket server.\n"); + } + +done: + close(connect_fd); +} diff --git a/accelerator/config.h b/accelerator/config.h index ce838e2f..89767f0b 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -130,7 +130,7 @@ typedef struct ta_core_s { } ta_core_t; /** - * Initializes configurations with default values + * @brief Initializes configurations with default values * * @param[in] core Pointer to Tangle-accelerator core configuration structure * @@ -152,7 +152,7 @@ static inline struct option* cli_build_options() { }; /** - * Set configurations with command line inputs + * @brief Set configurations with command line inputs * * @param[in] core Pointer to Tangle-accelerator core configuration structure * @param[in] key CLI command key @@ -165,7 +165,7 @@ static inline struct option* cli_build_options() { status_t cli_core_set(ta_core_t* const core, int key, char* const value); /** - * Initializes configurations with configuration file + * @brief Initializes configurations with configuration file * * @param[in] core Pointer to Tangle-accelerator core configuration structure * @param[in] argc Number of argument of CLI @@ -178,7 +178,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value); status_t ta_core_file_init(ta_core_t* const core, int argc, char** argv); /** - * Initializes configurations with CLI values + * @brief Initializes configurations with CLI values * * @param[in] core Pointer to Tangle-accelerator core configuration structure * @param[in] argc Number of argument of CLI @@ -191,7 +191,7 @@ status_t ta_core_file_init(ta_core_t* const core, int argc, char** argv); status_t ta_core_cli_init(ta_core_t* const core, int argc, char** argv); /** - * Start services after configurations are set + * @brief Start services after configurations are set * * @param[in] core Pointer to Tangle-accelerator core configuration structure * @@ -210,7 +210,7 @@ status_t ta_core_set(ta_core_t* const core); void ta_core_destroy(ta_core_t* const core); /** - * Initializes iota_client_service + * @brief Initializes iota_client_service * * @param[in] service IOTA client service * @param[in] host host of connecting service @@ -224,6 +224,11 @@ void ta_core_destroy(ta_core_t* const core); status_t ta_set_iota_client_service(iota_client_service_t* service, char const* host, uint16_t port, char const* const ca_pem); +/** + * @brief Notify other process with unix domain socket + */ +void notification_trigger(); + #ifdef __cplusplus } #endif diff --git a/accelerator/main.c b/accelerator/main.c index fcfb6470..788c9af9 100644 --- a/accelerator/main.c +++ b/accelerator/main.c @@ -134,9 +134,8 @@ int main(int argc, char* argv[]) { br_logger_init(); } - // Once tangle-accelerator finished initializing, return 'SIGUSR1' to parent process - pid_t pid = getppid(); - kill(pid, SIGUSR1); + // Once tangle-accelerator finished initializing, notify regression test script with unix domain socket + notification_trigger(); /* pause() cause TA to sleep until it catch a signal, * also the return value and errno should be -1 and EINTR on success. diff --git a/endpoint/unit-test/driver.sh b/endpoint/unit-test/driver.sh index a6f16b0a..cb4f3334 100755 --- a/endpoint/unit-test/driver.sh +++ b/endpoint/unit-test/driver.sh @@ -34,22 +34,18 @@ function run_test_suite(){ function start_ta(){ # Create tangle-accelerator for unit-test - trap 'TA_INIT=1' USR1 bazel run accelerator & TA=$! - TA_INIT=0 - - # Wait for tangle-acclerator build finish - while [ "$TA_INIT" -ne 1 ]; do - if ps -p $TA > /dev/null; then - echo "waiting for tangle-accelerator initialization" - sleep 1 - continue - else - # pid does not exist - break - fi - done + # Wait until tangle-accelerator has been initialized + echo "==============Wait for TA starting==============" + while read -r line + do + if [[ "$line" == "TA-START" ]] + then + echo "$line" + fi + done <<< $(nc -U -l $socket | tr '\0' '\n') + echo "==============TA has successfully started==============" } echo "Start unit-test for endpoint" diff --git a/tests/regression/common.sh b/tests/regression/common.sh index 545e1498..42fc0d1e 100644 --- a/tests/regression/common.sh +++ b/tests/regression/common.sh @@ -59,7 +59,9 @@ check_env() { # Parse command line arguments get_cli_args () { - sleep_time=$1 + socket=$1 shift remaining_args=$@ # Get the remaining arguments } + +start_notification="TA-START" diff --git a/tests/regression/router-sanitizer.sh b/tests/regression/router-sanitizer.sh index 28299aa5..4d451422 100644 --- a/tests/regression/router-sanitizer.sh +++ b/tests/regression/router-sanitizer.sh @@ -6,7 +6,7 @@ check_env setup_sanitizer_opts # Get command line arguments -# Current arguments parsed are +# Current arguments parsed are get_cli_args $@ # Install prerequisites @@ -20,24 +20,21 @@ redis-server & for (( i = 0; i < ${#SAN_OPTIONS[@]}; i++ )); do option=${SAN_OPTIONS[${i}]} - trap 'TA_INIT=1' USR1 bazel run accelerator ${option} -c dbg -- --ta_port=${TA_PORT} & TA=$! - - TA_INIT=0 - while [ "$TA_INIT" -ne 1 ]; do - if ps -p $TA > /dev/null; then - echo "waiting for tangle-accelerator initialization" - sleep 1 - continue - else - # pid does not exist - break - fi - done - trap "kill -9 ${TA};" INT # Trap SIGINT from Ctrl-C to stop TA + # Wait until tangle-accelerator has been initialized + echo "==============Wait for TA starting==============" + while read -r line + do + if [[ "$line" == "$start_notification" ]] + then + echo "$line" + fi + done <<< $(nc -U -l $socket | tr '\0' '\n') + echo "==============TA has successfully started==============" + python3 tests/regression/runner.py ${remaining_args} --url localhost:${TA_PORT} rc=$? diff --git a/tests/regression/run-api-with-mqtt.sh b/tests/regression/run-api-with-mqtt.sh index e5b6e447..3c1568a6 100755 --- a/tests/regression/run-api-with-mqtt.sh +++ b/tests/regression/run-api-with-mqtt.sh @@ -6,7 +6,7 @@ check_env setup_build_opts # Get command line arguments -# Current arguments parsed are +# Current arguments parsed are get_cli_args $@ # Install prerequisites @@ -22,18 +22,21 @@ for (( i = 0; i < ${#OPTIONS[@]}; i++ )); do cli_arg=${option} | cut -d '|' -f 1 build_arg=${option} | cut -d '|' -f 2 - trap 'TA_INIT=1' USR1 bazel run accelerator --define mqtt=enable ${build_arg} -- --quiet --ta_port=${TA_PORT} ${cli_arg} & TA=$! - - TA_INIT=0 - while [ "$TA_INIT" -ne 1 ]; do - echo "waiting for tangle-accelerator initialization" - sleep 1 - done - trap "kill -9 ${TA};" INT # Trap SIGINT from Ctrl-C to stop TA + # Wait until tangle-accelerator has been initialized + echo "==============Wait for TA starting==============" + while read -r line + do + if [[ "$line" == "$start_notification" ]] + then + echo "$line" + fi + done <<< $(nc -U -l $socket | tr '\0' '\n') + echo "==============TA has successfully started==============" + python3 tests/regression/runner.py ${remaining_args} --url "localhost" --mqtt rc=$? diff --git a/tests/regression/run-api.sh b/tests/regression/run-api.sh index e66bf86c..7f050c2e 100755 --- a/tests/regression/run-api.sh +++ b/tests/regression/run-api.sh @@ -6,7 +6,7 @@ check_env setup_build_opts # Get command line arguments -# Current arguments parsed are +# Current arguments parsed are get_cli_args $@ # Install prerequisites @@ -22,24 +22,21 @@ for (( i = 0; i < ${#OPTIONS[@]}; i++ )); do cli_arg=$(echo ${option} | cut -d '|' -f 2) build_arg=$(echo ${option} | cut -d '|' -f 1) - trap 'TA_INIT=1' USR1 bazel run accelerator ${build_arg} -- --ta_port=${TA_PORT} ${cli_arg} & TA=$! - - TA_INIT=0 - while [ "$TA_INIT" -ne 1 ]; do - if ps -p $TA > /dev/null; then - echo "waiting for tangle-accelerator initialization" - sleep 1 - continue - else - # pid does not exist - break - fi - done - trap "kill -9 ${TA};" INT # Trap SIGINT from Ctrl-C to stop TA + # Wait until tangle-accelerator has been initialized + echo "==============Wait for TA starting==============" + while read -r line + do + if [[ "$line" == "$start_notification" ]] + then + echo "$line" + fi + done <<< $(nc -U -l $socket | tr '\0' '\n') + echo "==============TA has successfully started==============" + python3 tests/regression/runner.py ${remaining_args} --url localhost:${TA_PORT} rc=$?