Skip to content

Commit

Permalink
Make listen() follow connection moves. (#911)
Browse files Browse the repository at this point in the history
When moving a `connection`, move its listeners as well.
  • Loading branch information
jtv authored Dec 22, 2024
1 parent 6f02798 commit 289ce9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pqxx::encrypt_password(char const user[], char const password[])
pqxx::connection::connection(connection &&rhs) :
m_conn{rhs.m_conn},
m_notice_waiters{std::move(rhs.m_notice_waiters)},
m_notification_handlers{std::move(rhs.m_notification_handlers)},
m_unique_id{rhs.m_unique_id}
{
rhs.check_movable();
Expand Down Expand Up @@ -251,11 +252,13 @@ pqxx::connection &pqxx::connection::operator=(connection &&rhs)
check_overwritable();
rhs.check_movable();

// Close our old connection, if any.
close();

m_conn = std::exchange(rhs.m_conn, nullptr);
m_unique_id = rhs.m_unique_id;
m_notice_waiters = std::move(rhs.m_notice_waiters);
m_notification_handlers = std::move(rhs.m_notification_handlers);

return *this;
}
Expand Down
19 changes: 19 additions & 0 deletions test/unit/test_notification.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,24 @@ void test_notifications_do_not_come_in_until_commit()
}


void test_notification_handlers_follow_connection_move()
{
auto const chan{"pqxx-chan3782"};
pqxx::connection cx1;
pqxx::connection *got{nullptr};
cx1.listen(chan, [&got](pqxx::notification n){ got = &n.conn; });
pqxx::connection cx2{std::move(cx1)};
pqxx::connection cx3;
cx3 = std::move(cx2);
pqxx::work tx{cx3};
tx.notify(chan);
tx.commit();
cx3.await_notification(3);
PQXX_CHECK(got != nullptr, "Did not get notified.");
PQXX_CHECK(got == &cx3, "Notification got the wrong connection.");
}


PQXX_REGISTER_TEST(test_notification_classic);
PQXX_REGISTER_TEST(test_notification_to_self_arrives_after_commit);
PQXX_REGISTER_TEST(test_notification_has_payload);
Expand All @@ -436,4 +454,5 @@ PQXX_REGISTER_TEST(test_notification_goes_to_right_handler);
PQXX_REGISTER_TEST(test_listen_on_same_channel_overwrites);
PQXX_REGISTER_TEST(test_empty_notification_handler_disables);
PQXX_REGISTER_TEST(test_notifications_do_not_come_in_until_commit);
PQXX_REGISTER_TEST(test_notification_handlers_follow_connection_move);
} // namespace

0 comments on commit 289ce9a

Please sign in to comment.