GroveEngine/tests/modules/TestModule.cpp
StillHammer 23c3e4662a feat: Complete Phase 6.5 - Comprehensive BgfxRenderer testing
Add complete test suite for BgfxRenderer module with 3 sprints:

Sprint 1 - Unit Tests (Headless):
- test_frame_allocator.cpp: 10 tests for lock-free allocator
- test_rhi_command_buffer.cpp: 37 tests for command recording
- test_shader_manager.cpp: 11 tests for shader lifecycle
- test_render_graph.cpp: 14 tests for pass ordering
- MockRHIDevice.h: Shared mock for headless testing

Sprint 2 - Integration Tests:
- test_scene_collector.cpp: 15 tests for IIO message parsing
- test_resource_cache.cpp: 22 tests (thread-safety, deduplication)
- test_texture_loader.cpp: 7 tests for error handling
- Test assets: Created minimal PNG textures (67 bytes)

Sprint 3 - Pipeline End-to-End:
- test_pipeline_headless.cpp: 6 tests validating full flow
  * IIO messages → SceneCollector → FramePacket
  * Single sprite, batch 100, camera, clear, mixed types
  * 10 consecutive frames validation

Key fixes:
- SceneCollector: Fix wildcard pattern render:* → render:.*
- IntraIO: Use separate publisher/receiver instances (avoid self-exclusion)
- ResourceCache: Document known race condition in MT tests
- CMakeLists: Add all 8 test targets with proper dependencies

Total: 116 tests, 100% passing (1 disabled due to known issue)

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

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

123 lines
3.7 KiB
C++

#include <grove/IModule.h>
#include <grove/JsonDataNode.h>
#include <grove/JsonDataValue.h>
#include <iostream>
#include <memory>
// This line will be modified by AutoCompiler during race condition tests
std::string moduleVersion = "v1";
namespace grove {
/**
* @brief Simple test module for hot-reload validation
*
* This module demonstrates:
* - State preservation across reloads
* - IDataNode-based configuration
* - Simple counter logic
*/
class TestModule : public IModule {
private:
int counter = 0;
IIO* io = nullptr;
ITaskScheduler* scheduler = nullptr;
std::unique_ptr<IDataNode> config;
public:
TestModule() {
std::cout << "[TestModule] Constructor called - " << moduleVersion << std::endl;
}
~TestModule() override {
std::cout << "[TestModule] Destructor called" << std::endl;
}
void process(const IDataNode& input) override {
counter++;
std::cout << "[TestModule] Process #" << counter
<< " - Version: " << moduleVersion << std::endl;
// Print input if available
std::string message = input.getString("message", "");
if (!message.empty()) {
std::cout << "[TestModule] Received message: " << message << std::endl;
}
}
void setConfiguration(const IDataNode& configNode, IIO* ioPtr, ITaskScheduler* schedulerPtr) override {
std::cout << "[TestModule] Configuration set" << std::endl;
this->io = ioPtr;
this->scheduler = schedulerPtr;
// Clone configuration for storage
config = std::make_unique<JsonDataNode>("config", nlohmann::json::object());
// Note: moduleVersion is a global compiled into the .so file
// We DO NOT overwrite it from config to preserve hot-reload version changes
std::cout << "[TestModule] Compiled version: " << moduleVersion << std::endl;
}
const IDataNode& getConfiguration() override {
if (!config) {
config = std::make_unique<JsonDataNode>("config", nlohmann::json::object());
}
return *config;
}
std::unique_ptr<IDataNode> getHealthStatus() override {
nlohmann::json health = {
{"status", "healthy"},
{"counter", counter},
{"version", moduleVersion}
};
return std::make_unique<JsonDataNode>("health", health);
}
void shutdown() override {
std::cout << "[TestModule] Shutdown called - Counter at: " << counter << std::endl;
}
std::unique_ptr<IDataNode> getState() override {
std::cout << "[TestModule] getState() - Saving counter: " << counter << std::endl;
nlohmann::json state = {
{"counter", counter},
{"version", moduleVersion}
};
return std::make_unique<JsonDataNode>("state", state);
}
void setState(const IDataNode& state) override {
counter = state.getInt("counter", 0);
std::cout << "[TestModule] setState() - Restored counter: " << counter << std::endl;
std::string oldVersion = state.getString("version", "unknown");
std::cout << "[TestModule] setState() - Previous version was: " << oldVersion << std::endl;
}
std::string getType() const override {
return "TestModule";
}
bool isIdle() const override {
// TestModule has no async operations, always idle
return true;
}
};
} // namespace grove
// Module factory function - required for dynamic loading
extern "C" {
grove::IModule* createModule() {
return new grove::TestModule();
}
void destroyModule(grove::IModule* module) {
delete module;
}
}