diff --git a/lib/core/database/redis_connection.dart b/lib/core/database/redis_connection.dart index 5a0a4b4..98ea8ce 100644 --- a/lib/core/database/redis_connection.dart +++ b/lib/core/database/redis_connection.dart @@ -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'); @@ -133,17 +135,24 @@ class RedisConnection { } Future 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 forceClose() => disconnect(); + Future info() async { if (!isConnected || _command == null) { throw StateError('Not connected to Redis'); @@ -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 (_) {} } } diff --git a/test/core/database/redis_connection_test.dart b/test/core/database/redis_connection_test.dart index ea60869..c742260 100644 --- a/test/core/database/redis_connection_test.dart +++ b/test/core/database/redis_connection_test.dart @@ -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', () {