Skip to content

Commit

Permalink
Merge pull request #694 from kleunen/retained_topic_map_fail
Browse files Browse the repository at this point in the history
Failed to reset value in retained_map on erase
  • Loading branch information
redboltz authored Oct 18, 2020
2 parents a86229f + e7cfa17 commit 4a48022
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
8 changes: 6 additions & 2 deletions test/retained_topic_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ BOOST_AUTO_TEST_CASE(general) {
map.insert_or_update("a/b/c", "123");
map.insert_or_update("a/b", "123");

map.erase("a/b/c");
BOOST_TEST(map.erase("a") == 0);
BOOST_TEST(map.erase("a") == 0);

BOOST_TEST(map.erase("a/b/c") == 1);
BOOST_TEST(map.size() != 1);

map.erase("a/b");
BOOST_TEST(map.erase("a/b") == 1);
BOOST_TEST(map.size() == 1);

std::vector<std::string> values = {
Expand Down Expand Up @@ -139,6 +142,7 @@ BOOST_AUTO_TEST_CASE(erase_upper_first) {
map.insert_or_update("a/b", "2");

auto e1 = map.erase("a/b"); // erase upper first

BOOST_TEST(e1 == 1);
{
std::set<std::string> match {
Expand Down
27 changes: 21 additions & 6 deletions test/retained_topic_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,24 @@ class retained_topic_map {
}

// Remove a value at the specified subscription path
bool erase_topic(MQTT_NS::string_view topic) {
size_t erase_topic(MQTT_NS::string_view topic) {
auto path = find_topic(topic);

for (auto entry : boost::adaptors::reverse(path)) {
--entry->second.count;
if (entry->second.count == 0) {
map.erase(entry->first);
// Reset the value if there is actually something stored
if(!path.empty() && path.back()->second.value) {
path.back()->second.value = MQTT_NS::nullopt;

for (auto entry : boost::adaptors::reverse(path)) {
--entry->second.count;
if (entry->second.count == 0) {
map.erase(entry->first);
}
}

return 1;
}

return !path.empty();
return 0;
}

public:
Expand Down Expand Up @@ -243,6 +250,14 @@ class retained_topic_map {
// Get the size of the map
std::size_t size() const { return map.size(); }

// Dump debug information
template<typename Output>
void dump(Output &out) {
for (auto const& i: map) {
out << i.first.first << " " << i.first.second << " " << (i.second.value ? "init" : "-") << " " << i.second.count << std::endl;
}
}

};

#endif // MQTT_RETAINED_TOPIC_MAP_HPP

0 comments on commit 4a48022

Please sign in to comment.