Skip to content

Commit

Permalink
Fix multiple commands in redis cache. (google#142)
Browse files Browse the repository at this point in the history
* Fix multiple commands in redis cache.

* Using future boundary to add stream.
  • Loading branch information
isoos authored Jul 8, 2021
1 parent 6c1d468 commit d566ef6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions neat_cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v2.0.1
* Fixed issue when adding multiple commands without waiting for them to complete first.

## v2.0.0
* Migrated to null-safety, dropping dependency on `package:dartis`.

Expand Down
4 changes: 3 additions & 1 deletion neat_cache/lib/src/providers/resp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class RespClient {

final _ByteStreamScanner _input;
final StreamSink<List<int>> _output;
Future _pendingStream = Future.value(null);

final _pending = Queue<Completer<Object?>>();

Expand Down Expand Up @@ -177,7 +178,8 @@ class RespClient {
final c = Completer<Object?>();
_pending.addLast(c);
try {
await _output.addStream(Stream.value(out.toBytes()));
_pendingStream = _pendingStream
.then((value) => _output.addStream(Stream.value(out.toBytes())));
} on Exception catch (e, st) {
await _abort(e, st);
}
Expand Down
2 changes: 1 addition & 1 deletion neat_cache/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: neat_cache
version: 2.0.0
version: 2.0.1
description: |
A neat cache abstraction for wrapping in-memory or redis caches.
homepage: https://github.com/google/dart-neats/tree/master/neat_cache
Expand Down
30 changes: 30 additions & 0 deletions neat_cache/test/cacheprovider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,42 @@ void testCacheProvider({
expect(r, isNull);
});

test('get multiple keys at the same time', () async {
await cache.purge('test-key');
final list = await Future.wait([
cache.get('test-empty-key-1'),
cache.get('test-empty-key-2'),
cache.get('test-empty-key-3'),
cache.get('test-empty-key-4'),
cache.get('test-empty-key-1'),
cache.get('test-empty-key-2'),
cache.get('test-empty-key-3'),
cache.get('test-empty-key-4'),
]);
expect(list, isNotEmpty);
expect(list.where((e) => e != null), isEmpty);
});

test('get/set key', () async {
await cache.set('test-key-2', 'hello-world-42');
final r = await cache.get('test-key-2');
expect(r, equals('hello-world-42'));
});

test('get/set multiple keys', () async {
await Future.wait([
cache.set('test-multi-key-1', 'hello-world-1'),
cache.set('test-multi-key-2', 'hello-world-2'),
cache.set('test-multi-key-3', 'hello-world-3'),
]);
final values = await Future.wait([
cache.get('test-multi-key-1'),
cache.get('test-multi-key-2'),
cache.get('test-multi-key-3'),
]);
expect(values, ['hello-world-1', 'hello-world-2', 'hello-world-3']);
});

test('set key (overwrite)', () async {
await cache.set('test-key-3', 'hello-once');
final r = await cache.get('test-key-3');
Expand Down

0 comments on commit d566ef6

Please sign in to comment.