Skip to content
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

Don't modify Cache.keys during iteration #698

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
}
Loading