Skip to content

Commit

Permalink
Fix initial config load when auto poll enabled with results from cache (
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein authored May 7, 2024
1 parent 2c6fa3a commit 95bb58a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 4.1.1 - 2024-05-06
### Fixed
- Fix initial config JSON load when auto poll enabled with results from cache.

## 4.1.0 - 2024-04-03
### Changed
- Rename `SettingsValue` to correct `SettingValue`
Expand Down
2 changes: 1 addition & 1 deletion lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const version = '4.1.0';
const version = '4.1.1';
const configJsonCacheVersion = 'v2';
const configJsonName = 'config_v6.json';
final DateTime distantPast = DateTime.utc(1970, 01, 01);
Expand Down
27 changes: 12 additions & 15 deletions lib/src/fetch/config_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,20 @@ class ConfigService with ContinuousFutureSynchronizer {

Future<SettingResult> getSettings() async {
final mode = _mode;
var threshold = distantPast;
var preferCached = _initialized;
if (mode is LazyLoadingMode) {
final entry = await _fetchIfOlder(
DateTime.now().toUtc().subtract(mode.cacheRefreshInterval));
return !entry.first.isEmpty
? SettingResult(
settings: entry.first.config.entries,
fetchTime: entry.first.fetchTime)
: SettingResult.empty;
} else {
final entry =
await _fetchIfOlder(distantPast, preferCached: _initialized);
return !entry.first.isEmpty
? SettingResult(
settings: entry.first.config.entries,
fetchTime: entry.first.fetchTime)
: SettingResult.empty;
threshold = DateTime.now().toUtc().subtract(mode.cacheRefreshInterval);
preferCached = false;
} else if (!_initialized && mode is AutoPollingMode) {
threshold = DateTime.now().toUtc().subtract(mode.autoPollInterval);
}
final entry = await _fetchIfOlder(threshold, preferCached: preferCached);
return !entry.first.isEmpty
? SettingResult(
settings: entry.first.config.entries,
fetchTime: entry.first.fetchTime)
: SettingResult.empty;
}

Future<RefreshResult> refresh() async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: configcat_client
description: >-
Dart (Flutter) SDK for ConfigCat. ConfigCat is a hosted feature flag service that lets you manage feature toggles across frontend, backend, mobile, desktop apps.
version: 4.1.0
version: 4.1.1
homepage: https://configcat.com/docs/sdk-reference/dart
repository: https://github.com/configcat/dart-sdk
issue_tracker: https://github.com/configcat/dart-sdk/issues
Expand Down
24 changes: 24 additions & 0 deletions test/config_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,30 @@ void main() {
// Cleanup
service.close();
});

test('polls when cache expired', () async {
// Arrange
final cached = createTestEntryWithTime({'key': true},
DateTime.now().toUtc().subtract(const Duration(seconds: 5)))
.serialize();
when(cache.read(any)).thenAnswer((_) => Future.value(cached));

final service = createService(
PollingMode.autoPoll(autoPollInterval: const Duration(seconds: 1)));

testAdapter.enqueueResponse(
getPath(), 200, createTestConfig({'key': false}).toJson());

// Act
final result = await service.getSettings();

// Assert
expect(result.settings['key']?.settingValue.booleanValue, isFalse);
expect(testAdapter.capturedRequests.length, 1);

// Cleanup
service.close();
});
});

group('Lazy Loading Tests', () {
Expand Down

0 comments on commit 95bb58a

Please sign in to comment.