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

Remove static_cast from LruGc API, and others #1324

Draft
wants to merge 9 commits into
base: wuandy/LruGC
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
11 changes: 3 additions & 8 deletions app/src/include/firebase/internal/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,10 @@ struct AlignedStorage {
#define FIREBASE_DEPRECATED
#endif // defined(SWIG) || defined(DOXYGEN)

// TODO: Replace all usages of the FIREBASE_DEPRECATED macro with the C++14

Choose a reason for hiding this comment

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

⚠️ Lint warning: Missing username in TODO; it should look like "// TODO(my_username): Stuff."

// [[deprecated]] attribute.
#ifndef FIREBASE_DEPRECATED
#ifdef __GNUC__
#define FIREBASE_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define FIREBASE_DEPRECATED __declspec(deprecated)
#else
// We don't know how to mark functions as "deprecated" with this compiler.
#define FIREBASE_DEPRECATED
#endif
#define FIREBASE_DEPRECATED [[deprecated]]
#endif // FIREBASE_DEPRECATED

// Calculates the number of elements in an array.
Expand Down
3 changes: 2 additions & 1 deletion firestore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ set(common_SRCS
src/common/hard_assert_common.h
src/common/listener_registration.cc
src/common/load_bundle_task_progress.cc
src/common/local_cache_settings.cc
src/common/macros.h
src/common/query.cc
src/common/query_snapshot.cc
Expand Down Expand Up @@ -214,6 +213,8 @@ set(main_SRCS
src/main/listener_main.h
src/main/listener_registration_main.cc
src/main/listener_registration_main.h
src/main/local_cache_settings_main.cc
src/main/local_cache_settings_main.h
src/main/promise_factory_main.h
src/main/promise_main.h
src/main/query_main.cc
Expand Down
3 changes: 2 additions & 1 deletion firestore/integration_test_internal/src/bundle_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ TEST_F(BundleTest, LoadedDocumentsShouldNotBeGarbageCollectedRightAway) {
// This test really only makes sense with memory persistence, as disk
// persistence only ever lazily deletes data.
auto new_settings = db->settings();
new_settings.set_local_cache_settings(MemoryCacheSettings::Create());
new_settings.set_local_cache_settings(
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings()));
db->set_settings(new_settings);

auto bundle = CreateTestBundle(db);
Expand Down
45 changes: 28 additions & 17 deletions firestore/integration_test_internal/src/firestore_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "util/future_test_util.h"
#if !defined(__ANDROID__)
#include "Firestore/core/src/util/autoid.h"
#include "Firestore/core/src/util/warnings.h"
#else
#include "android/util_autoid.h"
#endif // !defined(__ANDROID__)
Expand Down Expand Up @@ -1533,7 +1534,9 @@ class FirestoreCacheConfigTest : public FirestoreIntegrationTest {
TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForMemoryCacheWorks) {
auto* db = TestFirestore("legacy_memory_cache");
auto settings = db->settings();
WITH_DEPRECATED_API(settings.set_persistence_enabled(false));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings.set_persistence_enabled(false);
SUPPRESS_END()
db->set_settings(std::move(settings));

VerifyCachedDocumentDeletedImmediately(db);
Expand All @@ -1542,7 +1545,9 @@ TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForMemoryCacheWorks) {
TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForPersistenceCacheWorks) {
auto* db = TestFirestore("legacy_persistent_cache");
auto settings = db->settings();
WITH_DEPRECATED_API(settings.set_persistence_enabled(true));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings.set_persistence_enabled(true);
SUPPRESS_END()
db->set_settings(std::move(settings));

VerifyCachedDocumentStaysAround(db);
Expand All @@ -1551,7 +1556,8 @@ TEST_F(FirestoreCacheConfigTest, LegacyCacheConfigForPersistenceCacheWorks) {
TEST_F(FirestoreCacheConfigTest, NewCacheConfigForMemoryCacheWorks) {
auto* db = TestFirestore("new_memory_cache");
auto settings = db->settings();
settings.set_local_cache_settings(MemoryCacheSettings::Create());
settings.set_local_cache_settings(
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings()));
db->set_settings(std::move(settings));

VerifyCachedDocumentDeletedImmediately(db);
Expand All @@ -1560,8 +1566,9 @@ TEST_F(FirestoreCacheConfigTest, NewCacheConfigForMemoryCacheWorks) {
TEST_F(FirestoreCacheConfigTest, NewCacheConfigForPersistenceCacheWorks) {
auto* db = TestFirestore("new_persistent_cache");
auto settings = db->settings();
settings.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024));
settings.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(50 * 1024 *
1024)));
db->set_settings(std::move(settings));

VerifyCachedDocumentStaysAround(db);
Expand All @@ -1571,29 +1578,33 @@ TEST_F(FirestoreCacheConfigTest, CannotMixNewAndLegacyCacheConfig) {
{
auto* db = TestFirestore("mixing_1");
auto settings = db->settings();
settings.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024));
settings.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(50 * 1024 *
1024)));

WITH_DEPRECATED_API(
EXPECT_THROW(settings.set_cache_size_bytes(0), std::logic_error));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
EXPECT_THROW(settings.set_cache_size_bytes(0), std::logic_error);
SUPPRESS_END()
}

{
auto* db = TestFirestore("mixing_2");
auto settings = db->settings();
WITH_DEPRECATED_API(settings.set_persistence_enabled(false));
EXPECT_THROW(
settings.set_local_cache_settings(MemoryCacheSettings::Create()),
std::logic_error);
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings.set_persistence_enabled(false);
SUPPRESS_END()
EXPECT_THROW(settings.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::MemoryCacheSettings())),
std::logic_error);
}
}

TEST_F(FirestoreCacheConfigTest, CanGetDocumentFromCacheWithMemoryLruGC) {
auto* db = TestFirestore("new_persistent_cache");
auto settings = db->settings();
settings.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryLruGCSettings::Create()));
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings(
LocalCacheSettings::MemoryCacheSettings::LruGCSettings())));
db->set_settings(std::move(settings));

VerifyCachedDocumentStaysAround(db);
Expand All @@ -1603,8 +1614,8 @@ TEST_F(FirestoreCacheConfigTest, CannotGetDocumentFromCacheFromMemoryEagerGC) {
auto* db = TestFirestore("new_persistent_cache");
auto settings = db->settings();
settings.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryEagerGCSettings::Create()));
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings(
LocalCacheSettings::MemoryCacheSettings::EagerGCSettings())));
db->set_settings(std::move(settings));

VerifyCachedDocumentDeletedImmediately(db);
Expand Down
148 changes: 119 additions & 29 deletions firestore/integration_test_internal/src/settings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
*/

#include <stdint.h>
#include <memory>
#include <string>
#include <vector>

#include "absl/types/optional.h"
#include "absl/types/variant.h"
#include "firebase/firestore.h"
#include "firebase/firestore/local_cache_settings.h"
#include "firebase_test_framework.h"

#include "gtest/gtest.h"

#if !defined(__ANDROID__)
#include "Firestore/core/src/util/warnings.h"
#else
#endif // !defined(__ANDROID__)

namespace firebase {
namespace firestore {
namespace {
Expand All @@ -33,39 +43,51 @@ TEST(SettingsTest, Equality) {
Settings settings1;
settings1.set_host("foo");
settings1.set_ssl_enabled(true);
WITH_DEPRECATED_API(settings1.set_persistence_enabled(true));
WITH_DEPRECATED_API(settings1.set_cache_size_bytes(kFiveMb));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings1.set_persistence_enabled(true);
settings1.set_cache_size_bytes(kFiveMb);
SUPPRESS_END()

Settings settings2;
settings2.set_host("bar");
settings2.set_ssl_enabled(true);
WITH_DEPRECATED_API(settings2.set_persistence_enabled(true));
WITH_DEPRECATED_API(settings2.set_cache_size_bytes(kFiveMb));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings2.set_persistence_enabled(true);
settings2.set_cache_size_bytes(kFiveMb);
SUPPRESS_END()

Settings settings3;
settings3.set_host("foo");
settings3.set_ssl_enabled(false);
WITH_DEPRECATED_API(settings3.set_persistence_enabled(true));
WITH_DEPRECATED_API(settings3.set_cache_size_bytes(kFiveMb));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings3.set_persistence_enabled(true);
settings3.set_cache_size_bytes(kFiveMb);
SUPPRESS_END()

Settings settings4;
settings4.set_host("foo");
settings4.set_ssl_enabled(true);
WITH_DEPRECATED_API(settings4.set_persistence_enabled(false));
WITH_DEPRECATED_API(settings4.set_cache_size_bytes(kFiveMb));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings4.set_persistence_enabled(false);
settings4.set_cache_size_bytes(kFiveMb);
SUPPRESS_END()

Settings settings5;
settings5.set_host("foo");
settings5.set_ssl_enabled(true);
WITH_DEPRECATED_API(settings5.set_persistence_enabled(true));
WITH_DEPRECATED_API(settings5.set_cache_size_bytes(kSixMb));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings5.set_persistence_enabled(true);
settings5.set_cache_size_bytes(kSixMb);
SUPPRESS_END()

// This is the same as settings4.
Settings settings6;
settings6.set_host("foo");
settings6.set_ssl_enabled(true);
WITH_DEPRECATED_API(settings6.set_persistence_enabled(false));
WITH_DEPRECATED_API(settings6.set_cache_size_bytes(kFiveMb));
SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
settings6.set_persistence_enabled(false);
settings6.set_cache_size_bytes(kFiveMb);
SUPPRESS_END()

EXPECT_TRUE(settings1 == settings1);
EXPECT_TRUE(settings6 == settings4);
Expand Down Expand Up @@ -103,60 +125,64 @@ TEST(SettingsTest, EqualityWithLocalCacheSettings) {
Settings settings1;
settings1.set_host("foo");
settings1.set_ssl_enabled(true);
settings1.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kFiveMb));
settings1.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kFiveMb)));

Settings settings2;
settings2.set_host("bar");
settings2.set_ssl_enabled(true);
settings2.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kFiveMb));
settings2.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kFiveMb)));

Settings settings3;
settings3.set_host("foo");
settings3.set_ssl_enabled(false);
settings3.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kFiveMb));
settings3.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kFiveMb)));

Settings settings4;
settings4.set_host("foo");
settings4.set_ssl_enabled(true);
settings4.set_local_cache_settings(MemoryCacheSettings::Create());
settings4.set_local_cache_settings(
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings()));

Settings settings5;
settings5.set_host("foo");
settings5.set_ssl_enabled(true);
settings5.set_local_cache_settings(
PersistentCacheSettings::Create().WithSizeBytes(kSixMb));
settings5.set_local_cache_settings(LocalCacheSettings(
LocalCacheSettings::PersistentCacheSettings().WithSizeBytes(kSixMb)));

Settings settings6;
settings6.set_host("foo");
settings6.set_ssl_enabled(true);
settings6.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryEagerGCSettings::Create()));
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings(
LocalCacheSettings::MemoryCacheSettings::EagerGCSettings())));

Settings settings7;
settings7.set_host("foo");
settings7.set_ssl_enabled(true);
settings7.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryLruGCSettings::Create().WithSizeBytes(kFiveMb)));
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings(
LocalCacheSettings::MemoryCacheSettings::LruGCSettings()
.WithSizeBytes(kFiveMb))));

Settings settings8;
settings8.set_host("foo");
settings8.set_ssl_enabled(true);
settings8.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryLruGCSettings::Create().WithSizeBytes(kSixMb)));
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings(
LocalCacheSettings::MemoryCacheSettings::LruGCSettings()
.WithSizeBytes(kSixMb))));

// Same as settings7
Settings settings9;
settings9.set_host("foo");
settings9.set_ssl_enabled(true);
settings9.set_local_cache_settings(
MemoryCacheSettings::Create().WithGarbageCollectorSettings(
MemoryLruGCSettings::Create().WithSizeBytes(kFiveMb)));
LocalCacheSettings(LocalCacheSettings::MemoryCacheSettings(
LocalCacheSettings::MemoryCacheSettings::LruGCSettings()
.WithSizeBytes(kFiveMb))));

EXPECT_TRUE(settings1 == settings1);
EXPECT_TRUE(settings6 == settings4);
Expand Down Expand Up @@ -193,6 +219,70 @@ TEST(SettingsTest, EqualityWithLocalCacheSettings) {
EXPECT_TRUE(settings7 != settings8);
}

TEST(SettingsTest, EqualityAssumptionsAboutSharedPtrAreCorrect) {
const std::shared_ptr<std::string> shared_ptr_empty1;
const std::shared_ptr<std::string> shared_ptr_empty2;
const auto shared_ptr1 = std::make_shared<std::string>("Test String");
const auto shared_ptr1a = shared_ptr1;
const auto shared_ptr1b = shared_ptr1a;
const auto shared_ptr2 = std::make_shared<std::string>("Test String");

EXPECT_TRUE(shared_ptr_empty1 == shared_ptr_empty1);
EXPECT_TRUE(shared_ptr_empty1 == shared_ptr_empty2);
EXPECT_TRUE(shared_ptr1 == shared_ptr1);
EXPECT_TRUE(shared_ptr1 == shared_ptr1a);
EXPECT_TRUE(shared_ptr1 == shared_ptr1b);

EXPECT_FALSE(shared_ptr_empty1 == shared_ptr1);
EXPECT_FALSE(shared_ptr1 == shared_ptr_empty1);
EXPECT_FALSE(shared_ptr1 == shared_ptr2);
}

TEST(SettingsTest, EqualityAssumptionsAboutOptionalAreCorrect) {
absl::optional<std::string> invalid_optional1;
absl::optional<std::string> invalid_optional2;
absl::optional<std::string> optional1("Test String");
absl::optional<std::string> optional2("Test String");
absl::optional<std::string> optional3("A different Test String");

EXPECT_TRUE(invalid_optional1 == invalid_optional1);
EXPECT_TRUE(invalid_optional1 == invalid_optional2);
EXPECT_TRUE(optional1 == optional1);
EXPECT_TRUE(optional1 == optional2);

EXPECT_FALSE(invalid_optional1 == optional1);
EXPECT_FALSE(optional1 == optional3);
}

TEST(SettingsTest, EqualityAssumptionsAboutVariantAreCorrect) {
absl::variant<std::string, std::vector<int>> invalid_variant1;
absl::variant<std::string, std::vector<int>> invalid_variant2;
absl::variant<std::string, std::vector<int>> variant_with_string1("zzyzx");
absl::variant<std::string, std::vector<int>> variant_with_string2("zzyzx");
absl::variant<std::string, std::vector<int>> variant_with_string3("abcde");
absl::variant<std::string, std::vector<int>> variant_with_vector1(
std::vector<int>{1, 2, 3});
absl::variant<std::string, std::vector<int>> variant_with_vector2(
std::vector<int>{1, 2, 3});
absl::variant<std::string, std::vector<int>> variant_with_vector3(
std::vector<int>{9, 8, 7});

EXPECT_TRUE(invalid_variant1 == invalid_variant1);
EXPECT_TRUE(invalid_variant1 == invalid_variant2);
EXPECT_TRUE(variant_with_string1 == variant_with_string1);
EXPECT_TRUE(variant_with_string1 == variant_with_string2);
EXPECT_TRUE(variant_with_vector1 == variant_with_vector1);
EXPECT_TRUE(variant_with_vector1 == variant_with_vector2);

EXPECT_FALSE(invalid_variant1 == variant_with_string1);
EXPECT_FALSE(variant_with_string1 == invalid_variant1);
EXPECT_FALSE(invalid_variant1 == variant_with_vector1);
EXPECT_FALSE(variant_with_vector1 == invalid_variant1);
EXPECT_FALSE(variant_with_string1 == variant_with_string3);
EXPECT_FALSE(variant_with_string1 == variant_with_vector1);
EXPECT_FALSE(variant_with_vector1 == variant_with_vector3);
}

} // namespace
} // namespace firestore
} // namespace firebase
Loading