Skip to content
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
27 changes: 18 additions & 9 deletions lib/core/database/redis_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ class RedisConnection {
}
final result = await _command!.send_object(['PING']);
if (result == null || result.toString().toUpperCase() != 'PONG') {
await _conn?.close();
try {
await _conn?.close();
} catch (_) {}
_conn = null;
_command = null;
throw RedisConnectionException('PING failed');
Expand All @@ -133,17 +135,24 @@ class RedisConnection {
}

Future<void> disconnect() async {
final wasConnected = _isConnected;
_isConnected = false;
_command = null;
final c = _conn;
_conn = null;
try {
await c?.close();
} catch (e) {
debugPrint('RedisConnection.disconnect: $e');
if (c != null && wasConnected) {
try {
await c.close();
} catch (e) {
if (e is! TypeError && !e.toString().contains('Null check operator')) {
debugPrint('RedisConnection.disconnect: $e');
}
}
}
}

Future<void> forceClose() => disconnect();

Future<String> info() async {
if (!isConnected || _command == null) {
throw StateError('Not connected to Redis');
Expand Down Expand Up @@ -464,10 +473,10 @@ class RedisConnectionTestFake extends RedisConnection {
final c = _conn;
_conn = null;
_command = null;
try {
await c?.close();
} catch (e) {
debugPrint('RedisConnection.disconnect: $e');
if (c != null) {
try {
await c.close();
} catch (_) {}
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/core/database/redis_connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ void main() {
await conn.disconnect();
expect(conn.isConnected, false);
});

test('forceClose delegates to disconnect cleanly', () async {
final conn = RedisConnection(
id: 1,
name: 'test',
host: 'localhost',
);
await conn.forceClose();
expect(conn.isConnected, false);
});
});

group('RedisConnection.info', () {
Expand Down
Loading