GroveEngine/include/grove/SequentialModuleSystem.h
StillHammer d8c5f93429 feat: Add comprehensive hot-reload test suite with 3 integration scenarios
This commit implements a complete test infrastructure for validating
hot-reload stability and robustness across multiple scenarios.

## New Test Infrastructure

### Test Helpers (tests/helpers/)
- TestMetrics: FPS, memory, reload time tracking with statistics
- TestReporter: Assertion tracking and formatted test reports
- SystemUtils: Memory usage monitoring via /proc/self/status
- TestAssertions: Macro-based assertion framework

### Test Modules
- TankModule: Realistic module with 50 tanks for production testing
- ChaosModule: Crash-injection module for robustness validation
- StressModule: Lightweight module for long-duration stability tests

## Integration Test Scenarios

### Scenario 1: Production Hot-Reload (test_01_production_hotreload.cpp)
 PASSED - End-to-end hot-reload validation
- 30 seconds simulation (1800 frames @ 60 FPS)
- TankModule with 50 tanks, realistic state
- Source modification (v1.0 → v2.0), recompilation, reload
- State preservation: positions, velocities, frameCount
- Metrics: ~163ms reload time, 0.88MB memory growth

### Scenario 2: Chaos Monkey (test_02_chaos_monkey.cpp)
 PASSED - Extreme robustness testing
- 150+ random crashes per run (5% crash probability per frame)
- 5 crash types: runtime_error, logic_error, out_of_range, domain_error, state corruption
- 100% recovery rate via automatic hot-reload
- Corrupted state detection and rejection
- Random seed for unpredictable crash patterns
- Proof of real reload: temporary files in /tmp/grove_module_*.so

### Scenario 3: Stress Test (test_03_stress_test.cpp)
 PASSED - Long-duration stability validation
- 10 minutes simulation (36000 frames @ 60 FPS)
- 120 hot-reloads (every 5 seconds)
- 100% reload success rate (120/120)
- Memory growth: 2 MB (threshold: 50 MB)
- Avg reload time: 160ms (threshold: 500ms)
- No memory leaks, no file descriptor leaks

## Core Engine Enhancements

### ModuleLoader (src/ModuleLoader.cpp)
- Temporary file copy to /tmp/ for Linux dlopen cache bypass
- Robust reload() method: getState() → unload() → load() → setState()
- Automatic cleanup of temporary files
- Comprehensive error handling and logging

### DebugEngine (src/DebugEngine.cpp)
- Automatic recovery in processModuleSystems()
- Exception catching → logging → module reload → continue
- Module state dump utilities for debugging

### SequentialModuleSystem (src/SequentialModuleSystem.cpp)
- extractModule() for safe module extraction
- registerModule() for module re-registration
- Enhanced processModules() with error handling

## Build System
- CMake configuration for test infrastructure
- Shared library compilation for test modules (.so)
- CTest integration for all scenarios
- PIC flag management for spdlog compatibility

## Documentation (planTI/)
- Complete test architecture documentation
- Detailed scenario specifications with success criteria
- Global test plan and validation thresholds

## Validation Results
All 3 integration scenarios pass successfully:
- Production hot-reload: State preservation validated
- Chaos Monkey: 100% recovery from 150+ crashes
- Stress Test: Stable over 120 reloads, minimal memory growth

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 22:13:07 +08:00

91 lines
3.0 KiB
C++

#pragma once
#include <memory>
#include <string>
#include <queue>
#include <chrono>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include "IModuleSystem.h"
#include "IModule.h"
#include "IIO.h"
using json = nlohmann::json;
namespace grove {
/**
* @brief Sequential module system implementation for debug and testing
*
* SequentialModuleSystem processes modules one at a time in a simple, predictable manner.
* Perfect for development, debugging, and testing scenarios where deterministic execution
* is more important than performance.
*
* Features:
* - Single-threaded execution (thread-safe by design)
* - Immediate task execution (no actual scheduling)
* - Comprehensive logging of all operations
* - Simple state management
* - Perfect for step-by-step debugging
*
* Task scheduling behavior:
* - scheduleTask() executes immediately (no queue)
* - hasCompletedTasks() always returns 0 (tasks complete immediately)
* - getCompletedTask() throws (no queued results)
*/
class SequentialModuleSystem : public IModuleSystem {
private:
std::shared_ptr<spdlog::logger> logger;
std::unique_ptr<IModule> module;
std::string moduleName = "unknown";
std::unique_ptr<IIO> ioLayer;
// Performance tracking
std::chrono::high_resolution_clock::time_point lastProcessTime;
size_t processCallCount = 0;
float totalProcessTime = 0.0f;
float lastProcessDuration = 0.0f;
// Task execution tracking (for logging purposes)
size_t taskExecutionCount = 0;
// Helper methods
void logSystemStart();
void logProcessStart(float deltaTime);
void logProcessEnd(float processTime);
void logTaskExecution(const std::string& taskType, const IDataNode& taskData);
void validateModule() const;
public:
SequentialModuleSystem();
virtual ~SequentialModuleSystem();
// IModuleSystem implementation
void registerModule(const std::string& name, std::unique_ptr<IModule> module) override;
void processModules(float deltaTime) override;
void setIOLayer(std::unique_ptr<IIO> ioLayer) override;
std::unique_ptr<IDataNode> queryModule(const std::string& name, const IDataNode& input) override;
ModuleSystemType getType() const override;
int getPendingTaskCount(const std::string& moduleName) const override;
// Hot-reload support
std::unique_ptr<IModule> extractModule();
// ITaskScheduler implementation (inherited)
void scheduleTask(const std::string& taskType, std::unique_ptr<IDataNode> taskData) override;
int hasCompletedTasks() const override;
std::unique_ptr<IDataNode> getCompletedTask() override;
// Debug and monitoring methods
json getPerformanceMetrics() const;
void resetPerformanceMetrics();
float getAverageProcessTime() const;
size_t getProcessCallCount() const;
size_t getTaskExecutionCount() const;
// Configuration
void setLogLevel(spdlog::level::level_enum level);
};
} // namespace grove