@@ -8,16 +8,23 @@ void main() {
88 late Connection conn1;
99 late Connection conn2;
1010
11+ const conn1Name = 'conn1' ;
12+ const conn2Name = 'conn2' ;
13+
1114 setUp (() async {
1215 conn1 = await Connection .open (
1316 await server.endpoint (),
1417 settings: ConnectionSettings (
15- //transformer: _loggingTransformer('c1'),
16- ),
18+ applicationName: conn1Name,
19+ //transformer: _loggingTransformer('c1'),
20+ ),
1721 );
1822
1923 conn2 = await Connection .open (
2024 await server.endpoint (),
25+ settings: ConnectionSettings (
26+ applicationName: conn2Name,
27+ ),
2128 );
2229 });
2330
@@ -30,9 +37,8 @@ void main() {
3037 test (
3138 'with concurrent query: $concurrentQuery ' ,
3239 () async {
33- final endpoint = await server.endpoint ();
3440 final res = await conn2.execute (
35- "SELECT pid FROM pg_stat_activity where usename = '${ endpoint . username } ';" );
41+ "SELECT pid FROM pg_stat_activity where application_name = '$conn1Name ';" );
3642 final conn1PID = res.first.first as int ;
3743
3844 // Simulate issue by terminating a connection during a query
@@ -49,10 +55,9 @@ void main() {
4955 }
5056
5157 test ('with simple query protocol' , () async {
52- final endpoint = await server.endpoint ();
5358 // Get the PID for conn1
5459 final res = await conn2.execute (
55- "SELECT pid FROM pg_stat_activity where usename = '${ endpoint . username } ';" );
60+ "SELECT pid FROM pg_stat_activity where application_name = '$conn1Name ';" );
5661 final conn1PID = res.first.first as int ;
5762
5863 // ignore: unawaited_futures
@@ -65,6 +70,61 @@ void main() {
6570 'select pg_terminate_backend($conn1PID ) from pg_stat_activity;' );
6671 });
6772 });
73+
74+ group ('force close' , () {
75+ Future <Connection > openConnection (PostgresServer server) async {
76+ final conn = await Connection .open (await server.endpoint ());
77+ addTearDown (conn.close);
78+ return conn;
79+ }
80+
81+ Future <void > expectConn1ClosesForcefully (Connection conn) async {
82+ await conn
83+ .close (force: true ) //
84+ // If close takes too long, the test will fail (force=true would not be working correctly)
85+ // as it would be waiting for the query to finish
86+ .timeout (Duration (seconds: 1 ));
87+ expect (conn.isOpen, isFalse);
88+ }
89+
90+ Future <void > runLongQuery (Session session) {
91+ return session.execute ('select pg_sleep(10) from pg_stat_activity;' );
92+ }
93+
94+ withPostgresServer ('connection session' , (server) {
95+ test ('' , () async {
96+ final conn = await openConnection (server);
97+ // ignore: unawaited_futures
98+ runLongQuery (conn);
99+ // let it start
100+ await Future .delayed (const Duration (milliseconds: 100 ));
101+ await expectConn1ClosesForcefully (conn);
102+ });
103+ });
104+
105+ withPostgresServer ('tx session' , (server) {
106+ test ('' , () async {
107+ final conn = await openConnection (server);
108+ // ignore: unawaited_futures
109+ // Ignore async error, it will fail when the connection is closed and it tries to do COMMIT
110+ conn.runTx (runLongQuery).ignore ();
111+ // let it start
112+ await Future .delayed (const Duration (milliseconds: 100 ));
113+ await expectConn1ClosesForcefully (conn);
114+ });
115+ });
116+
117+ withPostgresServer ('run session' , (server) {
118+ test ('' , () async {
119+ final conn = await openConnection (server);
120+ // ignore: unawaited_futures
121+ conn.run (runLongQuery);
122+ // let it start
123+ await Future .delayed (const Duration (milliseconds: 100 ));
124+ await expectConn1ClosesForcefully (conn);
125+ });
126+ });
127+ });
68128}
69129
70130final _isPostgresException = isA <PgException >();
0 commit comments