From 4e35b6caccef6c6348d22fd6798d6bd48402bcba Mon Sep 17 00:00:00 2001 From: OSSRS-AI Date: Sun, 19 Oct 2025 22:37:43 -0400 Subject: [PATCH] AI: Add utest to cover signal manager --- trunk/configure | 12 +- trunk/src/app/srs_app_server.cpp | 2 +- trunk/src/app/srs_app_server.hpp | 4 +- trunk/src/utest/srs_utest_ai17.cpp | 2 +- trunk/src/utest/srs_utest_ai18.cpp | 2 +- trunk/src/utest/srs_utest_ai20.cpp | 163 ++++++++++++++++++ trunk/src/utest/srs_utest_ai20.hpp | 38 ++++ ...srs_utest_app12.cpp => srs_utest_ai21.cpp} | 2 +- ...srs_utest_app12.hpp => srs_utest_ai21.hpp} | 6 +- ...srs_utest_app13.cpp => srs_utest_ai22.cpp} | 2 +- ...srs_utest_app13.hpp => srs_utest_ai22.hpp} | 8 +- ...srs_utest_app14.cpp => srs_utest_ai23.cpp} | 2 +- ...srs_utest_app14.hpp => srs_utest_ai23.hpp} | 6 +- trunk/src/utest/srs_utest_mock.cpp | 2 +- trunk/src/utest/srs_utest_rtmp_conn.cpp | 2 +- 15 files changed, 227 insertions(+), 26 deletions(-) create mode 100644 trunk/src/utest/srs_utest_ai20.cpp create mode 100644 trunk/src/utest/srs_utest_ai20.hpp rename trunk/src/utest/{srs_utest_app12.cpp => srs_utest_ai21.cpp} (99%) rename trunk/src/utest/{srs_utest_app12.hpp => srs_utest_ai21.hpp} (98%) rename trunk/src/utest/{srs_utest_app13.cpp => srs_utest_ai22.cpp} (99%) rename trunk/src/utest/{srs_utest_app13.hpp => srs_utest_ai22.hpp} (99%) rename trunk/src/utest/{srs_utest_app14.cpp => srs_utest_ai23.cpp} (99%) rename trunk/src/utest/{srs_utest_app14.hpp => srs_utest_ai23.hpp} (99%) diff --git a/trunk/configure b/trunk/configure index 7a9f8c27a..780a795db 100755 --- a/trunk/configure +++ b/trunk/configure @@ -383,15 +383,15 @@ if [[ $SRS_UTEST == YES ]]; then "srs_utest_hevc_structs" "srs_utest_coworkers" "srs_utest_pithy_print" "srs_utest_protocol3" "srs_utest_app" "srs_utest_mock" "srs_utest_rtc_playstream" "srs_utest_rtc_publishstream" "srs_utest_rtc_conn" "srs_utest_rtmp_conn" "srs_utest_srt_conn" "srs_utest_http_conn") - MODULE_FILES+=("srs_utest_ai01" "srs_utest_ai02" "srs_utest_ai03" "srs_utest_ai04" "srs_utest_ai05" - "srs_utest_ai06" "srs_utest_ai07" "srs_utest_ai08" "srs_utest_ai09" "srs_utest_ai10" "srs_utest_ai11" - "srs_utest_ai12" "srs_utest_ai13" "srs_utest_ai14" "srs_utest_ai15" "srs_utest_ai16" "srs_utest_ai17" - "srs_utest_ai18" "srs_utest_ai19") + MODULE_FILES+=("srs_utest_ai01" "srs_utest_ai02" "srs_utest_ai03" "srs_utest_ai04" "srs_utest_ai05" + "srs_utest_ai06" "srs_utest_ai07" "srs_utest_ai08" "srs_utest_ai09" "srs_utest_ai10" "srs_utest_ai11" + "srs_utest_ai12" "srs_utest_ai13" "srs_utest_ai14" "srs_utest_ai15" "srs_utest_ai16" "srs_utest_ai17" + "srs_utest_ai18" "srs_utest_ai19" "srs_utest_ai20") if [[ $SRS_GB28181 == YES ]]; then - MODULE_FILES+=("srs_utest_gb28181" "srs_utest_app14") + MODULE_FILES+=("srs_utest_gb28181" "srs_utest_ai23") fi if [[ $SRS_RTSP == YES ]]; then - MODULE_FILES+=("srs_utest_app12" "srs_utest_app13") + MODULE_FILES+=("srs_utest_ai21" "srs_utest_ai22") fi ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot} ${LibSSLRoot} ${LibSrtpRoot}) if [[ $SRS_FFMPEG_FIT == YES ]]; then diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index acf340a80..f3dd4836a 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -1649,7 +1649,7 @@ void SrsServer::on_unpublish(ISrsRequest *r) SrsSignalManager *SrsSignalManager::instance = NULL; -SrsSignalManager::SrsSignalManager(SrsServer *s) +SrsSignalManager::SrsSignalManager(ISrsSignalHandler *s) { SrsSignalManager::instance = this; diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index a7df440f8..568ec6519 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -362,11 +362,11 @@ SRS_DECLARE_PRIVATE: // clang-format on // clang-format off SRS_DECLARE_PRIVATE: // clang-format on - SrsServer *server_; + ISrsSignalHandler *server_; ISrsCoroutine *trd_; public: - SrsSignalManager(SrsServer *s); + SrsSignalManager(ISrsSignalHandler *s); virtual ~SrsSignalManager(); public: diff --git a/trunk/src/utest/srs_utest_ai17.cpp b/trunk/src/utest/srs_utest_ai17.cpp index bfb47604d..9ab045082 100644 --- a/trunk/src/utest/srs_utest_ai17.cpp +++ b/trunk/src/utest/srs_utest_ai17.cpp @@ -22,7 +22,7 @@ using namespace std; #include #include #include -#include +#include #include #include #include diff --git a/trunk/src/utest/srs_utest_ai18.cpp b/trunk/src/utest/srs_utest_ai18.cpp index 76c65a98b..09c55a853 100644 --- a/trunk/src/utest/srs_utest_ai18.cpp +++ b/trunk/src/utest/srs_utest_ai18.cpp @@ -20,7 +20,7 @@ using namespace std; #include #include #include -#include +#include #include // Mock ISrsSrtSocket implementation diff --git a/trunk/src/utest/srs_utest_ai20.cpp b/trunk/src/utest/srs_utest_ai20.cpp new file mode 100644 index 000000000..8d2f320a8 --- /dev/null +++ b/trunk/src/utest/srs_utest_ai20.cpp @@ -0,0 +1,163 @@ +// +// Copyright (c) 2013-2025 The SRS Authors +// +// SPDX-License-Identifier: MIT +// + +#include + +using namespace std; + +#include +#include +#include +#include +#include +#include + +// Mock ISrsSignalHandler implementation +MockSignalHandlerForManager::MockSignalHandlerForManager() +{ + signal_reload_count_ = 0; + signal_fast_quit_count_ = 0; + signal_gracefully_quit_count_ = 0; + signal_reopen_log_count_ = 0; + signal_persistence_config_count_ = 0; + signal_assert_abort_count_ = 0; + last_signo_ = 0; +} + +MockSignalHandlerForManager::~MockSignalHandlerForManager() +{ +} + +void MockSignalHandlerForManager::on_signal(int signo) +{ + last_signo_ = signo; + + if (signo == SRS_SIGNAL_RELOAD) { + signal_reload_count_++; + } else if (signo == SRS_SIGNAL_FAST_QUIT) { + signal_fast_quit_count_++; + } else if (signo == SRS_SIGNAL_GRACEFULLY_QUIT) { + signal_gracefully_quit_count_++; + } else if (signo == SRS_SIGNAL_REOPEN_LOG) { + signal_reopen_log_count_++; + } else if (signo == SRS_SIGNAL_PERSISTENCE_CONFIG) { + signal_persistence_config_count_++; + } else if (signo == SRS_SIGNAL_ASSERT_ABORT) { + signal_assert_abort_count_++; + } +} + +void MockSignalHandlerForManager::reset() +{ + signal_reload_count_ = 0; + signal_fast_quit_count_ = 0; + signal_gracefully_quit_count_ = 0; + signal_reopen_log_count_ = 0; + signal_persistence_config_count_ = 0; + signal_assert_abort_count_ = 0; + last_signo_ = 0; +} + +VOID TEST(SignalManagerTest, MajorWorkflow) +{ + srs_error_t err; + + // Create mock signal handler + MockSignalHandlerForManager mock_handler; + + // Create SrsSignalManager instance + SrsUniquePtr signal_manager(new SrsSignalManager(&mock_handler)); + + // Test 1: Initialize the signal manager (creates pipe) + HELPER_EXPECT_SUCCESS(signal_manager->initialize()); + + // Verify pipe was created + EXPECT_TRUE(signal_manager->sig_pipe_[0] > 0); + EXPECT_TRUE(signal_manager->sig_pipe_[1] > 0); + EXPECT_TRUE(signal_manager->signal_read_stfd_ != NULL); + + // Test 2: Start the signal manager (starts coroutine) + HELPER_EXPECT_SUCCESS(signal_manager->start()); + + // Give coroutine time to start + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Test 3: Write SRS_SIGNAL_RELOAD directly to the pipe + int signo = SRS_SIGNAL_RELOAD; + ssize_t nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + // Give time for signal to be processed + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Verify the signal was received and processed + EXPECT_EQ(1, mock_handler.signal_reload_count_); + EXPECT_EQ(SRS_SIGNAL_RELOAD, mock_handler.last_signo_); + + // Test 4: Write SRS_SIGNAL_REOPEN_LOG directly to the pipe + signo = SRS_SIGNAL_REOPEN_LOG; + nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + // Give time for signal to be processed + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Verify the signal was received and processed + EXPECT_EQ(1, mock_handler.signal_reopen_log_count_); + EXPECT_EQ(SRS_SIGNAL_REOPEN_LOG, mock_handler.last_signo_); + + // Test 5: Write SRS_SIGNAL_FAST_QUIT directly to the pipe + signo = SRS_SIGNAL_FAST_QUIT; + nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + // Give time for signal to be processed + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Verify the signal was received and processed + EXPECT_EQ(1, mock_handler.signal_fast_quit_count_); + EXPECT_EQ(SRS_SIGNAL_FAST_QUIT, mock_handler.last_signo_); + + // Test 6: Write SRS_SIGNAL_GRACEFULLY_QUIT directly to the pipe + signo = SRS_SIGNAL_GRACEFULLY_QUIT; + nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + // Give time for signal to be processed + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Verify the signal was received and processed + EXPECT_EQ(1, mock_handler.signal_gracefully_quit_count_); + EXPECT_EQ(SRS_SIGNAL_GRACEFULLY_QUIT, mock_handler.last_signo_); + + // Test 7: Write SRS_SIGNAL_PERSISTENCE_CONFIG directly to the pipe + signo = SRS_SIGNAL_PERSISTENCE_CONFIG; + nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + // Give time for signal to be processed + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Verify the signal was received and processed + EXPECT_EQ(1, mock_handler.signal_persistence_config_count_); + EXPECT_EQ(SRS_SIGNAL_PERSISTENCE_CONFIG, mock_handler.last_signo_); + + // Test 8: Write multiple signals in sequence + signo = SRS_SIGNAL_RELOAD; + nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + signo = SRS_SIGNAL_RELOAD; + nwrite = ::write(signal_manager->sig_pipe_[1], &signo, sizeof(int)); + EXPECT_EQ((ssize_t)sizeof(int), nwrite); + + // Give time for signals to be processed + srs_usleep(1 * SRS_UTIME_MILLISECONDS); + + // Verify the signal was received multiple times (now 3 times total) + EXPECT_EQ(3, mock_handler.signal_reload_count_); + EXPECT_EQ(SRS_SIGNAL_RELOAD, mock_handler.last_signo_); +} diff --git a/trunk/src/utest/srs_utest_ai20.hpp b/trunk/src/utest/srs_utest_ai20.hpp new file mode 100644 index 000000000..7de30112c --- /dev/null +++ b/trunk/src/utest/srs_utest_ai20.hpp @@ -0,0 +1,38 @@ +// +// Copyright (c) 2013-2025 The SRS Authors +// +// SPDX-License-Identifier: MIT +// + +#ifndef SRS_UTEST_AI20_HPP +#define SRS_UTEST_AI20_HPP + +/* +#include +*/ +#include + +#include + +// Mock ISrsSignalHandler implementation for testing SrsSignalManager +class MockSignalHandlerForManager : public ISrsSignalHandler +{ +public: + int signal_reload_count_; + int signal_fast_quit_count_; + int signal_gracefully_quit_count_; + int signal_reopen_log_count_; + int signal_persistence_config_count_; + int signal_assert_abort_count_; + int last_signo_; + +public: + MockSignalHandlerForManager(); + virtual ~MockSignalHandlerForManager(); + +public: + virtual void on_signal(int signo); + void reset(); +}; + +#endif diff --git a/trunk/src/utest/srs_utest_app12.cpp b/trunk/src/utest/srs_utest_ai21.cpp similarity index 99% rename from trunk/src/utest/srs_utest_app12.cpp rename to trunk/src/utest/srs_utest_ai21.cpp index 406d7aba3..75b35b457 100644 --- a/trunk/src/utest/srs_utest_app12.cpp +++ b/trunk/src/utest/srs_utest_ai21.cpp @@ -3,7 +3,7 @@ // // SPDX-License-Identifier: MIT // -#include +#include using namespace std; diff --git a/trunk/src/utest/srs_utest_app12.hpp b/trunk/src/utest/srs_utest_ai21.hpp similarity index 98% rename from trunk/src/utest/srs_utest_app12.hpp rename to trunk/src/utest/srs_utest_ai21.hpp index 6b0fea758..9259e620c 100644 --- a/trunk/src/utest/srs_utest_app12.hpp +++ b/trunk/src/utest/srs_utest_ai21.hpp @@ -4,11 +4,11 @@ // SPDX-License-Identifier: MIT // -#ifndef SRS_UTEST_APP12_HPP -#define SRS_UTEST_APP12_HPP +#ifndef SRS_UTEST_AI21_HPP +#define SRS_UTEST_AI21_HPP /* -#include +#include */ #include diff --git a/trunk/src/utest/srs_utest_app13.cpp b/trunk/src/utest/srs_utest_ai22.cpp similarity index 99% rename from trunk/src/utest/srs_utest_app13.cpp rename to trunk/src/utest/srs_utest_ai22.cpp index 6b9c6c879..5c68f3c47 100644 --- a/trunk/src/utest/srs_utest_app13.cpp +++ b/trunk/src/utest/srs_utest_ai22.cpp @@ -3,7 +3,7 @@ // // SPDX-License-Identifier: MIT // -#include +#include using namespace std; diff --git a/trunk/src/utest/srs_utest_app13.hpp b/trunk/src/utest/srs_utest_ai22.hpp similarity index 99% rename from trunk/src/utest/srs_utest_app13.hpp rename to trunk/src/utest/srs_utest_ai22.hpp index 4b6c1ff58..dc02772e1 100644 --- a/trunk/src/utest/srs_utest_app13.hpp +++ b/trunk/src/utest/srs_utest_ai22.hpp @@ -4,11 +4,11 @@ // SPDX-License-Identifier: MIT // -#ifndef SRS_UTEST_APP13_HPP -#define SRS_UTEST_APP13_HPP +#ifndef SRS_UTEST_AI22_HPP +#define SRS_UTEST_AI22_HPP /* -#include +#include */ #include @@ -35,7 +35,7 @@ #include #endif #include -#include +#include // Mock request class for testing edge upstream class MockEdgeRequest : public ISrsRequest diff --git a/trunk/src/utest/srs_utest_app14.cpp b/trunk/src/utest/srs_utest_ai23.cpp similarity index 99% rename from trunk/src/utest/srs_utest_app14.cpp rename to trunk/src/utest/srs_utest_ai23.cpp index 745aa3e4b..edb3a46a0 100644 --- a/trunk/src/utest/srs_utest_app14.cpp +++ b/trunk/src/utest/srs_utest_ai23.cpp @@ -3,7 +3,7 @@ // // SPDX-License-Identifier: MIT // -#include +#include using namespace std; diff --git a/trunk/src/utest/srs_utest_app14.hpp b/trunk/src/utest/srs_utest_ai23.hpp similarity index 99% rename from trunk/src/utest/srs_utest_app14.hpp rename to trunk/src/utest/srs_utest_ai23.hpp index a4eab41f6..bb5329124 100644 --- a/trunk/src/utest/srs_utest_app14.hpp +++ b/trunk/src/utest/srs_utest_ai23.hpp @@ -4,11 +4,11 @@ // SPDX-License-Identifier: MIT // -#ifndef SRS_UTEST_APP14_HPP -#define SRS_UTEST_APP14_HPP +#ifndef SRS_UTEST_AI23_HPP +#define SRS_UTEST_AI23_HPP /* -#include +#include */ #include diff --git a/trunk/src/utest/srs_utest_mock.cpp b/trunk/src/utest/srs_utest_mock.cpp index 3fdd89b9f..503eb5bf8 100644 --- a/trunk/src/utest/srs_utest_mock.cpp +++ b/trunk/src/utest/srs_utest_mock.cpp @@ -40,7 +40,7 @@ #include -#include +#include // MockSdpFactory implementation MockSdpFactory::MockSdpFactory() diff --git a/trunk/src/utest/srs_utest_rtmp_conn.cpp b/trunk/src/utest/srs_utest_rtmp_conn.cpp index 4e0396812..60722df11 100644 --- a/trunk/src/utest/srs_utest_rtmp_conn.cpp +++ b/trunk/src/utest/srs_utest_rtmp_conn.cpp @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include