diff --git a/driver/host_selector/round_robin_host_selector.cpp b/driver/host_selector/round_robin_host_selector.cpp index ab33aa6c..8a22b613 100644 --- a/driver/host_selector/round_robin_host_selector.cpp +++ b/driver/host_selector/round_robin_host_selector.cpp @@ -15,6 +15,7 @@ #include "round_robin_host_selector.h" #include +#include #include #include @@ -73,7 +74,12 @@ HostInfo RoundRobinHostSelector::GetHost(std::vector hosts, bool is_wr CreateCacheEntries(selection, properties); const std::string cluster_id_key = selection.at(0).GetHost(); - const std::shared_ptr cluster_info = round_robin_cache_.Get(cluster_id_key); + const std::optional> cached_info = + round_robin_cache_.Get(cluster_id_key); + if (!cached_info.has_value() || !cached_info.value()) { + throw std::runtime_error("No round robin cluster info cached for host: " + cluster_id_key); + } + const std::shared_ptr& cluster_info = cached_info.value(); const std::shared_ptr last_host = cluster_info->last_host; int last_host_idx = NO_HOST_IDX; @@ -143,15 +149,20 @@ void RoundRobinHostSelector::CreateCacheEntries(const std::vector& hos return RoundRobinHostSelector::round_robin_cache_.Find(host.GetHost()); }); + // An entry may expire between the Find above and the Get below, so treat a missing entry the same as having no cached entry at all. + std::optional> cached_info; + if (!hosts_with_cached_entry.empty()) { + cached_info = round_robin_cache_.Get(hosts_with_cached_entry.at(0).GetHost()); + } + // Update cache entries, else create new cache - if (hosts_with_cached_entry.empty()) { + if (!cached_info.has_value() || !cached_info.value()) { const std::shared_ptr cluster_info = std::make_shared(); UpdatePropsDefaultWeight(cluster_info, props); UpdatePropsHostWeight(cluster_info, props); UpdateCache(hosts, cluster_info); } else { - const std::string cluster_id_key = hosts_with_cached_entry.at(0).GetHost(); - const std::shared_ptr cluster_info = round_robin_cache_.Get(cluster_id_key); + const std::shared_ptr& cluster_info = cached_info.value(); if (CheckPropChange(cluster_info->last_default_weight_str, RoundRobinProperty::DEFAULT_WEIGHT_KEY, props)) { cluster_info->default_weight = 1; UpdatePropsDefaultWeight(cluster_info, props); diff --git a/driver/plugin/blue_green/blue_green_plugin.cpp b/driver/plugin/blue_green/blue_green_plugin.cpp index ee47599f..54ff48fb 100644 --- a/driver/plugin/blue_green/blue_green_plugin.cpp +++ b/driver/plugin/blue_green/blue_green_plugin.cpp @@ -65,7 +65,7 @@ SQLRETURN BlueGreenPlugin::Connect( // Get provider lock to check if there is an existing status // This is to allow the plugin to increment the provider if it hasn't already std::unique_lock lock_guard(provider_lock_); - this->blue_green_status_ = status_map_->Get(this->blue_green_id_); + this->blue_green_status_ = status_map_->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); if (this->blue_green_status_.GetCurrentPhase().GetPhase() == BlueGreenPhase::UNKNOWN) { LOG(INFO) << "[FALLBACK-A] Default connection, no status found: " << this->blue_green_id_; LOG(INFO) << "Default connection, no status found: " << this->blue_green_id_; @@ -120,7 +120,7 @@ SQLRETURN BlueGreenPlugin::Connect( rc = (*route_itr)->Connect(dbc, HostInfo(conn_host), this->odbc_helper_, BlueGreenPlugin::status_map_); LOG(INFO) << "[ROUTE-LOOP] iteration=" << loop_iteration << ", returned rc=" << std::to_string(rc); if (!SQL_SUCCEEDED(rc)) { - this->blue_green_status_ = status_map_->Get(this->blue_green_id_); + this->blue_green_status_ = status_map_->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); BlueGreenPhase refetched_phase = this->blue_green_status_.GetCurrentPhase(); if (refetched_phase.GetPhase() == BlueGreenPhase::UNKNOWN) { this->end_time_ = std::chrono::steady_clock::now(); @@ -182,7 +182,7 @@ SQLRETURN BlueGreenPlugin::Execute( this->ResetRoutingTiming(); this->InitProvider(); - this->blue_green_status_ = status_map_->Get(this->blue_green_id_); + this->blue_green_status_ = status_map_->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); if (this->blue_green_status_.GetCurrentPhase().GetPhase() == BlueGreenPhase::UNKNOWN) { LOG(INFO) << "Default execution, no status found: " << this->blue_green_id_; return ExecuteNext(StatementHandle, StatementText, TextLength); @@ -220,7 +220,7 @@ SQLRETURN BlueGreenPlugin::Execute( rc = (*route_itr)->Execute(stmt, this->odbc_helper_, BlueGreenPlugin::status_map_); LOG(INFO) << "Execute route returned: " << std::to_string(rc); if (!SQL_SUCCEEDED(rc)) { - this->blue_green_status_ = status_map_->Get(this->blue_green_id_); + this->blue_green_status_ = status_map_->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); if (this->blue_green_status_.GetCurrentPhase().GetPhase() == BlueGreenPhase::UNKNOWN) { this->end_time_ = std::chrono::steady_clock::now(); LOG(WARNING) << "Default execution, statuses reset, routes cleared for role: " << host_role.ToString() << ", host: " << conn_host; diff --git a/driver/plugin/blue_green/blue_green_status_provider.cpp b/driver/plugin/blue_green/blue_green_status_provider.cpp index 48a8ef27..519d0683 100644 --- a/driver/plugin/blue_green/blue_green_status_provider.cpp +++ b/driver/plugin/blue_green/blue_green_status_provider.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include static constexpr int HASH_MULTIPLIER = 31; @@ -447,12 +448,12 @@ std::vector> BlueGreenStatusProvider::AddSub for (const auto& itr : this->role_by_host_map_->GetMapCopy()) { const std::string host = itr.first; const BlueGreenRole role = itr.second; - const std::pair host_pair = this->corresponding_nodes_->Get(host); - if (role != BlueGreenRole::SOURCE || host_pair.first.GetHost().empty()) { + const std::optional> host_pair = this->corresponding_nodes_->Get(host); + if (role != BlueGreenRole::SOURCE || !host_pair.has_value() || host_pair->first.GetHost().empty()) { continue; } - const HostInfo blue_host = host_pair.first; - const std::string blue_ip = this->host_ip_map_->Get(blue_host.GetHost()); + const HostInfo blue_host = host_pair->first; + const std::string blue_ip = this->host_ip_map_->Get(blue_host.GetHost()).value_or(""); const HostInfo blue_ip_host = blue_ip.empty() ? blue_host : HostInfo(blue_ip, blue_host.GetPort(), blue_host.GetHostState(), blue_host.GetHostRole()); @@ -590,13 +591,14 @@ BlueGreenStatus BlueGreenStatusProvider::GetStatusOfPost() { // NOLINT(misc-no- void BlueGreenStatusProvider::CreatePostRouting(std::vector>& connect_routing) { if (!this->blue_dns_update_completed_ || !this->all_green_nodes_changed_) { for (const auto& [host, role] : this->role_by_host_map_->GetMapCopy()) { - if (role != BlueGreenRole::SOURCE || !this->corresponding_nodes_->Contains(host)) { + const std::optional> node_pair = this->corresponding_nodes_->Get(host); + if (role != BlueGreenRole::SOURCE || !node_pair.has_value()) { continue; } const std::string blue_host = host; const bool is_blue_host_instance = RdsUtils::IsRdsInstance(blue_host); - const auto [blue_host_info, green_host_info] = this->corresponding_nodes_->Get(blue_host); + const auto& [blue_host_info, green_host_info] = *node_pair; if (green_host_info.GetHost().empty()) { connect_routing.push_back(std::make_shared(blue_host, role, this->blue_green_id_)); @@ -607,7 +609,7 @@ void BlueGreenStatusProvider::CreatePostRouting(std::vectorhost_ip_map_->Get(green_host); + const std::string green_host_ip = this->host_ip_map_->Get(green_host).value_or(""); const HostInfo green_ip_host_info = HostInfo(green_host_ip, green_host_info.GetPort(), green_host_info.GetHostState(), green_host_info.GetHostRole()); // Check if green ghost connected with blue (non-prefixed) IAM host name @@ -653,7 +655,7 @@ void BlueGreenStatusProvider::CreatePostRouting(std::vectorhost_ip_map_->Get(green_host), interim_status.port_); + const HostInfo green_ip_host_info = HostInfo(this->host_ip_map_->Get(green_host).value_or(""), interim_status.port_); std::vector iam_hosts; if (this->IsAlreadySuccessfullyConnected(green_host, blue_host)) { @@ -715,7 +717,7 @@ void BlueGreenStatusProvider::RegisterIamHost(const std::string& connect_host, c } { const std::lock_guard lock_guard(this->iam_mutex_); - std::set iam_success_connects = this->iam_host_success_connects_map_->Get(connect_host); + std::set iam_success_connects = this->iam_host_success_connects_map_->Get(connect_host).value_or(std::set{}); iam_success_connects.insert(iam_host); this->iam_host_success_connects_map_->InsertOrAssign(connect_host, iam_success_connects); } @@ -743,7 +745,8 @@ bool BlueGreenStatusProvider::IsAlreadySuccessfullyConnected(std::string connect if (!this->iam_host_success_connects_map_) { return false; } - return this->iam_host_success_connects_map_->Get(connect_host).contains(iam_host); + const std::optional> iam_success_connects = this->iam_host_success_connects_map_->Get(connect_host); + return iam_success_connects.has_value() && iam_success_connects->contains(iam_host); } void BlueGreenStatusProvider::StorePhaseTime(BlueGreenPhase phase) { diff --git a/driver/plugin/blue_green/routing/base_routing.cpp b/driver/plugin/blue_green/routing/base_routing.cpp index 897b1917..a7952b46 100644 --- a/driver/plugin/blue_green/routing/base_routing.cpp +++ b/driver/plugin/blue_green/routing/base_routing.cpp @@ -34,14 +34,14 @@ void BaseRouting::Delay( const std::chrono::steady_clock::time_point end = start + delay_ms; const std::chrono::milliseconds min_delay = delay_ms < MIN_SLEEP_MS ? delay_ms : MIN_SLEEP_MS; - BlueGreenStatus cached_status = status_cache->Get(id); + BlueGreenStatus cached_status = status_cache->Get(id).value_or(BlueGreenStatus{}); if (cached_status.GetCurrentPhase().GetPhase() == BlueGreenPhase::UNKNOWN) { std::this_thread::sleep_for(delay_ms); } else { do { std::this_thread::sleep_for(min_delay); - } while ((cached_status = status_cache->Get(id)) == status && GetCurrTime() < end); + } while ((cached_status = status_cache->Get(id).value_or(BlueGreenStatus{})) == status && GetCurrTime() < end); } } diff --git a/driver/plugin/blue_green/routing/connect/suspend_connect_routing.cpp b/driver/plugin/blue_green/routing/connect/suspend_connect_routing.cpp index 4c1b3a41..53265f64 100644 --- a/driver/plugin/blue_green/routing/connect/suspend_connect_routing.cpp +++ b/driver/plugin/blue_green/routing/connect/suspend_connect_routing.cpp @@ -32,11 +32,11 @@ SQLRETURN SuspendConnectRouting::Connect( const std::chrono::steady_clock::time_point start_time = GetCurrTime(); const std::chrono::steady_clock::time_point end_time = start_time + timeout; - BlueGreenStatus cached_status = status_cache->Get(this->blue_green_id_); + BlueGreenStatus cached_status = status_cache->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); while (GetCurrTime() <= end_time && cached_status.GetCurrentPhase().GetPhase() == BlueGreenPhase::IN_PROGRESS) { this->Delay(SLEEP_DURATION_MS, cached_status, status_cache, this->blue_green_id_); - cached_status = status_cache->Get(this->blue_green_id_); + cached_status = status_cache->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); }; if (cached_status.GetCurrentPhase().GetPhase() == BlueGreenPhase::IN_PROGRESS) { diff --git a/driver/plugin/blue_green/routing/connect/suspend_until_node_found_connect_routing.cpp b/driver/plugin/blue_green/routing/connect/suspend_until_node_found_connect_routing.cpp index c88becdf..dbf4b271 100644 --- a/driver/plugin/blue_green/routing/connect/suspend_until_node_found_connect_routing.cpp +++ b/driver/plugin/blue_green/routing/connect/suspend_until_node_found_connect_routing.cpp @@ -24,7 +24,7 @@ SQLRETURN SuspendUntilNodeFoundConnectRouting::Connect( std::shared_ptr odbc_helper, std::shared_ptr> status_cache) { - BlueGreenStatus cached_status = status_cache->Get(this->blue_green_id_); + BlueGreenStatus cached_status = status_cache->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); const std::string host = info.GetHost(); const std::map> nodes = cached_status.GetCorrespondingNodes(); @@ -34,7 +34,7 @@ SQLRETURN SuspendUntilNodeFoundConnectRouting::Connect( while (GetCurrTime() <= end_time && cached_status.GetCurrentPhase().GetPhase() != BlueGreenPhase::COMPLETED && !nodes.contains(host)) { this->Delay(SLEEP_DURATION_MS, cached_status, status_cache, this->blue_green_id_); - cached_status = status_cache->Get(this->blue_green_id_); + cached_status = status_cache->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); }; if (cached_status.GetCurrentPhase().GetPhase() == BlueGreenPhase::COMPLETED) { diff --git a/driver/plugin/blue_green/routing/execute/suspend_execute_routing.cpp b/driver/plugin/blue_green/routing/execute/suspend_execute_routing.cpp index 4fbc34c0..5035e9c8 100644 --- a/driver/plugin/blue_green/routing/execute/suspend_execute_routing.cpp +++ b/driver/plugin/blue_green/routing/execute/suspend_execute_routing.cpp @@ -32,11 +32,11 @@ SQLRETURN SuspendExecuteRouting::Execute( const std::chrono::steady_clock::time_point start_time = GetCurrTime(); const std::chrono::steady_clock::time_point end_time = start_time + timeout; - BlueGreenStatus cached_status = status_cache->Get(this->blue_green_id_); + BlueGreenStatus cached_status = status_cache->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); while (GetCurrTime() <= end_time && cached_status.GetCurrentPhase().GetPhase() == BlueGreenPhase::IN_PROGRESS) { this->Delay(SLEEP_DURATION_MS, cached_status, status_cache, this->blue_green_id_); - cached_status = status_cache->Get(this->blue_green_id_); + cached_status = status_cache->Get(this->blue_green_id_).value_or(BlueGreenStatus{}); }; if (cached_status.GetCurrentPhase().GetPhase() == BlueGreenPhase::IN_PROGRESS) { diff --git a/driver/plugin/custom_endpoint/custom_endpoint_monitor.cpp b/driver/plugin/custom_endpoint/custom_endpoint_monitor.cpp index ae649ea9..2df3160d 100644 --- a/driver/plugin/custom_endpoint/custom_endpoint_monitor.cpp +++ b/driver/plugin/custom_endpoint/custom_endpoint_monitor.cpp @@ -26,6 +26,7 @@ #include "../../util/rds_utils.h" #include +#include SlidingCacheMap CustomEndpointMonitor::endpoint_cache_; @@ -114,7 +115,7 @@ void CustomEndpointMonitor::Run() { } filter.endpoint_type = endpoint_info.GetEndpointType(); - const HostFilter cached_filter = endpoint_cache_.Get(endpoint_identifier_); + const std::optional cached_filter = endpoint_cache_.Get(endpoint_identifier_); if (cached_filter != filter) { LOG(INFO) << "Detected change in custom endpoint info for " << endpoint_identifier_; if (const std::shared_ptr service = this->plugin_service_.lock()) { @@ -148,7 +149,7 @@ void CustomEndpointMonitor::Run() { } bool CustomEndpointMonitor::HasInfo() { - return endpoint_cache_.Get(endpoint_identifier_) != HostFilter{}; + return endpoint_cache_.Get(endpoint_identifier_).has_value(); } void CustomEndpointMonitor::IncreaseDelay() { diff --git a/driver/util/concurrent_map.h b/driver/util/concurrent_map.h index 89b6808d..e1e5b16a 100644 --- a/driver/util/concurrent_map.h +++ b/driver/util/concurrent_map.h @@ -17,6 +17,7 @@ #include #include +#include template class ConcurrentMap { @@ -56,12 +57,12 @@ class ConcurrentMap { map_.insert(other.begin(), other.end()); }; - Value Get(const Key& key) const { + std::optional Get(const Key& key) const { const std::lock_guard lock(mutex_); if (const auto itr = map_.find(key); itr != map_.end()) { return itr->second; } - return {}; + return std::nullopt; }; std::map GetMapCopy() const { diff --git a/driver/util/plugin_service.cpp b/driver/util/plugin_service.cpp index 09a0acad..93f8a2ea 100644 --- a/driver/util/plugin_service.cpp +++ b/driver/util/plugin_service.cpp @@ -183,7 +183,7 @@ void PluginService::ForceRefreshHosts(bool verify_writer, std::chrono::milliseco } std::vector PluginService::GetHosts() { - return topology_map_->Get(this->cluster_id_); + return topology_map_->Get(this->cluster_id_).value_or(std::vector{}); } void PluginService::SetHosts(const std::vector& hosts) { @@ -191,8 +191,8 @@ void PluginService::SetHosts(const std::vector& hosts) { } std::vector PluginService::GetFilteredHosts() { - std::vector hosts = topology_map_->Get(this->cluster_id_); - HostFilter host_filter = host_filter_map_->Get(this->cluster_id_); + std::vector hosts = topology_map_->Get(this->cluster_id_).value_or(std::vector{}); + HostFilter host_filter = host_filter_map_->Get(this->cluster_id_).value_or(HostFilter{}); if (host_filter.allowed_host_ids.empty() && host_filter.blocked_host_ids.empty()) { return hosts; diff --git a/driver/util/rds_lib_loader.h b/driver/util/rds_lib_loader.h index 03348080..9fe50945 100644 --- a/driver/util/rds_lib_loader.h +++ b/driver/util/rds_lib_loader.h @@ -82,18 +82,8 @@ class RdsLibLoader { template RdsLibResult RdsLibLoader::CallFunction(const std::string& func_name, Args... args) { - FUNC_HANDLE driver_function = nullptr; // Try retrieving from cache - { - if (function_cache_->Contains(func_name)) { - try { - driver_function = function_cache_->Get(func_name); - } catch (const std::out_of_range&) { - // Should not happen but done to satisfy clang-tidy - driver_function = nullptr; - } - } - } + FUNC_HANDLE driver_function = function_cache_->Get(func_name).value_or(nullptr); // Cache miss if (!driver_function) { driver_function = GetFunction(func_name); diff --git a/driver/util/sliding_cache_map.h b/driver/util/sliding_cache_map.h index 5099c335..43e8fdfb 100644 --- a/driver/util/sliding_cache_map.h +++ b/driver/util/sliding_cache_map.h @@ -18,6 +18,7 @@ #include #include #include +#include template struct CacheEntry { @@ -65,7 +66,7 @@ class SlidingCacheMap { cache_[key] = CacheEntry{value, expiry_time, ms_ttl}; } - V Get(const K& key) { + std::optional Get(const K& key) { const std::lock_guard lock(cache_lock_); const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); if (const auto itr = cache_.find(key); itr != cache_.end()) { @@ -78,7 +79,7 @@ class SlidingCacheMap { // Expired, remove from cache cache_.erase(itr); } - return {}; + return std::nullopt; } bool Find(const K& key) { diff --git a/test/unit_test/concurrent_map_test.cpp b/test/unit_test/concurrent_map_test.cpp index 7be1292c..eaf82897 100644 --- a/test/unit_test/concurrent_map_test.cpp +++ b/test/unit_test/concurrent_map_test.cpp @@ -14,6 +14,7 @@ #include "../../driver/util/concurrent_map.h" +#include #include #include @@ -52,6 +53,12 @@ TEST_F(ConcurrentMapTest, sync_basic_operations) { // Erase cache.Erase(CACHE_KEY_A); EXPECT_FALSE(cache.Contains(CACHE_KEY_A)); + EXPECT_EQ(std::nullopt, cache.Get(CACHE_KEY_A)); + + // A stored default-constructed value is distinguishable from a miss + cache.InsertOrAssign(CACHE_KEY_A, 0); + EXPECT_EQ(0, cache.Get(CACHE_KEY_A)); + cache.Erase(CACHE_KEY_A); // Size EXPECT_EQ(0, cache.Size()); diff --git a/test/unit_test/sliding_cache_map_test.cpp b/test/unit_test/sliding_cache_map_test.cpp index f5b61d55..69ff5017 100644 --- a/test/unit_test/sliding_cache_map_test.cpp +++ b/test/unit_test/sliding_cache_map_test.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -46,8 +47,8 @@ TEST_F(SlidingCacheMapTest, put_cache) { SlidingCacheMap cache; cache.Put(CACHE_KEY_A, CACHE_VAL_A); cache.Put(CACHE_KEY_B, CACHE_VAL_B, CACHE_EXP_LONG); - EXPECT_STREQ(CACHE_VAL_A.c_str(), cache.Get(CACHE_KEY_A).c_str()); - EXPECT_STREQ(CACHE_VAL_B.c_str(), cache.Get(CACHE_KEY_B).c_str()); + EXPECT_EQ(CACHE_VAL_A, cache.Get(CACHE_KEY_A)); + EXPECT_EQ(CACHE_VAL_B, cache.Get(CACHE_KEY_B)); } TEST_F(SlidingCacheMapTest, put_cache_update) { @@ -56,30 +57,38 @@ TEST_F(SlidingCacheMapTest, put_cache_update) { cache.Put(CACHE_KEY_A, CACHE_VAL_A); cache.Put(CACHE_KEY_B, CACHE_VAL_B, CACHE_EXP_SHORT); EXPECT_EQ(2, cache.Size()); - EXPECT_STREQ(CACHE_VAL_B.c_str(), cache.Get(CACHE_KEY_B).c_str()); + EXPECT_EQ(CACHE_VAL_B, cache.Get(CACHE_KEY_B)); cache.Put(CACHE_KEY_B, CACHE_VAL_B, CACHE_EXP_LONG); std::this_thread::sleep_for(CACHE_EXP_MID); EXPECT_EQ(2, cache.Size()); - EXPECT_STREQ(CACHE_VAL_B.c_str(), cache.Get(CACHE_KEY_B).c_str()); + EXPECT_EQ(CACHE_VAL_B, cache.Get(CACHE_KEY_B)); } TEST_F(SlidingCacheMapTest, get_cache_hit) { SlidingCacheMap cache; cache.Put(CACHE_KEY_A, CACHE_VAL_A); - EXPECT_STREQ(CACHE_VAL_A.c_str(), cache.Get(CACHE_KEY_A).c_str()); + EXPECT_EQ(CACHE_VAL_A, cache.Get(CACHE_KEY_A)); } TEST_F(SlidingCacheMapTest, get_cache_miss) { SlidingCacheMap cache; - EXPECT_STREQ(CACHE_EMPTY.c_str(), cache.Get(CACHE_KEY_A).c_str()); + EXPECT_EQ(std::nullopt, cache.Get(CACHE_KEY_A)); +} + +TEST_F(SlidingCacheMapTest, get_stored_default_value_is_a_hit) { + SlidingCacheMap cache; + cache.Put(CACHE_KEY_A, CACHE_EMPTY); + // A stored default-constructed value is distinguishable from a miss + EXPECT_EQ(CACHE_EMPTY, cache.Get(CACHE_KEY_A)); + EXPECT_EQ(std::nullopt, cache.Get(CACHE_KEY_B)); } TEST_F(SlidingCacheMapTest, get_cache_expire) { SlidingCacheMap cache; cache.Put(CACHE_KEY_A, CACHE_VAL_A, CACHE_EXP_SHORT); std::this_thread::sleep_for(CACHE_EXP_MID); - EXPECT_STREQ(CACHE_EMPTY.c_str(), cache.Get(CACHE_KEY_A).c_str()); + EXPECT_EQ(std::nullopt, cache.Get(CACHE_KEY_A)); } TEST_F(SlidingCacheMapTest, find_cache_miss) { @@ -136,7 +145,7 @@ TEST_F(SlidingCacheMapTest, ttl_update_find) { // Should be valid due to earlier `find()` updates TTL EXPECT_EQ(1, cache.Size()); - EXPECT_STREQ(CACHE_VAL_A.c_str(), cache.Get(CACHE_KEY_A).c_str()); + EXPECT_EQ(CACHE_VAL_A, cache.Get(CACHE_KEY_A)); } TEST_F(SlidingCacheMapTest, ttl_update_get) { @@ -148,12 +157,12 @@ TEST_F(SlidingCacheMapTest, ttl_update_get) { std::this_thread::sleep_for(CACHE_EXP_SHORT); // Touch to refresh TTL - EXPECT_STREQ(CACHE_VAL_A.c_str(), cache.Get(CACHE_KEY_A).c_str()); + EXPECT_EQ(CACHE_VAL_A, cache.Get(CACHE_KEY_A)); // Sleep again combined with previous to expire original put std::this_thread::sleep_for(CACHE_EXP_SHORT); // Should be valid due to earlier `get()` updates TTL EXPECT_EQ(1, cache.Size()); - EXPECT_STREQ(CACHE_VAL_A.c_str(), cache.Get(CACHE_KEY_A).c_str()); + EXPECT_EQ(CACHE_VAL_A, cache.Get(CACHE_KEY_A)); }