-
Notifications
You must be signed in to change notification settings - Fork 461
Fix stale test debug callback #11785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+269
−18
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c9f97d5
Fix stale test debug callback
jkwak-work b1ae635
Address review feedback
jkwak-work ed5ecde
Add debug callback lifetime tests
jkwak-work 31df95c
Make debug callback bridge thread-safe
jkwak-work 0389951
Add debug callback bridge regression coverage
jkwak-work ea591de
Use retained debug bridge per test invocation
jkwak-work File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
176 changes: 176 additions & 0 deletions
176
tools/gfx-unit-test/scoped-core-debug-callback-test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| #include "../render-test/slang-support.h" | ||
| #include "unit-test/slang-unit-test.h" | ||
|
|
||
| #include <atomic> | ||
| #include <thread> | ||
|
|
||
| namespace | ||
| { | ||
| void emitError(renderer_test::CoreToRHIDebugBridge& bridge, const char* message) | ||
| { | ||
| bridge.handleMessage(rhi::DebugMessageType::Error, rhi::DebugMessageSource::Layer, message); | ||
| } | ||
|
|
||
| renderer_test::CoreToRHIDebugBridge* getRetainedBridgeAfterStackCallbackScope() | ||
| { | ||
| auto bridge = renderer_test::createRetainedCoreToRHIDebugBridge(); | ||
|
|
||
| renderer_test::CoreDebugCallback callback; | ||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(*bridge, &callback); | ||
| emitError(*bridge, "retained scope"); | ||
| } | ||
| SLANG_CHECK(callback.getString() == "retained scope\n"); | ||
|
|
||
| return bridge.Ptr(); | ||
| } | ||
| } // namespace | ||
|
|
||
| SLANG_UNIT_TEST(scopedCoreDebugCallbackClearsBridgeOnExit) | ||
|
jkwak-work marked this conversation as resolved.
|
||
| { | ||
| renderer_test::CoreToRHIDebugBridge bridge; | ||
|
|
||
| { | ||
| renderer_test::CoreDebugCallback callback; | ||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &callback); | ||
| emitError(bridge, "inside scope"); | ||
| SLANG_CHECK(callback.getString() == "inside scope\n"); | ||
| } | ||
|
|
||
| emitError(bridge, "after scope"); | ||
| SLANG_CHECK(callback.getString() == "inside scope\n"); | ||
| } | ||
|
|
||
| emitError(bridge, "after callback"); | ||
|
|
||
| renderer_test::CoreDebugCallback nextCallback; | ||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &nextCallback); | ||
| emitError(bridge, "next scope"); | ||
| } | ||
| SLANG_CHECK(nextCallback.getString() == "next scope\n"); | ||
| } | ||
|
|
||
| SLANG_UNIT_TEST(scopedCoreDebugCallbackDoesNotLeakAcrossScopes) | ||
| { | ||
| renderer_test::CoreToRHIDebugBridge bridge; | ||
| renderer_test::CoreDebugCallback firstCallback; | ||
| renderer_test::CoreDebugCallback secondCallback; | ||
|
|
||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &firstCallback); | ||
| emitError(bridge, "first"); | ||
| } | ||
|
|
||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &secondCallback); | ||
| emitError(bridge, "second"); | ||
| } | ||
|
|
||
| SLANG_CHECK(firstCallback.getString() == "first\n"); | ||
| SLANG_CHECK(secondCallback.getString() == "second\n"); | ||
| } | ||
|
jkwak-work marked this conversation as resolved.
|
||
|
|
||
| SLANG_UNIT_TEST(scopedCoreDebugCallbackClearsBridgeOnException) | ||
| { | ||
| renderer_test::CoreToRHIDebugBridge bridge; | ||
| renderer_test::CoreDebugCallback firstCallback; | ||
|
|
||
| try | ||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &firstCallback); | ||
| emitError(bridge, "before throw"); | ||
| throw 1; | ||
| } | ||
| catch (...) | ||
| { | ||
| } | ||
|
|
||
| emitError(bridge, "after exception"); | ||
|
|
||
| renderer_test::CoreDebugCallback secondCallback; | ||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &secondCallback); | ||
| emitError(bridge, "next iteration"); | ||
| } | ||
|
|
||
| SLANG_CHECK(firstCallback.getString() == "before throw\n"); | ||
| SLANG_CHECK(secondCallback.getString() == "next iteration\n"); | ||
| } | ||
|
jkwak-work marked this conversation as resolved.
|
||
|
|
||
| SLANG_UNIT_TEST(scopedCoreDebugCallbackSeparatesRetainedBridgeScopes) | ||
| { | ||
| renderer_test::CoreToRHIDebugBridge* oldBridge = getRetainedBridgeAfterStackCallbackScope(); | ||
| auto nextBridge = renderer_test::createRetainedCoreToRHIDebugBridge(); | ||
|
|
||
| renderer_test::CoreDebugCallback nextCallback; | ||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(*nextBridge, &nextCallback); | ||
| emitError(*oldBridge, "after stack callback"); | ||
| emitError(*nextBridge, "next invocation"); | ||
| } | ||
| SLANG_CHECK(nextCallback.getString() == "next invocation\n"); | ||
| } | ||
|
|
||
| SLANG_UNIT_TEST(coreDebugBridgeHandlesConcurrentMessages) | ||
| { | ||
| static constexpr int kThreadCount = 4; | ||
| static constexpr int kMessageCount = 1024; | ||
|
|
||
| renderer_test::CoreToRHIDebugBridge bridge; | ||
| renderer_test::CoreDebugCallback callback; | ||
| std::atomic<bool> startWriting(false); | ||
| std::atomic<bool> keepReading(true); | ||
|
|
||
| std::thread readerThread( | ||
| [&]() | ||
| { | ||
| while (keepReading.load(std::memory_order_acquire)) | ||
| { | ||
| callback.getString(); | ||
| } | ||
| }); | ||
|
|
||
| std::thread writerThreads[kThreadCount]; | ||
| for (int threadIndex = 0; threadIndex < kThreadCount; ++threadIndex) | ||
| { | ||
| writerThreads[threadIndex] = std::thread( | ||
| [&]() | ||
| { | ||
| while (!startWriting.load(std::memory_order_acquire)) | ||
| { | ||
| std::this_thread::yield(); | ||
| } | ||
|
|
||
| for (int messageIndex = 0; messageIndex < kMessageCount; ++messageIndex) | ||
| { | ||
| emitError(bridge, "x"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| { | ||
| renderer_test::ScopedCoreDebugCallback scopedDebugCallback(bridge, &callback); | ||
| startWriting.store(true, std::memory_order_release); | ||
| for (int spinCount = 0; spinCount < 100000 && callback.getString().getLength() == 0; | ||
| ++spinCount) | ||
| { | ||
| std::this_thread::yield(); | ||
| } | ||
| SLANG_CHECK(callback.getString().getLength() > 0); | ||
| } | ||
|
|
||
| for (auto& writerThread : writerThreads) | ||
| { | ||
| writerThread.join(); | ||
| } | ||
|
|
||
| keepReading.store(false, std::memory_order_release); | ||
| readerThread.join(); | ||
|
|
||
| auto capturedLength = callback.getString().getLength(); | ||
| SLANG_CHECK(capturedLength > 0); | ||
| SLANG_CHECK(capturedLength <= kThreadCount * kMessageCount * 2); | ||
| SLANG_CHECK((capturedLength % 2) == 0); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.