Skip to content

Commit

Permalink
wait for all events.
Browse files Browse the repository at this point in the history
  • Loading branch information
shankarseal committed Mar 18, 2024
1 parent 3761336 commit c835336
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions tests/api_test/api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ntsecapi.h>
#include <thread>
#include <vector>
using namespace std::chrono_literals;

CATCH_REGISTER_LISTENER(_watchdog)

Expand Down Expand Up @@ -1229,12 +1230,19 @@ TEST_CASE("ioctl_stress", "[stress]")
bpf_object__close(object);
}

struct _ring_buffer_test_context
{
uint32_t event_count;
uint32_t expected_event_count;
std::promise<void> promise;
};

TEST_CASE("test_ringbuffer_wraparound", "[stress]")
{
// Load bindmonitor_ringbuf.sys.
struct bpf_object* object = nullptr;
fd_t program_fd;
uint32_t event_count = 0;
struct _ring_buffer_test_context context;
std::string app_id = "api_test.exe";
uint32_t thread_count = 2;
native_module_helper_t native_helper;
Expand All @@ -1254,14 +1262,21 @@ TEST_CASE("test_ringbuffer_wraparound", "[stress]")
REQUIRE(max_iterations % thread_count == 0);
uint32_t iterations_per_thread = max_iterations / thread_count;

// Initialize context.
context.event_count = 0;
context.expected_event_count = max_iterations;
auto ring_buffer_event_callback = context.promise.get_future();
// Subscribe to the ring buffer.
auto ring = ring_buffer__new(
process_map_fd,
[](void* ctx, void*, size_t) {
(*((uint32_t*)ctx))++;
struct _ring_buffer_test_context* context = reinterpret_cast<_ring_buffer_test_context*>(ctx);
if (++context->event_count == context->expected_event_count) {
context->promise.set_value();
}
return 0;
},
&event_count,
&context,
nullptr);

// Create 2 threads that invoke the program to trigger ring buffer events.
Expand Down Expand Up @@ -1290,7 +1305,8 @@ TEST_CASE("test_ringbuffer_wraparound", "[stress]")
for (auto& t : threads) {
t.join();
}
REQUIRE(event_count == max_iterations);
// Wait for 1 second for the ring buffer to receive all events.
REQUIRE(ring_buffer_event_callback.wait_for(1s) == std::future_status::ready);

// Unsubscribe from the ring buffer.
ring_buffer__free(ring);
Expand Down

0 comments on commit c835336

Please sign in to comment.