|
| 1 | +import 'package:postgres/postgres_v3_experimental.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +import '../docker.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + // NOTE: The Docker Container will not close after stopping this test so that needs to be done manually. |
| 8 | + usePostgresDocker(); |
| 9 | + |
| 10 | + group('service-side connection close', |
| 11 | + skip: 'the error is not caught or handled properly', () { |
| 12 | + // ignore: unused_local_variable |
| 13 | + late final PgConnection conn1; |
| 14 | + late final PgConnection conn2; |
| 15 | + |
| 16 | + setUpAll(() async { |
| 17 | + conn1 = await PgConnection.open( |
| 18 | + PgEndpoint( |
| 19 | + host: 'localhost', |
| 20 | + database: 'dart_test', |
| 21 | + username: 'dart', |
| 22 | + password: 'dart', |
| 23 | + ), |
| 24 | + sessionSettings: PgSessionSettings( |
| 25 | + onBadSslCertificate: (cert) => true, |
| 26 | + ), |
| 27 | + ); |
| 28 | + |
| 29 | + conn2 = await PgConnection.open( |
| 30 | + PgEndpoint( |
| 31 | + host: 'localhost', |
| 32 | + database: 'dart_test', |
| 33 | + username: 'postgres', |
| 34 | + password: 'postgres', |
| 35 | + ), |
| 36 | + sessionSettings: PgSessionSettings( |
| 37 | + onBadSslCertificate: (cert) => true, |
| 38 | + ), |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test('produce error', () async { |
| 43 | + // get conn1 PID |
| 44 | + final res = await conn2 |
| 45 | + .execute("SELECT pid FROM pg_stat_activity where usename = 'dart';"); |
| 46 | + final conn1PID = res.first.first as int; |
| 47 | + |
| 48 | + // Simulate issue by terminating a connection during a query |
| 49 | + // ignore: unawaited_futures |
| 50 | + conn1.execute( |
| 51 | + 'select * from pg_stat_activity;'); // comment this out and a different error will appear |
| 52 | + |
| 53 | + // Terminate the conn1 while the query is running |
| 54 | + await conn2.execute( |
| 55 | + 'select pg_terminate_backend($conn1PID) from pg_stat_activity;'); |
| 56 | + // this will cause the following exception: |
| 57 | + // PostgreSQLException (PostgreSQLSeverity.fatal 57P01: terminating connection due to administrator command ) |
| 58 | + |
| 59 | + expect(true, true); |
| 60 | + }); |
| 61 | + |
| 62 | + tearDownAll(() async { |
| 63 | + print('closing conn1'); |
| 64 | + await conn1.close(); // this will never close & execution will hang here |
| 65 | + print('closing conn2'); |
| 66 | + await conn2.close(); |
| 67 | + }); |
| 68 | + }); |
| 69 | +} |
0 commit comments