Skip to content
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

RCORE-2172 Fix race condition in "unregister connection change listener" test while listener is being unregistered #7824

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions test/object-store/sync/session/connection_change_notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,54 @@ TEST_CASE("sync: Connection state changes", "[sync][session][connection change]"
return sessions_are_connected(*session);
});

std::atomic<bool> listener1_called(false);
std::atomic<size_t> listener1_call_cnt(0);
std::atomic<bool> listener2_called(false);

auto token1 = session->register_connection_change_callback(
[&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener1_called = true;
listener1_call_cnt = listener1_call_cnt + 1;
});
session->unregister_connection_change_callback(token1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd guess that the race is because there is spurious network connection changes between the calls to register and unregister. If that is the case, what you could do is get the count of times called after unregistering the callback, and check that it is the same after calling log_out() below.

// One call may have been in progress when unregistered
REQUIRE(listener1_call_cnt <= 1);
size_t listener1_called = listener1_call_cnt;

session->register_connection_change_callback([&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener2_called = true;
});
user->log_out();
REQUIRE(sessions_are_disconnected(*session));
// ensure callback 1 was not called anymore
REQUIRE(listener1_call_cnt == listener1_called);
REQUIRE(listener2_called);
}

SECTION("unregister connection change listener during callback") {
uint64_t token1;
std::atomic<int> listener1_call_cnt(0);
std::atomic<bool> listener2_called(false);
auto session = sync_session(
user, "/connection-state-changes-3", [](auto, auto) {}, SyncSessionStopPolicy::AfterChangesUploaded);
token1 = session->register_connection_change_callback(
[&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener1_call_cnt = listener1_call_cnt + 1;
session->unregister_connection_change_callback(token1);
});

EventLoop::main().run_until([&] {
return sessions_are_active(*session);
});
EventLoop::main().run_until([&] {
return sessions_are_connected(*session);
});

session->register_connection_change_callback([&](SyncSession::ConnectionState, SyncSession::ConnectionState) {
listener2_called = true;
});

user->log_out();
REQUIRE(sessions_are_disconnected(*session));
REQUIRE(listener1_called == false);
REQUIRE(listener2_called == true);
REQUIRE(listener1_call_cnt == 1); // Only called once before unregister
REQUIRE(listener2_called);
}
}
Loading