Repository navigation
Fix bucket name being reused between instances of different buckets #496
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
base: main
Are you sure you want to change the base?
Changes from all commits
8069727
b36b3f3
d36a71e
acfd67e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,29 +8,31 @@ import 'firebase_http_file_service.dart'; | |||||
| class FirebaseCacheManager extends CacheManager { | ||||||
| static const key = 'firebaseCache'; | ||||||
|
|
||||||
| static final FirebaseCacheManager _instance = FirebaseCacheManager._( | ||||||
| retryOptions: _retryOptions, | ||||||
| bucket: _bucket, | ||||||
| ); | ||||||
|
|
||||||
| static RetryOptions? _retryOptions; | ||||||
|
|
||||||
| static String? _bucket; | ||||||
| static final Map<String, FirebaseCacheManager> _instances = {}; | ||||||
|
|
||||||
| factory FirebaseCacheManager({RetryOptions? retryOptions, String? bucket}) { | ||||||
| _bucket = bucket; | ||||||
| _retryOptions = retryOptions; | ||||||
| return _instance; | ||||||
| final cacheKey = bucket ?? key; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: the raw bucket name is now the Fix: namespace the key, for example:
Suggested change
(and update the test expectations to match). |
||||||
| return _instances.putIfAbsent( | ||||||
| cacheKey, | ||||||
| () => FirebaseCacheManager._( | ||||||
| key: cacheKey, | ||||||
| retryOptions: retryOptions, | ||||||
| bucket: bucket, | ||||||
| ), | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| FirebaseCacheManager._({RetryOptions? retryOptions, String? bucket}) | ||||||
| : super( | ||||||
| Config( | ||||||
| key, | ||||||
| fileService: FirebaseHttpFileService( | ||||||
| retryOptions: retryOptions, | ||||||
| bucket: bucket, | ||||||
| ), | ||||||
| ), | ||||||
| ); | ||||||
| FirebaseCacheManager._({ | ||||||
| required String key, | ||||||
| RetryOptions? retryOptions, | ||||||
| String? bucket, | ||||||
| }) : super( | ||||||
| Config( | ||||||
| key, | ||||||
| fileService: FirebaseHttpFileService( | ||||||
| retryOptions: retryOptions, | ||||||
| bucket: bucket, | ||||||
| ), | ||||||
| ), | ||||||
| ); | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,59 @@ | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_cache_manager_firebase/flutter_cache_manager_firebase.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| const pathProviderChannel = MethodChannel('plugins.flutter.io/path_provider'); | ||
| late Directory tempDir; | ||
|
|
||
| setUp(() { | ||
| tempDir = Directory.systemTemp.createTempSync('fcm_firebase_test_'); | ||
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | ||
| .setMockMethodCallHandler(pathProviderChannel, (call) async { | ||
| return tempDir.path; | ||
| }); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (optional): |
||
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | ||
| .setMockMethodCallHandler(pathProviderChannel, null); | ||
| if (tempDir.existsSync()) { | ||
| tempDir.deleteSync(recursive: true); | ||
| } | ||
| }); | ||
|
|
||
| test('FirebaseCacheManager key is set', () { | ||
| expect(FirebaseCacheManager.key, 'firebaseCache'); | ||
| }); | ||
|
|
||
| test('FirebaseCacheManager returns separate instances per bucket', () { | ||
| final bucketA = FirebaseCacheManager(bucket: 'bucket-a'); | ||
| final bucketAAgain = FirebaseCacheManager(bucket: 'bucket-a'); | ||
| final bucketB = FirebaseCacheManager(bucket: 'bucket-b'); | ||
| final defaultInstance = FirebaseCacheManager(); | ||
|
|
||
| expect(identical(bucketA, bucketAAgain), isTrue); | ||
| expect(identical(bucketA, bucketB), isFalse); | ||
| expect(identical(bucketA, defaultInstance), isFalse); | ||
|
|
||
| expect(bucketA.config.cacheKey, 'bucket-a'); | ||
| expect( | ||
| (bucketA.config.fileService as FirebaseHttpFileService).bucket, | ||
| 'bucket-a', | ||
| ); | ||
| expect(bucketB.config.cacheKey, 'bucket-b'); | ||
| expect( | ||
| (bucketB.config.fileService as FirebaseHttpFileService).bucket, | ||
| 'bucket-b', | ||
| ); | ||
| expect(defaultInstance.config.cacheKey, FirebaseCacheManager.key); | ||
| expect( | ||
| (defaultInstance.config.fileService as FirebaseHttpFileService).bucket, | ||
| isNull, | ||
| ); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (optional): the factory now returns one shared instance per bucket, and
retryOptionsonly takes effect on the first call for that bucket; later calls with different options get the existing instance unchanged. Neither is documented. AGENTS.md asks for "Public API documented with///doc comments where applicable". Fix: add a///comment on the factory that says both.