-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathv3_close_test.dart
152 lines (128 loc) · 4.54 KB
/
v3_close_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import 'dart:async';
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';
import 'docker.dart';
void main() {
withPostgresServer('v3 close', (server) {
late Connection conn1;
late Connection conn2;
const conn1Name = 'conn1';
const conn2Name = 'conn2';
setUp(() async {
conn1 = await Connection.open(
await server.endpoint(),
settings: ConnectionSettings(
applicationName: conn1Name,
//transformer: _loggingTransformer('c1'),
),
);
conn2 = await Connection.open(
await server.endpoint(),
settings: ConnectionSettings(
applicationName: conn2Name,
),
);
});
tearDown(() async {
await conn1.close();
await conn2.close();
});
for (final concurrentQuery in [false, true]) {
test(
'with concurrent query: $concurrentQuery',
() async {
final res = await conn2.execute(
"SELECT pid FROM pg_stat_activity where application_name = '$conn1Name';");
final conn1PID = res.first.first as int;
// Simulate issue by terminating a connection during a query
if (concurrentQuery) {
// We expect that terminating the connection will throw.
expect(conn1.execute('select pg_sleep(1) from pg_stat_activity;'),
_throwsPostgresException);
}
// Terminate the conn1 while the query is running
await conn2.execute('select pg_terminate_backend($conn1PID);');
},
);
}
test('with simple query protocol', () async {
// Get the PID for conn1
final res = await conn2.execute(
"SELECT pid FROM pg_stat_activity where application_name = '$conn1Name';");
final conn1PID = res.first.first as int;
// ignore: unawaited_futures
expect(
conn1.execute('select pg_sleep(1) from pg_stat_activity;',
ignoreRows: true),
_throwsPostgresException);
await conn2.execute(
'select pg_terminate_backend($conn1PID) from pg_stat_activity;');
});
test('empty query does not close connection', () async {
await conn1.execute('-- test');
expect(await conn1.execute('SELECT 1'), [
[1]
]);
});
});
group('force close', () {
Future<Connection> openConnection(PostgresServer server) async {
final conn = await Connection.open(await server.endpoint());
addTearDown(conn.close);
return conn;
}
Future<void> expectConn1ClosesForcefully(Connection conn) async {
await conn
.close(force: true) //
// If close takes too long, the test will fail (force=true would not be working correctly)
// as it would be waiting for the query to finish
.timeout(Duration(seconds: 1));
expect(conn.isOpen, isFalse);
}
Future<void> runLongQuery(Session session) {
return session.execute('select pg_sleep(10) from pg_stat_activity;');
}
withPostgresServer('connection session', (server) {
test('connection session', () async {
final conn = await openConnection(server);
final rs = runLongQuery(conn);
// let it start
await Future.delayed(const Duration(milliseconds: 100));
await expectConn1ClosesForcefully(conn);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
withPostgresServer('tx session', (server) {
test('tx', () async {
final conn = await openConnection(server);
final started = Completer();
final rs = conn.runTx((tx) async {
started.complete();
await runLongQuery(tx);
});
// let it start
await started.future;
await Future.delayed(const Duration(milliseconds: 100));
await expectConn1ClosesForcefully(conn);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
withPostgresServer('run session', (server) {
test('run', () async {
final conn = await openConnection(server);
final started = Completer();
final rs = conn.run((s) async {
started.complete();
await runLongQuery(s);
});
// let it start
await started.future;
await Future.delayed(const Duration(milliseconds: 100));
await expectConn1ClosesForcefully(conn);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
});
}
final _isPostgresException = isA<PgException>();
final _throwsPostgresException = throwsA(_isPostgresException);