Skip to content

Commit

Permalink
bones_api_utils_test.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpassos committed May 20, 2024
1 parent 57f02e1 commit 8383d73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/src/bones_api_utils_timedmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class TimedMap<K, V> implements Map<K, V> {
/// The key timeout. When a key is put, it expires after the timeout [Duration].
final Duration keyTimeout;

final bool? Function(Object? key, Duration elapsedTime, Duration keyTimeout)?
keyTimeoutChecker;
final bool? Function(TimedMap timedMap, Object? key, Duration elapsedTime,
Duration keyTimeout)? keyTimeoutChecker;

TimedMap(this.keyTimeout, [Map<K, V>? map, this.keyTimeoutChecker]) {
if (map != null) {
Expand Down Expand Up @@ -140,7 +140,7 @@ class TimedMap<K, V> implements Map<K, V> {
keyTimeout ??= this.keyTimeout;

if (keyTimeoutChecker != null) {
var expired = keyTimeoutChecker(key, elapsedTime, keyTimeout);
var expired = keyTimeoutChecker(this, key, elapsedTime, keyTimeout);
if (expired != null) {
return expired;
}
Expand Down
28 changes: 28 additions & 0 deletions test/bones_api_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,34 @@ void main() {

expect(m.length, equals(0));
});

test('keyTimeoutChecker', () async {
final checks = <String>[];

var m = TimedMap<String, int>(Duration(seconds: 1), null, (tm, k, t, to) {
checks.add('$k,${to.inMilliseconds}');
if (k == 'b') return false;
return null;
});

m.put('a', 1);
m.put('b', 2);

expect(m.length, equals(2));
expect(checks, isEmpty);

m.checkAllEntries();

expect(m.length, equals(2));
expect(checks, equals(['a,1000', 'b,1000']));

await Future.delayed(Duration(milliseconds: 1100));

m.checkAllEntries();

expect(m, equals({'b': 2}));
expect(checks, equals(['a,1000', 'b,1000', 'a,1000', 'b,1000']));
});
});

group('PositionalFields', () {
Expand Down

0 comments on commit 8383d73

Please sign in to comment.