Skip to content

Commit 12086e0

Browse files
committed
main tests pass.
1 parent 2021a27 commit 12086e0

File tree

4 files changed

+119
-6
lines changed

4 files changed

+119
-6
lines changed

firestore/integration_test_internal/src/firestore_test.cc

+89
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#include <future>
2121
#include <memory>
2222
#include <stdexcept>
23+
#include "firebase/firestore/document_snapshot.h"
24+
#include "firebase/firestore/field_value.h"
25+
#include "firebase/firestore/local_cache_settings.h"
26+
#include "firebase/firestore/map_field_value.h"
27+
#include "firebase/firestore/source.h"
2328

2429
#if defined(__ANDROID__)
2530
#include "android/firestore_integration_test_android.h"
@@ -1499,6 +1504,90 @@ TEST_F(FirestoreIntegrationTest, ClearPersistenceWhileRunningFails) {
14991504
EXPECT_EQ(await_clear_persistence.error(), Error::kErrorFailedPrecondition);
15001505
}
15011506

1507+
TEST_F(FirestoreIntegrationTest, LegacyCacheConfigForMemoryCacheWorks) {
1508+
auto* db = TestFirestore("legacy_memory_cache");
1509+
auto settings = db->settings();
1510+
settings.set_persistence_enabled(false);
1511+
db->set_settings(std::move(settings));
1512+
1513+
Await(db->Document("rooms/eros")
1514+
.Set(MapFieldValue{{"desc", FieldValue::String("eros")}}));
1515+
1516+
auto get_future = db->Document("rooms/eros").Get(Source::kCache);
1517+
const DocumentSnapshot* snapshot = Await(get_future);
1518+
EXPECT_EQ(get_future.status(), FutureStatus::kFutureStatusComplete);
1519+
EXPECT_FALSE(snapshot->is_valid());
1520+
snapshot->id();
1521+
}
1522+
1523+
TEST_F(FirestoreIntegrationTest, LegacyCacheConfigForPersistenceCacheWorks) {
1524+
auto* db = TestFirestore("legacy_persistent_cache");
1525+
auto settings = db->settings();
1526+
settings.set_persistence_enabled(true);
1527+
db->set_settings(std::move(settings));
1528+
1529+
Await(db->Document("rooms/eros")
1530+
.Set(MapFieldValue{{"desc", FieldValue::String("eros")}}));
1531+
1532+
auto get_future = db->Document("rooms/eros").Get(Source::kCache);
1533+
const DocumentSnapshot* snapshot = Await(get_future);
1534+
EXPECT_EQ(get_future.status(), FutureStatus::kFutureStatusComplete);
1535+
EXPECT_TRUE(snapshot->is_valid());
1536+
EXPECT_THAT(snapshot->GetData(),
1537+
ContainerEq(MapFieldValue{{"desc", FieldValue::String("eros")}}));
1538+
}
1539+
1540+
TEST_F(FirestoreIntegrationTest, NewCacheConfigForMemoryCacheWorks) {
1541+
auto* db = TestFirestore("new_memory_cache");
1542+
auto settings = db->settings();
1543+
settings.set_local_cache_settings(MemoryCacheSettings::Create());
1544+
db->set_settings(std::move(settings));
1545+
1546+
Await(db->Document("rooms/eros")
1547+
.Set(MapFieldValue{{"desc", FieldValue::String("eros")}}));
1548+
1549+
auto get_future = db->Document("rooms/eros").Get(Source::kCache);
1550+
const DocumentSnapshot* snapshot = Await(get_future);
1551+
EXPECT_EQ(get_future.status(), FutureStatus::kFutureStatusComplete);
1552+
EXPECT_FALSE(snapshot->is_valid());
1553+
}
1554+
1555+
TEST_F(FirestoreIntegrationTest, NewCacheConfigForPersistenceCacheWorks) {
1556+
auto* db = TestFirestore("new_persistent_cache");
1557+
auto settings = db->settings();
1558+
settings.set_local_cache_settings(
1559+
PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024));
1560+
db->set_settings(std::move(settings));
1561+
1562+
Await(db->Document("rooms/eros")
1563+
.Set(MapFieldValue{{"desc", FieldValue::String("eros")}}));
1564+
1565+
auto get_future = db->Document("rooms/eros").Get(Source::kCache);
1566+
const DocumentSnapshot* snapshot = Await(get_future);
1567+
EXPECT_EQ(get_future.status(), FutureStatus::kFutureStatusComplete);
1568+
EXPECT_TRUE(snapshot->is_valid());
1569+
EXPECT_THAT(snapshot->GetData(),
1570+
ContainerEq(MapFieldValue{{"desc", FieldValue::String("eros")}}));
1571+
}
1572+
1573+
TEST_F(FirestoreIntegrationTest, CannotMixNewAndLegacyCacheConfig) {
1574+
{
1575+
auto* db = TestFirestore("mixing_1");
1576+
auto settings = db->settings();
1577+
settings.set_local_cache_settings(
1578+
PersistentCacheSettings::Create().WithSizeBytes(50 * 1024 * 1024));
1579+
EXPECT_THROW(settings.set_cache_size_bytes(0), std::logic_error);
1580+
}
1581+
{
1582+
auto* db = TestFirestore("mixing_2");
1583+
auto settings = db->settings();
1584+
settings.set_persistence_enabled(false);
1585+
EXPECT_THROW(
1586+
settings.set_local_cache_settings(MemoryCacheSettings::Create()),
1587+
std::logic_error);
1588+
}
1589+
}
1590+
15021591
// Note: this test only exists in C++.
15031592
TEST_F(FirestoreIntegrationTest, DomainObjectsReferToSameFirestoreInstance) {
15041593
EXPECT_EQ(TestFirestore(), TestFirestore()->Document("foo/bar").firestore());

firestore/src/common/settings.cc

+19-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "Firestore/core/src/util/hard_assert.h"
2525
#include "app/meta/move.h"
2626
#include "firebase/firestore/local_cache_settings.h"
27+
#include "firestore/src/common/exception_common.h"
2728

2829
#if !defined(__ANDROID__)
2930
#include "Firestore/core/src/util/executor.h"
@@ -75,7 +76,12 @@ std::shared_ptr<LocalCacheSettings> Settings::local_cache_settings() const {
7576
}
7677

7778
void Settings::set_local_cache_settings(const LocalCacheSettings& cache) {
78-
HARD_ASSERT(!used_legacy_cache_settings_, "");
79+
if (used_legacy_cache_settings_) {
80+
SimpleThrowIllegalState(
81+
"Cannot mix set_local_cache_settings() with legacy cache api like "
82+
"set_persistence_enabled() or set_cache_size_bytes()");
83+
}
84+
7985
if (cache.kind() == api::LocalCacheSettings::Kind::kPersistent) {
8086
local_cache_settings_ = std::make_shared<PersistentCacheSettings>(
8187
*static_cast<const PersistentCacheSettings&>(cache).settings_internal_);
@@ -86,13 +92,23 @@ void Settings::set_local_cache_settings(const LocalCacheSettings& cache) {
8692
}
8793

8894
void Settings::set_persistence_enabled(bool enabled) {
89-
HARD_ASSERT(local_cache_settings() == nullptr, "");
95+
if (local_cache_settings_ != nullptr) {
96+
SimpleThrowIllegalState(
97+
"Cannot mix legacy cache api set_persistence_enabled() with new cache "
98+
"api set_local_cache_settings()");
99+
}
100+
90101
persistence_enabled_ = enabled;
91102
used_legacy_cache_settings_ = true;
92103
}
93104

94105
void Settings::set_cache_size_bytes(int64_t value) {
95-
HARD_ASSERT(local_cache_settings() == nullptr, "");
106+
if (local_cache_settings_ != nullptr) {
107+
SimpleThrowIllegalState(
108+
"Cannot mix legacy cache api set_cache_size_bytes() with new cache api "
109+
"set_local_cache_settings()");
110+
}
111+
96112
cache_size_bytes_ = value;
97113
used_legacy_cache_settings_ = true;
98114
}

firestore/src/include/firebase/firestore/local_cache_settings.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class PersistentCacheSettings final : public LocalCacheSettings {
5252
PersistentCacheSettings(const PersistentCacheSettingsInternal& other);
5353

5454
PersistentCacheSettings WithSizeBytes(int64_t size) const;
55-
const CoreCacheSettings& core_cache_settings() const override;
5655
api::LocalCacheSettings::Kind kind() const override {
5756
return api::LocalCacheSettings::Kind::kPersistent;
5857
}
@@ -62,6 +61,8 @@ class PersistentCacheSettings final : public LocalCacheSettings {
6261

6362
PersistentCacheSettings();
6463

64+
const CoreCacheSettings& core_cache_settings() const override;
65+
6566
std::unique_ptr<PersistentCacheSettingsInternal> settings_internal_;
6667
};
6768

@@ -70,7 +71,6 @@ class MemoryCacheSettings final : public LocalCacheSettings {
7071
static MemoryCacheSettings Create();
7172
~MemoryCacheSettings();
7273

73-
const CoreCacheSettings& core_cache_settings() const override;
7474
api::LocalCacheSettings::Kind kind() const override {
7575
return api::LocalCacheSettings::Kind::kMemory;
7676
}
@@ -81,6 +81,7 @@ class MemoryCacheSettings final : public LocalCacheSettings {
8181
friend class Settings;
8282

8383
MemoryCacheSettings();
84+
const CoreCacheSettings& core_cache_settings() const override;
8485

8586
std::unique_ptr<MemoryCacheSettingsInternal> settings_internal_;
8687
};

firestore/src/main/firestore_main.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ Settings FirestoreInternal::settings() const {
191191
result.set_ssl_enabled(from.ssl_enabled());
192192
result.set_persistence_enabled(from.persistence_enabled());
193193
result.set_cache_size_bytes(from.cache_size_bytes());
194+
// TODO(wuandy): This line should be deleted when legacy cache config is
195+
// removed.
196+
result.used_legacy_cache_settings_ = false;
194197

195198
return result;
196199
}
@@ -199,7 +202,11 @@ void FirestoreInternal::set_settings(Settings from) {
199202
api::Settings settings;
200203
settings.set_host(std::move(from.host()));
201204
settings.set_ssl_enabled(from.is_ssl_enabled());
202-
if (!from.used_legacy_cache_settings_) {
205+
// TODO(wuandy): Checking `from.local_cache_settings_` is required, because
206+
// FirestoreInternal::settings() overrides used_legacy_cache_settings_. All
207+
// this special logic should go away when legacy cache config is removed.
208+
if (!from.used_legacy_cache_settings_ &&
209+
from.local_cache_settings_ != nullptr) {
203210
settings.set_local_cache_settings(
204211
from.local_cache_settings()->core_cache_settings());
205212
} else {

0 commit comments

Comments
 (0)