Skip to content

Commit 6b6729b

Browse files
authored
Merge pull request #473 from firebase/feature/rc_rest_update
[RemoteConfig] Fix the fetch interval bug
2 parents 3803777 + 1adbd29 commit 6b6729b

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

remote_config/integration_test/src/integration_test.cc

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,32 @@ static const firebase::remote_config::ConfigKeyValueVariant kServerValue[] = {
170170
{"TestDefaultOnly", firebase::Variant::FromMutableString(
171171
"Default value that won't be overridden")}};
172172

173-
static Future<void> SetDefaultsV2(RemoteConfig* rc) {
173+
static Future<void> SetDefaults(RemoteConfig* rc) {
174174
size_t default_count = FIREBASE_ARRAYSIZE(defaults);
175175
return rc->SetDefaults(defaults, default_count);
176176
}
177177

178+
static Future<void> SetDefaultConfigSettings(RemoteConfig* rc) {
179+
firebase::remote_config::ConfigSettings defaultConfigSettings;
180+
return rc->SetConfigSettings(defaultConfigSettings);
181+
}
182+
183+
static Future<void> SetZeroIntervalConfigSettings(RemoteConfig* rc) {
184+
firebase::remote_config::ConfigSettings zeroIntervalConfigSettings;
185+
zeroIntervalConfigSettings.minimum_fetch_interval_in_milliseconds = 0;
186+
return rc->SetConfigSettings(zeroIntervalConfigSettings);
187+
}
188+
178189
// Test cases below.
179190

180191
TEST_F(FirebaseRemoteConfigTest, TestInitializeAndTerminate) {
181192
// Already tested via SetUp() and TearDown().
182193
}
183194

184-
TEST_F(FirebaseRemoteConfigTest, TestSetDefaultsV2) {
195+
TEST_F(FirebaseRemoteConfigTest, TestSetDefault) {
185196
ASSERT_NE(rc_, nullptr);
186197

187-
EXPECT_TRUE(WaitForCompletion(SetDefaultsV2(rc_), "SetDefaultsV2"));
198+
EXPECT_TRUE(WaitForCompletion(SetDefaults(rc_), "SetDefaults"));
188199

189200
bool validated_defaults = true;
190201
firebase::remote_config::ValueInfo value_info;
@@ -243,10 +254,10 @@ TEST_F(FirebaseRemoteConfigTest, TestSetDefaultsV2) {
243254
}
244255
}
245256

246-
TEST_F(FirebaseRemoteConfigTest, TestGetKeysV2) {
257+
TEST_F(FirebaseRemoteConfigTest, TestGetKeys) {
247258
ASSERT_NE(rc_, nullptr);
248259

249-
EXPECT_TRUE(WaitForCompletion(SetDefaultsV2(rc_), "SetDefaultsV2"));
260+
EXPECT_TRUE(WaitForCompletion(SetDefaults(rc_), "SetDefaults"));
250261

251262
std::vector<std::string> keys = rc_->GetKeys();
252263
EXPECT_THAT(
@@ -265,7 +276,7 @@ TEST_F(FirebaseRemoteConfigTest, TestGetKeysV2) {
265276
TEST_F(FirebaseRemoteConfigTest, TestGetAll) {
266277
ASSERT_NE(rc_, nullptr);
267278

268-
EXPECT_TRUE(WaitForCompletion(SetDefaultsV2(rc_), "SetDefaultsV2"));
279+
EXPECT_TRUE(WaitForCompletion(SetDefaults(rc_), "SetDefaults"));
269280
EXPECT_TRUE(WaitForCompletion(
270281
RunWithRetry([](RemoteConfig* rc) { return rc->Fetch(); }, rc_),
271282
"Fetch"));
@@ -288,10 +299,10 @@ TEST_F(FirebaseRemoteConfigTest, TestGetAll) {
288299
TestBoolean true
289300
TestString This is a string
290301
*/
291-
TEST_F(FirebaseRemoteConfigTest, TestFetchV2) {
302+
TEST_F(FirebaseRemoteConfigTest, TestFetch) {
292303
ASSERT_NE(rc_, nullptr);
293304

294-
EXPECT_TRUE(WaitForCompletion(SetDefaultsV2(rc_), "SetDefaultsV2"));
305+
EXPECT_TRUE(WaitForCompletion(SetDefaults(rc_), "SetDefaults"));
295306
EXPECT_TRUE(WaitForCompletion(
296307
RunWithRetry([](RemoteConfig* rc) { return rc->Fetch(); }, rc_),
297308
"Fetch"));
@@ -334,4 +345,31 @@ TEST_F(FirebaseRemoteConfigTest, TestFetchV2) {
334345
sizeof(kExpectedBlobServerValue)));
335346
}
336347

348+
TEST_F(FirebaseRemoteConfigTest, TestFetchInterval) {
349+
ASSERT_NE(rc_, nullptr);
350+
EXPECT_TRUE(WaitForCompletion(
351+
RunWithRetry([](RemoteConfig* rc) { return rc->Fetch(); }, rc_),
352+
"Fetch"));
353+
EXPECT_TRUE(WaitForCompletion(rc_->Activate(), "Activate"));
354+
uint64_t current_fetch_time = rc_->GetInfo().fetch_time;
355+
// Making sure the config settings's fetch interval is 12 hours
356+
EXPECT_TRUE(WaitForCompletion(SetDefaultConfigSettings(rc_),
357+
"SetDefaultConfigSettings"));
358+
// Second fetch, should respect fetch interval and don't change data.
359+
EXPECT_TRUE(WaitForCompletion(
360+
RunWithRetry([](RemoteConfig* rc) { return rc->Fetch(); }, rc_),
361+
"Fetch"));
362+
EXPECT_EQ(current_fetch_time, rc_->GetInfo().fetch_time);
363+
// Update fetch interval to 0
364+
EXPECT_TRUE(WaitForCompletion(SetZeroIntervalConfigSettings(rc_),
365+
"SetZeroIntervalConfigSettings"));
366+
LogDebug("Current Fetch Interval: %lld",
367+
rc_->GetConfigSettings().minimum_fetch_interval_in_milliseconds);
368+
// Third fetch, this should operate the real fetch and update the fetch time.
369+
EXPECT_TRUE(WaitForCompletion(
370+
RunWithRetry([](RemoteConfig* rc) { return rc->Fetch(); }, rc_),
371+
"Fetch"));
372+
EXPECT_NE(current_fetch_time, rc_->GetInfo().fetch_time);
373+
}
374+
337375
} // namespace firebase_testapp_automated

remote_config/src/desktop/remote_config_desktop.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ Future<bool> RemoteConfigInternal::FetchAndActivate() {
139139
const auto future_handle =
140140
future_impl_.SafeAlloc<bool>(kRemoteConfigFnFetchAndActivate);
141141

142-
cache_expiration_in_seconds_ = kDefaultCacheExpiration;
142+
cache_expiration_in_seconds_ =
143+
config_settings_.minimum_fetch_interval_in_milliseconds;
143144

144145
uint64_t milliseconds_since_epoch =
145146
std::chrono::duration_cast<std::chrono::milliseconds>(

remote_config/src/include/firebase/remote_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,6 @@ class RemoteConfig {
511511
// Clean up RemoteConfig instance.
512512
void DeleteInternal();
513513

514-
uint64_t GetConfigFetchInterval();
515-
516514
/// The Firebase App this remote config is connected to.
517515
App* app_;
518516

remote_config/src/remote_config.cc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ Future<bool> RemoteConfig::FetchAndActivateLastResult() {
131131
return internal_->FetchAndActivateLastResult();
132132
}
133133

134-
Future<void> RemoteConfig::Fetch() { return Fetch(GetConfigFetchInterval()); }
134+
Future<void> RemoteConfig::Fetch() {
135+
return Fetch(GetConfigSettings().minimum_fetch_interval_in_milliseconds);
136+
}
135137

136138
Future<void> RemoteConfig::Fetch(uint64_t cache_expiration_in_seconds) {
137139
return internal_->Fetch(cache_expiration_in_seconds);
@@ -229,14 +231,5 @@ std::map<std::string, Variant> RemoteConfig::GetAll() {
229231
// TODO(b/147143718): Change to a more descriptive name.
230232
const ConfigInfo RemoteConfig::GetInfo() { return internal_->GetInfo(); }
231233

232-
uint64_t RemoteConfig::GetConfigFetchInterval() {
233-
uint64_t cache_time =
234-
GetConfigSettings().minimum_fetch_interval_in_milliseconds;
235-
if (cache_time == 0) {
236-
cache_time = kDefaultCacheExpiration;
237-
}
238-
return cache_time;
239-
}
240-
241234
} // namespace remote_config
242235
} // namespace firebase

0 commit comments

Comments
 (0)