-
-
Notifications
You must be signed in to change notification settings - Fork 500
Expand file tree
/
Copy pathcache_store_test.dart
More file actions
447 lines (354 loc) · 14.8 KB
/
cache_store_test.dart
File metadata and controls
447 lines (354 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_cache_manager/src/cache_store.dart';
import 'package:flutter_cache_manager/src/storage/cache_object.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'helpers/config_extensions.dart';
import 'helpers/mock_cache_info_repository.dart';
import 'helpers/test_configuration.dart';
void main() {
late int fileId;
late String fileName;
late String fileUrl;
late DateTime validTill;
late CacheObject cacheObject;
setUp(() {
fileId = 666;
fileName = 'testimage.png';
fileUrl = 'baseflow.com/test.png';
validTill = DateTime(2017, 9, 7, 17, 30);
cacheObject = CacheObject(
fileUrl,
relativePath: fileName,
id: fileId,
validTill: validTill,
);
});
group('Retrieving files from store', () {
test('Store should return null when file not cached', () async {
var repo = MockCacheInfoRepository();
when(repo.get(any)).thenAnswer((_) => Future.value(null));
var store = CacheStore(createTestConfig());
expect(await store.getFile('This is a test'), null);
});
test('Store should return FileInfo when file is cached', () async {
var config = createTestConfig();
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, DateTime.now());
var tempDir = createDir();
await (await tempDir).childFile('testimage.png').create();
var store = CacheStore(config);
expect(await store.getFile('baseflow.com/test.png'), isNotNull);
});
test('Store should return null if file is not cached', () async {
var config = createTestConfig();
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, validTill,
id: fileId, key: fileUrl);
var tempDir = createDir();
await (await tempDir).childFile('testimage.png').create();
final store = CacheStore(config);
final results = Future.wait([
store.removeCachedFile(cacheObject),
store.removeCachedFile(cacheObject),
]);
expect(
() => results,
returnsNormally,
);
});
test('Store should return null when file is no longer cached', () async {
var repo = MockCacheInfoRepository();
when(repo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
validTill: clock.now().add(const Duration(days: 7)),
)));
var store = CacheStore(createTestConfig());
expect(await store.getFile('baseflow.com/test.png'), null);
});
test('Store should return no CacheInfo when file not cached', () async {
var repo = MockCacheInfoRepository();
when(repo.get(any)).thenAnswer((_) => Future.value(null));
var store = CacheStore(createTestConfig());
expect(await store.retrieveCacheData('This is a test'), null);
});
test('Store should return CacheInfo when file is cached', () async {
var config = createTestConfig();
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, DateTime.now(), id: 1);
var store = CacheStore(config);
final cacheObject = await store.retrieveCacheData(fileUrl);
expect(cacheObject, isNotNull);
expect(cacheObject!.id, isNotNull);
});
test('Store should return CacheInfo from memory when asked twice',
() async {
var validTill = DateTime.now();
var config = createTestConfig();
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, validTill, id: 1);
var store = CacheStore(config);
var result = await store.retrieveCacheData(fileUrl);
expect(result, isNotNull);
expect(result!.id, isNotNull);
var otherResult = await store.retrieveCacheData(fileUrl);
expect(otherResult!.id, isNotNull);
verify(config.mockRepo.get(any)).called(1);
});
test(
'Store should return File from memcache only when file is retrieved before',
() async {
var validTill = DateTime.now();
var config = createTestConfig();
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, validTill);
var store = CacheStore(config);
expect(await store.getFileFromMemory(fileUrl), null);
await store.getFile(fileUrl);
expect(await store.getFileFromMemory(fileUrl), isNotNull);
});
test(
'Store.memoryCacheContainsKey should return true if the key is present in the memory cache',
() async {
var config = createTestConfig();
var store = CacheStore(config);
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
validTill: clock.now().add(const Duration(days: 7)),
);
await store.putFile(cacheObject);
expect(store.memoryCacheContainsKey('baseflow.com/test.png'), true);
expect(store.memoryCacheContainsKey('unseen-file'), false);
});
});
group('Storing files in store', () {
test('Store should store fileinfo in repo', () async {
var config = createTestConfig();
var store = CacheStore(config);
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
validTill: clock.now().add(const Duration(days: 7)),
);
await store.putFile(cacheObject);
verify(config.repo.updateOrInsert(cacheObject)).called(1);
});
test(
'Store should store fileinfo in repo and id should be available afterwards',
() async {
var config = createTestConfig();
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
validTill: clock.now().add(const Duration(days: 7)),
);
await config.returnsFile(cacheObject.relativePath);
when(config.mockRepo.updateOrInsert(cacheObject)).thenAnswer(
(realInvocation) async => cacheObject.copyWith(id: 1),
);
var store = CacheStore(config);
await store.putFile(cacheObject);
verify(config.repo.updateOrInsert(cacheObject)).called(1);
final result = await store.retrieveCacheData(cacheObject.key);
expect(result!.id, isNotNull);
});
});
group('Removing files in store', () {
test('Store should remove fileinfo from repo on delete', () async {
var validTill = DateTime.now();
var config = createTestConfig();
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, validTill);
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
var cacheObject = CacheObject(
fileUrl,
relativePath: fileName,
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
await store.removeCachedFile(cacheObject);
verify(config.mockRepo.deleteAll(argThat(contains(cacheObject.id))))
.called(1);
});
test('Store should remove file over capacity', () async {
var config = createTestConfig();
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
await config.returnsFile('testimage.png');
when(config.mockRepo.getObjectsOverCapacity(any))
.thenAnswer((_) => Future.value([cacheObject]));
when(config.mockRepo.getOldObjects(any))
.thenAnswer((_) => Future.value([]));
when(config.mockRepo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(cacheObject));
expect(await store.getFile('baseflow.com/test.png'), isNotNull);
await untilCalled(config.mockRepo.deleteAll(any));
verify(config.mockRepo.getObjectsOverCapacity(any)).called(1);
verify(config.mockRepo.deleteAll(argThat(contains(cacheObject.id))))
.called(1);
});
test('Store should remove file over that are too old', () async {
var config = createTestConfig();
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
await config.returnsFile('testimage.png');
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
when(config.mockRepo.getObjectsOverCapacity(any))
.thenAnswer((_) => Future.value([]));
when(config.mockRepo.getOldObjects(any))
.thenAnswer((_) => Future.value([cacheObject]));
when(config.mockRepo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(cacheObject));
expect(await store.getFile('baseflow.com/test.png'), isNotNull);
await untilCalled(config.mockRepo.deleteAll(any));
verify(config.mockRepo.getOldObjects(any)).called(1);
verify(config.mockRepo.deleteAll(argThat(contains(cacheObject.id))))
.called(1);
});
test('Store should remove file old and over capacity', () async {
var config = createTestConfig();
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
await config.returnsFile('testimage.png');
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
when(config.mockRepo.getObjectsOverCapacity(any))
.thenAnswer((_) => Future.value([cacheObject]));
when(config.mockRepo.getOldObjects(any))
.thenAnswer((_) => Future.value([cacheObject]));
when(config.mockRepo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(cacheObject));
expect(await store.getFile('baseflow.com/test.png'), isNotNull);
await untilCalled(config.mockRepo.deleteAll(any));
await Future.delayed(const Duration(milliseconds: 5));
verify(config.mockRepo.getObjectsOverCapacity(any)).called(1);
verify(config.mockRepo.getOldObjects(any)).called(1);
verify(config.mockRepo.deleteAll(argThat(contains(cacheObject.id))))
.called(1);
});
test('Store should recheck cache info when file is removed', () async {
var config = createTestConfig();
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
var file = await config.returnsFile('testimage.png');
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
when(config.mockRepo.getObjectsOverCapacity(any))
.thenAnswer((_) => Future.value([]));
when(config.mockRepo.getOldObjects(any))
.thenAnswer((_) => Future.value([]));
when(config.mockRepo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(cacheObject));
expect(await store.getFile('baseflow.com/test.png'), isNotNull);
await file.delete();
expect(await store.getFile('baseflow.com/test.png'), isNull);
});
test('Store should not remove files that are not old or over capacity',
() async {
var config = createTestConfig();
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
await config.returnsFile('testimage.png');
var cacheObject = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage.png',
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
when(config.mockRepo.getObjectsOverCapacity(any))
.thenAnswer((_) => Future.value([]));
when(config.mockRepo.getOldObjects(any))
.thenAnswer((_) => Future.value([]));
when(config.mockRepo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(cacheObject));
expect(await store.getFile('baseflow.com/test.png'), isNotNull);
await untilCalled(config.mockRepo.deleteAll(any));
verify(config.mockRepo.getOldObjects(any)).called(1);
verifyNever(config.mockRepo.deleteAll(argThat(contains(cacheObject.id))));
});
test('Store should remove all files when emptying cache', () async {
var config = createTestConfig();
var store = CacheStore(config);
store.cleanupRunMinInterval = const Duration(milliseconds: 1);
var co1 = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage1.png',
id: 1,
validTill: clock.now().add(const Duration(days: 7)),
);
var co2 = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage2.png',
id: 2,
validTill: clock.now().add(const Duration(days: 7)),
);
var co3 = CacheObject(
'baseflow.com/test.png',
relativePath: 'testimage3.png',
id: 3,
validTill: clock.now().add(const Duration(days: 7)),
);
final cacheObjects = [co1, co2, co3];
// Write the cache files to the filesystem
final List<File> cacheFiles = [];
for (var cacheObject in cacheObjects) {
final f = await config.returnsFile(cacheObject.relativePath);
cacheFiles.add(f);
}
// Include an extra file
// This could be a desync between the store and the filesystem
cacheFiles
.add(await config.returnsFile("non-existent-file-in-store.png"));
when(config.mockRepo.getAllObjects())
.thenAnswer((_) => Future.value(cacheObjects));
await store.emptyCache();
// make sure that all cached files in the filesystem are deleted
for (var cacheFile in cacheFiles) {
expect(await cacheFile.exists(), isFalse);
}
verify(config.mockRepo
.deleteAll(argThat(containsAll([co1.id, co2.id, co3.id])))).called(1);
});
test('Store should delete file when remove cached file', () async {
var config = createTestConfig();
var store = CacheStore(config);
await config.returnsFile(fileName);
config.returnsCacheObject(fileUrl, fileName, validTill, id: 1);
var cacheObject = await store.retrieveCacheData(fileUrl);
expect(cacheObject, isNotNull);
var fileInfo = await store.getFile(cacheObject!.key);
expect(await fileInfo?.file.exists(), isTrue);
await store.removeCachedFile(cacheObject);
expect(await fileInfo?.file.exists(), isFalse);
});
});
}
Future<Directory> createDir() async {
final fileSystem = MemoryFileSystem();
return fileSystem.systemTempDirectory.createTemp('test');
}