GroveEngine/tests/integration/test_22_bgfx_sprites_headless.cpp
StillHammer 1b7703f07b feat(IIO)!: BREAKING CHANGE - Callback-based message dispatch
## Breaking Change

IIO API redesigned from manual pull+if-forest to callback dispatch.
All modules must update their subscribe() calls to pass handlers.

### Before (OLD API)
```cpp
io->subscribe("input:mouse");

void process(...) {
    while (io->hasMessages()) {
        auto msg = io->pullMessage();
        if (msg.topic == "input:mouse") {
            handleMouse(msg);
        } else if (msg.topic == "input:keyboard") {
            handleKeyboard(msg);
        }
    }
}
```

### After (NEW API)
```cpp
io->subscribe("input:mouse", [this](const Message& msg) {
    handleMouse(msg);
});

void process(...) {
    while (io->hasMessages()) {
        io->pullAndDispatch();  // Callbacks invoked automatically
    }
}
```

## Changes

**Core API (include/grove/IIO.h)**
- Added: `using MessageHandler = std::function<void(const Message&)>`
- Changed: `subscribe()` now requires `MessageHandler` callback parameter
- Changed: `subscribeLowFreq()` now requires `MessageHandler` callback
- Removed: `pullMessage()`
- Added: `pullAndDispatch()` - pulls and auto-dispatches to handlers

**Implementation (src/IntraIO.cpp)**
- Store callbacks in `Subscription.handler`
- `pullAndDispatch()` matches topic against ALL subscriptions (not just first)
- Fixed: Regex pattern compilation supports both wildcards (*) and regex (.*)
- Performance: ~1000 msg/s throughput (unchanged from before)

**Files Updated**
- 31 test/module files migrated to callback API (via parallel agents)
- 8 documentation files updated (DEVELOPER_GUIDE, USER_GUIDE, module READMEs)

## Bugs Fixed During Migration

1. **pullAndDispatch() early return bug**: Was only calling FIRST matching handler
   - Fix: Loop through ALL subscriptions, invoke all matching handlers

2. **Regex pattern compilation bug**: Pattern "player:.*" failed to match
   - Fix: Detect ".*" in pattern → use as regex, otherwise escape and convert wildcards

## Testing

 test_11_io_system: PASSED (IIO pub/sub, pattern matching, batching)
 test_threaded_module_system: 6/6 PASSED
 test_threaded_stress: 5/5 PASSED (50 modules, 100x reload, concurrent ops)
 test_12_datanode: PASSED
 10 TopicTree scenarios: 10/10 PASSED
 benchmark_e2e: ~1000 msg/s throughput

Total: 23+ tests passing

## Performance Impact

No performance regression from callback dispatch:
- IIO throughput: ~1000 msg/s (same as before)
- ThreadedModuleSystem: Speedup ~1.0x (barrier pattern expected)

## Migration Guide

For all modules using IIO:

1. Update subscribe() calls to include handler lambda
2. Replace pullMessage() loops with pullAndDispatch()
3. Move topic-specific logic from if-forest into callbacks

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-19 14:19:27 +07:00

139 lines
4.9 KiB
C++

/**
* Test: BgfxRenderer Sprite Integration Test (Headless)
*
* Tests the BgfxRendererModule data structures without actual rendering.
* Validates: JsonDataNode for sprite data, IIO message publishing.
*
* Note: SceneCollector/FramePacket tests are in the visual test that links with BgfxRenderer.
*/
#include <grove/JsonDataNode.h>
#include <grove/IntraIOManager.h>
#include <grove/IntraIO.h>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <iostream>
#include <cmath>
using Catch::Matchers::WithinAbs;
TEST_CASE("SpriteInstance data layout", "[bgfx][unit]") {
// Test that SpriteInstance struct can be constructed from IIO message data
auto sprite = std::make_unique<grove::JsonDataNode>("sprite");
sprite->setDouble("x", 100.0);
sprite->setDouble("y", 200.0);
sprite->setDouble("scaleX", 32.0);
sprite->setDouble("scaleY", 32.0);
sprite->setDouble("rotation", 1.57f); // ~90 degrees
sprite->setDouble("u0", 0.0);
sprite->setDouble("v0", 0.0);
sprite->setDouble("u1", 1.0);
sprite->setDouble("v1", 1.0);
sprite->setInt("color", 0xFF0000FF);
sprite->setInt("textureId", 5);
sprite->setInt("layer", 10);
// Verify data can be read back
REQUIRE(sprite->getDouble("x") == 100.0);
REQUIRE(sprite->getDouble("y") == 200.0);
REQUIRE(sprite->getDouble("scaleX") == 32.0);
REQUIRE(sprite->getDouble("scaleY") == 32.0);
REQUIRE_THAT(sprite->getDouble("rotation"), WithinAbs(1.57, 0.01));
REQUIRE(sprite->getInt("color") == 0xFF0000FF);
REQUIRE(sprite->getInt("textureId") == 5);
REQUIRE(sprite->getInt("layer") == 10);
}
TEST_CASE("IIO sprite message routing between modules", "[bgfx][integration]") {
// Use singleton IntraIOManager (same as IntraIO::publish uses)
auto& ioManager = grove::IntraIOManager::getInstance();
// Create two IO instances to simulate module communication
auto gameIO = ioManager.createInstance("test_game_module");
auto rendererIO = ioManager.createInstance("test_renderer_module");
int messageCount = 0;
bool firstMessageVerified = false;
// Renderer subscribes to render topics with callback
rendererIO->subscribe("render:*", [&](const Message& msg) {
messageCount++;
if (messageCount == 1) {
// Verify first message
REQUIRE(msg.topic == "render:sprite");
REQUIRE(msg.data != nullptr);
REQUIRE_THAT(msg.data->getDouble("x"), WithinAbs(100.0, 0.01));
firstMessageVerified = true;
}
});
// Game module publishes sprites via IIO
for (int i = 0; i < 3; ++i) {
auto sprite = std::make_unique<grove::JsonDataNode>("sprite");
sprite->setDouble("x", 100.0 + i * 50);
sprite->setDouble("y", 200.0 + i * 30);
sprite->setDouble("scaleX", 32.0);
sprite->setDouble("scaleY", 32.0);
sprite->setInt("color", 0xFFFFFFFF);
sprite->setInt("layer", i);
std::unique_ptr<grove::IDataNode> spriteData = std::move(sprite);
gameIO->publish("render:sprite", std::move(spriteData));
}
// Dispatch messages to trigger callbacks
while (rendererIO->hasMessages() > 0) {
rendererIO->pullAndDispatch();
}
// Verify we received all 3 messages
REQUIRE(messageCount == 3);
REQUIRE(firstMessageVerified);
// Cleanup
rendererIO->clearAllMessages();
ioManager.removeInstance("test_game_module");
ioManager.removeInstance("test_renderer_module");
}
TEST_CASE("Camera message structure", "[bgfx][unit]") {
auto camera = std::make_unique<grove::JsonDataNode>("camera");
camera->setDouble("x", 100.0);
camera->setDouble("y", 50.0);
camera->setDouble("zoom", 2.0);
camera->setInt("viewportX", 0);
camera->setInt("viewportY", 0);
camera->setInt("viewportW", 800);
camera->setInt("viewportH", 600);
REQUIRE(camera->getDouble("x") == 100.0);
REQUIRE(camera->getDouble("y") == 50.0);
REQUIRE(camera->getDouble("zoom") == 2.0);
REQUIRE(camera->getInt("viewportW") == 800);
REQUIRE(camera->getInt("viewportH") == 600);
}
TEST_CASE("Clear color message structure", "[bgfx][unit]") {
auto clear = std::make_unique<grove::JsonDataNode>("clear");
clear->setInt("color", 0x112233FF);
REQUIRE(clear->getInt("color") == 0x112233FF);
}
TEST_CASE("Debug line message structure", "[bgfx][unit]") {
auto line = std::make_unique<grove::JsonDataNode>("line");
line->setDouble("x1", 10.0);
line->setDouble("y1", 20.0);
line->setDouble("x2", 100.0);
line->setDouble("y2", 200.0);
line->setInt("color", 0xFF0000FF);
REQUIRE(line->getDouble("x1") == 10.0);
REQUIRE(line->getDouble("y1") == 20.0);
REQUIRE(line->getDouble("x2") == 100.0);
REQUIRE(line->getDouble("y2") == 200.0);
REQUIRE(line->getInt("color") == 0xFF0000FF);
}