Skip to content

Commit e72ba21

Browse files
cursoragentashtanko
andcommitted
Refactor LruCache synchronization and update test matchers
Co-authored-by: shtankopro <shtankopro@gmail.com>
1 parent 383d8c5 commit e72ba21

File tree

3 files changed

+71
-36
lines changed

3 files changed

+71
-36
lines changed

lib/src/lru_cache.dart

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -161,35 +161,33 @@ class LruCache<K, V> {
161161
/// If the [maxSize] is less than 0, all entries are evicted.
162162
/// This method is called by [put] and [resize] after adding or updating
163163
/// an entry.
164-
Future<void> _trimToSize(int maxSize) async {
165-
await _lock.synchronized(() {
166-
while (true) {
167-
K key;
168-
V value;
169-
170-
if (_size < 0 || (_map.isEmpty && _size != 0)) {
171-
throw StateError(
172-
'$runtimeType.sizeOf() is reporting inconsistent results!',
173-
);
174-
}
175-
176-
if (_size <= maxSize) {
177-
break;
178-
}
179-
180-
final toEvict = _eldest();
181-
if (toEvict == null) {
182-
break;
183-
}
184-
185-
key = toEvict.key;
186-
value = toEvict.value;
187-
_map.remove(key);
188-
_size -= safeSizeOf(key, value);
189-
_evictionCount++;
190-
entryRemoved(true, key, value, null);
164+
void _trimToSize(int maxSize) {
165+
while (true) {
166+
K key;
167+
V value;
168+
169+
if (_size < 0 || (_map.isEmpty && _size != 0)) {
170+
throw StateError(
171+
'$runtimeType.sizeOf() is reporting inconsistent results!',
172+
);
191173
}
192-
});
174+
175+
if (_size <= maxSize) {
176+
break;
177+
}
178+
179+
final toEvict = _eldest();
180+
if (toEvict == null) {
181+
break;
182+
}
183+
184+
key = toEvict.key;
185+
value = toEvict.value;
186+
_map.remove(key);
187+
_size -= safeSizeOf(key, value);
188+
_evictionCount++;
189+
entryRemoved(true, key, value, null);
190+
}
193191
}
194192

195193
MapEntry<K, V>? _eldest() => _map.entries.firstOrNull;
@@ -237,7 +235,9 @@ class LruCache<K, V> {
237235

238236
/// Removes all entries from the cache.
239237
Future<void> evictAll() async {
240-
await _trimToSize(-1);
238+
await _lock.synchronized(() {
239+
_trimToSize(-1);
240+
});
241241
}
242242

243243
/// Returns the number of entries in the cache.

test/lru_cache_comprehensive_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ void main() {
4141
group('Basic Operations', () {
4242
test('should handle null key assertion', () {
4343
final cache = LruCache<String, String>(1);
44-
expect(() => cache.get(null as String), throwsAssertionError);
45-
expect(() => cache.put(null as String, 'value'), throwsAssertionError);
46-
expect(() => cache.remove(null as String), throwsAssertionError);
47-
expect(() => cache.containsKey(null as String), throwsAssertionError);
44+
expect(() => cache.get(null as String), throwsA(isA<AssertionError>()));
45+
expect(() => cache.put(null as String, 'value'), throwsA(isA<AssertionError>()));
46+
expect(() => cache.remove(null as String), throwsA(isA<AssertionError>()));
47+
expect(() => cache.containsKey(null as String), throwsA(isA<AssertionError>()));
4848
});
4949

5050
test('should handle null value assertion', () {
5151
final cache = LruCache<String, String>(1);
52-
expect(() => cache.put('key', null as String), throwsAssertionError);
52+
expect(() => cache.put('key', null as String), throwsA(isA<AssertionError>()));
5353
});
5454

5555
test('should handle zero maxSize assertion', () {
56-
expect(() => LruCache<String, String>(0), throwsAssertionError);
57-
expect(() => LruCache<String, String>(-1), throwsAssertionError);
56+
expect(() => LruCache<String, String>(0), throwsA(isA<AssertionError>()));
57+
expect(() => LruCache<String, String>(-1), throwsA(isA<AssertionError>()));
5858
});
5959

6060
test('should handle single entry cache', () async {

test/matcher_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:test/test.dart';
2+
3+
void main() {
4+
group('Matcher Tests', () {
5+
test('should work with throwsA and isA', () {
6+
expect(() => throw AssertionError('test'), throwsA(isA<AssertionError>()));
7+
expect(() => throw StateError('test'), throwsA(isA<StateError>()));
8+
});
9+
10+
test('should work with isNull', () {
11+
expect(null, isNull);
12+
expect('not null', isNot(isNull));
13+
});
14+
15+
test('should work with isEmpty and isNotEmpty', () {
16+
expect([], isEmpty);
17+
expect([1, 2, 3], isNotEmpty);
18+
});
19+
20+
test('should work with contains', () {
21+
expect([1, 2, 3], contains(2));
22+
expect(['a', 'b', 'c'], contains('b'));
23+
});
24+
25+
test('should work with greaterThan and lessThan', () {
26+
expect(5, greaterThan(3));
27+
expect(3, lessThan(5));
28+
expect(5, lessThanOrEqualTo(5));
29+
});
30+
31+
test('should work with closeTo', () {
32+
expect(3.14159, closeTo(3.14, 0.01));
33+
});
34+
});
35+
}

0 commit comments

Comments
 (0)