AI: Add utest to cover app http module.

This commit is contained in:
OSSRS-AI
2025-10-04 22:41:09 -04:00
committed by winlin
parent b5664747ac
commit 3948f0d4fe
42 changed files with 3382 additions and 330 deletions

View File

@@ -125,6 +125,118 @@ codebase_structure:
description: "Smart pointer definitions for memory management"
code_patterns:
dependency_inversion:
principle: "MANDATORY - Classes should depend on interfaces, not concrete classes"
description: |
Follow the Dependency Inversion Principle (DIP) - high-level modules should not depend on low-level modules.
Both should depend on abstractions (interfaces). This improves testability, maintainability, and flexibility.
In SRS, interfaces start with 'ISrs' prefix (e.g., ISrsBufferCache, ISrsMessageQueue), while concrete classes
start with 'Srs' prefix (e.g., SrsBufferCache, SrsMessageQueue).
usage: |
WRONG: Depending on concrete class
class SrsBufferCache {
private:
SrsMessageQueue *queue_; // Direct dependency on concrete class
};
CORRECT: Depending on interface
class SrsBufferCache : public ISrsBufferCache {
private:
ISrsMessageQueue *queue_; // Dependency on interface
};
rules:
- "Interfaces start with 'ISrs' prefix (e.g., ISrsMessageQueue, ISrsBufferCache, ISrsCoroutineHandler)"
- "Concrete classes start with 'Srs' prefix (e.g., SrsMessageQueue, SrsBufferCache, SrsServer)"
- "Class member variables should use interface types (ISrs*) instead of concrete types (Srs*)"
- "Classes should implement interfaces when they provide functionality that may need to be mocked or substituted"
- "Use concrete types only for instantiation, not for member variable declarations"
benefits:
- "Enables dependency injection for unit testing"
- "Allows easy mocking of dependencies in tests"
- "Reduces coupling between components"
- "Makes code more flexible and maintainable"
- "Facilitates future refactoring and extensions"
rationale: "Interface-based design is fundamental to testable, maintainable code. It allows components to be tested in isolation and makes the codebase more flexible for future changes."
avoid_global_variables:
principle: "MANDATORY - Never directly use global variables, convert to member fields for mockability"
description: |
Direct use of global variables makes code untestable because globals cannot be mocked.
Global variables in SRS start with _srs prefix (e.g., _srs_config, _srs_sources, _srs_stat, _srs_hooks, _srs_context).
Always store global references as member fields in the constructor so they can be replaced with mocks in tests.
usage: |
WRONG: Direct use of global variable
srs_error_t SrsBufferCache::start() {
fast_cache_ = _srs_config->get_vhost_http_remux_fast_cache(req_->vhost_);
}
CORRECT: Store global as member field for mockability
SrsBufferCache::SrsBufferCache(ISrsRequest *r) {
config_ = _srs_config; // Store global reference as member field
}
SrsBufferCache::~SrsBufferCache() {
config_ = NULL; // Set to NULL, do NOT free (it's a global reference)
}
srs_error_t SrsBufferCache::start() {
fast_cache_ = config_->get_vhost_http_remux_fast_cache(req_->vhost_);
}
rules:
- "Global variables in SRS start with _srs prefix: _srs_config, _srs_sources, _srs_stat, _srs_hooks, _srs_context, etc."
- "Never access global variables directly in methods"
- "Always store global references as member fields in constructor"
- "Use interface types for member fields (e.g., ISrsAppConfig* config_)"
- "In destructor, set global reference fields to NULL but do NOT free them"
- "This allows tests to inject mock objects by setting the member field"
common_global_variables:
core_globals:
- "_srs_log - Global logging interface (ISrsLog*)"
- "_srs_context - Thread context for coroutine management (ISrsContext*)"
- "_srs_config - Global configuration object (SrsConfig*)"
- "_srs_kernel_factory - Kernel object factory (ISrsKernelFactory*)"
- "_srs_app_factory - Application object factory (SrsAppFactory*)"
app_globals:
- "_srs_server - Main SRS server instance (SrsServer*)"
- "_srs_sources - Live source manager (SrsLiveSourceManager*)"
- "_srs_stat - Statistics manager (SrsStatistic*)"
- "_srs_hooks - HTTP hooks manager (ISrsHttpHooks*)"
- "_srs_circuit_breaker - Circuit breaker for overload protection (ISrsCircuitBreaker*)"
- "_srs_dvr_async - Async worker for DVR operations (SrsAsyncCallWorker*)"
timing_globals:
- "_srs_clock - Wall clock for time operations (SrsWallClock*)"
- "_srs_shared_timer - Shared timer for periodic tasks (SrsSharedTimer*)"
- "_srs_stages - Stage manager for pithy print (SrsStageManager*)"
resource_globals:
- "_srs_conn_manager - Connection resource manager (SrsResourceManager*)"
- "_srs_pps_* - Various PPS (packets per second) statistics counters"
webrtc_globals:
- "_srs_blackhole - WebRTC blackhole for packet dropping (SrsRtcBlackhole*)"
- "_srs_rtc_dtls_certificate - DTLS certificate for WebRTC (SrsDtlsCertificate*)"
other_globals:
- "_srs_in_docker - Boolean flag indicating if running in Docker"
- "_srs_config_by_env - Boolean flag for environment variable configuration"
- "_srs_binary - Binary name of SRS executable"
benefits:
- "Enables dependency injection for unit testing"
- "Allows mocking of global dependencies in tests"
- "Makes dependencies explicit and visible in class interface"
- "Improves code testability and maintainability"
rationale: "Global variables create hidden dependencies that cannot be mocked or controlled in tests. Converting them to member fields makes dependencies explicit and testable."
cpp_compatibility:
standard: "C++98"
description: |