-
Notifications
You must be signed in to change notification settings - Fork 211
chore: add INTERCEPT capability to enable unit test code to run arbitrary code at predefined points in the library code #5725
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
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f1d92c
intercept.h with unit_interception demonstration
rroelke 0bf6bff
Fix windows C4459
rroelke 8c5c12f
Merge remote-tracking branch 'origin/main' into rr/unit-test-intercepts
rroelke 3369d27
Review comments
rroelke 3aa5319
Test INTERCEPT reference type and fix forwarding
rroelke 9d78993
We don't need __VA_OPT__
rroelke 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * @file intercept.h | ||
| * | ||
| * @section LICENSE | ||
| * | ||
| * The MIT License | ||
| * | ||
| * @copyright Copyright (c) 2025 TileDB, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| * | ||
| * @section DESCRIPTION | ||
| * | ||
| * This file provides declarations for interception, which allows us | ||
| * to run arbitrary code internally at pre-defined "interception points". | ||
| * This can be used to verify that a test case causes a specific event | ||
| * to occur, or it can be used to pause and resume tasks so as | ||
| * to simulate particular patterns of concurrent execution, or it can | ||
| * be used to simulate exceptions being thrown from particular blocks | ||
| * of code, etc. etc. | ||
| */ | ||
|
|
||
| #ifndef TILEDB_TEST_INTERCEPTION_H | ||
| #define TILEDB_TEST_INTERCEPTION_H | ||
|
|
||
| #if defined(TILEDB_INTERCEPTS) | ||
|
|
||
| #include "tiledb/common/macros.h" | ||
|
|
||
| #include <functional> | ||
| #include <list> | ||
|
|
||
| namespace tiledb::intercept { | ||
|
|
||
| /** | ||
| * A set of actions to perform at a logical interception point. | ||
| * | ||
| * An `InterceptionPoint` may capture values from an `INTERCEPT` (see below) | ||
| * and runs an arbitrary set of callbacks over those values. | ||
| * | ||
| * Do not use this directly; instead use the macros `DECLARE_INTERCEPT`, | ||
| * `DEFINE_INTERCEPT`, and `INTERCEPT` to create functions which | ||
| * respectively create and invoke callbacks of `InterceptionPoint`. | ||
| */ | ||
| template <std::copyable... T> | ||
| class InterceptionPoint { | ||
| using Self = InterceptionPoint<T...>; | ||
|
|
||
| public: | ||
| class CallbackRegistration { | ||
| Self& intercept_; | ||
|
|
||
| using iter = std::list<std::function<void(T&&...)>>::const_iterator; | ||
| iter callback_node_; | ||
|
|
||
| public: | ||
| CallbackRegistration(InterceptionPoint& intercept, iter node) | ||
| : intercept_(intercept) | ||
| , callback_node_(node) { | ||
| } | ||
|
|
||
| ~CallbackRegistration() { | ||
| intercept_.callbacks_.erase(callback_node_); | ||
| } | ||
|
|
||
| DISABLE_COPY_AND_COPY_ASSIGN(CallbackRegistration); | ||
| }; | ||
|
|
||
| void event(T&&... args) { | ||
| for (auto& callback : callbacks_) { | ||
| callback(std::forward<T>(args)...); | ||
| } | ||
| } | ||
|
|
||
| CallbackRegistration and_also(std::function<void(T&&...)>&& callback) { | ||
| callbacks_.push_back(std::move(callback)); | ||
| return CallbackRegistration(*this, std::next(callbacks_.end(), -1)); | ||
| } | ||
|
|
||
| private: | ||
| friend class CallbackRegistration; | ||
|
|
||
| std::list<std::function<void(T&&...)>> callbacks_; | ||
| }; | ||
|
|
||
| } // namespace tiledb::intercept | ||
|
|
||
| #define DECLARE_INTERCEPT(name, ...) \ | ||
| extern tiledb::intercept::InterceptionPoint<__VA_ARGS__>& name() | ||
|
|
||
| #define DEFINE_INTERCEPT(name, ...) \ | ||
| tiledb::intercept::InterceptionPoint<__VA_ARGS__>& name() { \ | ||
| static tiledb::intercept::InterceptionPoint<__VA_ARGS__> impl; \ | ||
| return impl; \ | ||
| } | ||
|
|
||
| #define INTERCEPT(name, ...) \ | ||
| do { \ | ||
| ([](auto... args) { \ | ||
| name().event(std::forward<decltype(args)>(args)...); \ | ||
| })(__VA_ARGS__); \ | ||
| } while (0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be not a macro?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The macro is useful for |
||
|
|
||
| #else // not defined(TILEDB_INTERCEPTS) | ||
|
|
||
| #define DECLARE_INTERCEPT(...) | ||
| #define DEFINE_INTERCEPT(...) | ||
| #define INTEREPT(...) | ||
|
rroelke marked this conversation as resolved.
Outdated
|
||
|
|
||
| #endif | ||
|
|
||
| #endif | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /** | ||
| * @file unit_intercept.cc | ||
| * | ||
| * @section LICENSE | ||
| * | ||
| * The MIT License | ||
| * | ||
| * @copyright Copyright (c) 2025 TileDB, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| * | ||
| * @section DESCRIPTION | ||
| * | ||
| * This file contains unit tests and simple examples for the `INTERCEPT` | ||
| * capability. | ||
| */ | ||
|
|
||
| #include "tiledb/common/util/intercept.h" | ||
|
|
||
| #include <test/support/tdb_catch.h> | ||
|
|
||
| #include <barrier> | ||
| #include <thread> | ||
|
|
||
| // global variable for demonstrating intercept perfect forwarding | ||
| int global = 0; | ||
|
|
||
| namespace intercept { | ||
|
|
||
| DECLARE_INTERCEPT(my_library_function_entry); | ||
| DEFINE_INTERCEPT(my_library_function_entry); | ||
|
|
||
| // NB: DECLARE_INTERCEPT is not strictly necessary if everything is in the same | ||
| // file. The DECLARE also need not be in a header, the linker will resolve it | ||
| // correctly if the DECLARE is written in the unit test file and the DEFINE | ||
| // in the source file. | ||
| DEFINE_INTERCEPT(my_library_function_exit, int, std::string_view, int); | ||
|
|
||
| } // namespace intercept | ||
|
|
||
| /** | ||
| * Silly library function for demonstrating intercepts. | ||
| */ | ||
| int my_library_function(std::string_view arg) { | ||
| INTERCEPT(intercept::my_library_function_entry); | ||
|
|
||
| const int local = global++; | ||
| INTERCEPT(intercept::my_library_function_exit, global, arg, local); | ||
|
|
||
| return local; | ||
| } | ||
|
|
||
| /** | ||
| * Demonstrates using intercepts to log aspects of an execution | ||
| * which we might want to make assertions about in a test. | ||
| */ | ||
| TEST_CASE("Intercept log", "[intercept]") { | ||
| std::map<std::string, std::vector<std::pair<int, int>>> values; | ||
|
|
||
| { | ||
| auto cb = intercept::my_library_function_exit().and_also( | ||
| [&values](int snapshot_global, std::string_view arg, int local) { | ||
| values[std::string(arg)].emplace_back( | ||
| std::make_pair(snapshot_global, local)); | ||
| }); | ||
|
|
||
| global = 0; | ||
| my_library_function("foo"); | ||
| my_library_function("bar"); | ||
| my_library_function("foo"); | ||
|
|
||
| CHECK(values.size() == 2); | ||
| CHECK(values["foo"] == std::vector<std::pair<int, int>>{{1, 0}, {3, 2}}); | ||
| CHECK(values["bar"] == std::vector<std::pair<int, int>>{{2, 1}}); | ||
| } | ||
|
|
||
| // now that the callback is de-registered we shouldn't see anything | ||
| const decltype(values) snapshot = values; | ||
| my_library_function("bar"); | ||
| CHECK(values == snapshot); | ||
| } | ||
|
|
||
| /** | ||
| * Demonstrates using intercepts to simulate errors occurring | ||
| * within a library function. This can be very useful if it is known | ||
| * that the throwing of the error is causing problems, but the error | ||
| * itself is difficult to reproduce. | ||
| */ | ||
| TEST_CASE("Intercept simulate error", "[intercept]") { | ||
| // nothing happens | ||
| my_library_function("foo"); | ||
|
|
||
| // we can register a callback to make it throw | ||
| { | ||
| auto cb = intercept::my_library_function_entry().and_also( | ||
| []() { throw std::logic_error("intercept"); }); | ||
| CHECK_THROWS( | ||
| my_library_function("foo"), | ||
| Catch::Matchers::ContainsSubstring("intercept")); | ||
| } | ||
|
|
||
| // now the callback is de-registered, it should not throw again | ||
| my_library_function("foo"); | ||
| } | ||
|
|
||
| /** | ||
| * Demonstrates using intercepts to synchronize multiple threads, | ||
| * producing a deterministic behavior. | ||
| */ | ||
| TEST_CASE("Intercept synchronize", "[intercept]") { | ||
| global = 0; | ||
|
|
||
| std::barrier sync(2); | ||
|
|
||
| auto cb = intercept::my_library_function_exit().and_also( | ||
| [&sync](int snapshot_global, std::string_view, int) { | ||
| if (snapshot_global == 2) { | ||
| // waits for the main thread | ||
| sync.arrive_and_wait(); | ||
| // the main thread has arrived; wait for its signal to resume | ||
| sync.arrive_and_wait(); | ||
| } | ||
| }); | ||
|
|
||
| std::vector<int> tt_values; | ||
|
|
||
| std::thread tt([&tt_values]() { | ||
| tt_values.push_back(my_library_function("foo")); | ||
| tt_values.push_back(my_library_function("bar")); | ||
| tt_values.push_back(my_library_function("baz")); | ||
| tt_values.push_back(my_library_function("gub")); | ||
| }); | ||
|
|
||
| sync.arrive_and_wait(); | ||
|
|
||
| // the thread is waiting for a signal to continue, | ||
| // we can run arbitrary code while it does so | ||
| global = 100; | ||
|
|
||
| sync.arrive_and_wait(); | ||
|
|
||
| tt.join(); | ||
|
|
||
| // because we synchronized the two threads we should always | ||
| // see exactly the same values | ||
| CHECK(tt_values == std::vector<int>{0, 1, 100, 101}); | ||
| } |
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.