Skip to content

Commit

Permalink
Don't modify Cache.keys during iteration (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything authored Oct 2, 2024
1 parent 59c0b39 commit 8b4b451
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/cache/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Cache<T> extends MapBase<Snowflake, T> {
factory Cache(Nyxx client, String identifier, CacheConfig<T> config) => client.cache.getCache(identifier, config);

@override
Iterable<Snowflake> get keys => _mru.map((entry) => entry.id);
Iterable<Snowflake> get keys => _entries.keys;

/// Filter the items in this cache so that it obeys the [config].
///
Expand Down
17 changes: 17 additions & 0 deletions test/unit/cache/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,22 @@ void main() {
cache1[entity.id] = entity;
expect(cache2.containsKey(entity.id), isFalse);
});

test('toList', () {
final cache = MockNyxx().cache.getCache<MockSnowflakeEntity>('test', CacheConfig());

final entity1 = MockSnowflakeEntity(id: Snowflake(1));
final entity2 = MockSnowflakeEntity(id: Snowflake(2));
final entity3 = MockSnowflakeEntity(id: Snowflake(3));
final entity4 = MockSnowflakeEntity(id: Snowflake(4));
final entity5 = MockSnowflakeEntity(id: Snowflake(5));

for (final entity in [entity1, entity2, entity3, entity4, entity5]) {
cache[entity.id] = entity;
}

expect(cache.keys.toList, returnsNormally);
expect(cache.values.toList, returnsNormally);
});
});
}

0 comments on commit 8b4b451

Please sign in to comment.