Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,50 @@ void runInstanceTests() {
await FirebaseFirestore.setLoggingEnabled(true);
await FirebaseFirestore.setLoggingEnabled(false);
});

testWidgets(
'Settings() - `persistenceEnabled` & `cacheSizeBytes` with acceptable number',
(widgetTester) async {
FirebaseFirestore.instance.settings =
const Settings(persistenceEnabled: true, cacheSizeBytes: 10000000);
// Used to trigger settings
await FirebaseFirestore.instance
.collection('flutter-tests')
.doc('new-doc')
.set(
{'some': 'data'},
);
});

testWidgets(
'Settings() - `persistenceEnabled` & `cacheSizeBytes` with `Settings.CACHE_SIZE_UNLIMITED`',
(widgetTester) async {
FirebaseFirestore.instance.settings = const Settings(
persistenceEnabled: true,
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
);
// Used to trigger settings
await FirebaseFirestore.instance
.collection('flutter-tests')
.doc('new-doc')
.set(
{'some': 'data'},
);
});

testWidgets(
'Settings() - `persistenceEnabled` & without `cacheSizeBytes`',
(widgetTester) async {
FirebaseFirestore.instance.settings =
const Settings(persistenceEnabled: true);
// Used to trigger settings
await FirebaseFirestore.instance
.collection('flutter-tests')
.doc('new-doc')
.set(
{'some': 'data'},
);
});
},
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,20 @@ - (FIRFirestoreSettings *)FIRFirestoreSettings {

if (![values[@"persistenceEnabled"] isEqual:[NSNull null]]) {
bool persistEnabled = [((NSNumber *)values[@"persistenceEnabled"]) boolValue];
int size = [((NSNumber *)values[@"cacheSizeBytes"]) intValue];

// This is the maximum amount of cache allowed. We use the same number on android.
// This now causes an exception: kFIRFirestoreCacheSizeUnlimited
NSNumber *size = @104857600;

if (![values[@"cacheSizeBytes"] isEqual:[NSNull null]]) {
NSNumber *cacheSizeBytes = ((NSNumber *)values[@"cacheSizeBytes"]);
if ([cacheSizeBytes intValue] != -1) {
size = cacheSizeBytes;
}
}

if (persistEnabled) {
NSNumber *unlimitedPersistence =
[NSNumber numberWithLongLong:kFIRFirestoreCacheSizeUnlimited];
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this constant created by us or was it exposed by the Firebase SDK? Should we ask the Firebase iOS SDK explanations about it if it's exposed by the native sdk?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's one of the SDKs constants. Worth asking in the next meeting 👍

settings.cacheSettings = [[FIRPersistentCacheSettings alloc]
initWithSizeBytes:size == -1 ? unlimitedPersistence
: ((NSNumber *)values[@"cacheSizeBytes"])];
settings.cacheSettings = [[FIRPersistentCacheSettings alloc] initWithSizeBytes:size];
} else {
settings.cacheSettings = [[FIRMemoryCacheSettings alloc]
initWithGarbageCollectorSettings:[[FIRMemoryLRUGCSettings alloc] init]];
Expand Down
31 changes: 31 additions & 0 deletions packages/cloud_firestore/cloud_firestore/test/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,36 @@ void main() {
);
});
});

group('Settings()', () {
test('Test the assert for setting `cacheSizeBytes` minimum and maximum',
() {
void configureCache(int? cacheSizeBytes) {
assert(
cacheSizeBytes == null ||
cacheSizeBytes == Settings.CACHE_SIZE_UNLIMITED ||
(cacheSizeBytes >= 1048576 && cacheSizeBytes <= 104857600),
'Cache size, if specified, must be either CACHE_SIZE_UNLIMITED or between 1048576 bytes (inclusive) and 104857600 bytes (inclusive).',
);
}

// Happy paths
expect(() => configureCache(null), returnsNormally);
expect(
() => configureCache(Settings.CACHE_SIZE_UNLIMITED),
returnsNormally,
);
expect(() => configureCache(5000000), returnsNormally);
expect(() => configureCache(1048577), returnsNormally);
expect(() => configureCache(104857600), returnsNormally);
expect(() => configureCache(104857500), returnsNormally);

// Assertion triggers
expect(() => configureCache(1), throwsA(isA<AssertionError>()));
expect(() => configureCache(1000), throwsA(isA<AssertionError>()));
expect(() => configureCache(200000000), throwsA(isA<AssertionError>()));
expect(() => configureCache(500000), throwsA(isA<AssertionError>()));
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,23 @@ class Settings {
bool? sslEnabled,
int? cacheSizeBytes,
bool? ignoreUndefinedProperties,
}) =>
Settings(
persistenceEnabled: persistenceEnabled ?? this.persistenceEnabled,
host: host ?? this.host,
sslEnabled: sslEnabled ?? this.sslEnabled,
cacheSizeBytes: cacheSizeBytes ?? this.cacheSizeBytes,
ignoreUndefinedProperties:
ignoreUndefinedProperties ?? this.ignoreUndefinedProperties,
);
}) {
assert(
cacheSizeBytes == null ||
cacheSizeBytes == CACHE_SIZE_UNLIMITED ||
// 1mb and 100mb. minimum and maximum inclusive range.
(cacheSizeBytes >= 1048576 && cacheSizeBytes <= 104857600),
'Cache size must be between 1048576 bytes (inclusive) and 104857600 bytes (inclusive)');

return Settings(
persistenceEnabled: persistenceEnabled ?? this.persistenceEnabled,
host: host ?? this.host,
sslEnabled: sslEnabled ?? this.sslEnabled,
cacheSizeBytes: cacheSizeBytes ?? this.cacheSizeBytes,
ignoreUndefinedProperties:
ignoreUndefinedProperties ?? this.ignoreUndefinedProperties,
);
}

@override
bool operator ==(Object other) =>
Expand Down