-
Notifications
You must be signed in to change notification settings - Fork 38
/
pool_test.dart
287 lines (240 loc) · 8.51 KB
/
pool_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import 'dart:async';
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';
import 'docker.dart';
void main() {
withPostgresServer('generic', (server) {
late Pool pool;
setUp(() async {
pool = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(maxConnectionCount: 8),
);
// We can't write to the public schema by default in postgres 15, so
// create one for this test.
await pool.execute('CREATE SCHEMA IF NOT EXISTS test');
});
tearDown(() => pool.close());
test('does not support channels', () {
expect(pool.withConnection((c) async => c.channels.notify('foo')),
throwsUnsupportedError);
});
test('execute re-uses free connection', () async {
// The temporary table is only visible to the connection creating it, so
// this asserts that all statements are running on the same underlying
// connection.
await pool.execute('CREATE TEMPORARY TABLE foo (bar INTEGER);');
await pool.execute('INSERT INTO foo VALUES (1), (2), (3);');
final results = await pool.execute('SELECT * FROM foo');
expect(results, hasLength(3));
});
test('can use transactions', () async {
// The table can't be temporary because it needs to be visible across
// connections.
await pool.execute(
'CREATE TABLE IF NOT EXISTS test.transactions (bar INTEGER);');
addTearDown(() => pool.execute('DROP TABLE test.transactions;'));
final completeTransaction = Completer();
final transaction = pool.runTx((session) async {
await session
.execute('INSERT INTO test.transactions VALUES (1), (2), (3);');
await completeTransaction.future;
});
var rows = await pool.execute('SELECT * FROM test.transactions');
expect(rows, isEmpty);
completeTransaction.complete();
await transaction;
rows = await pool.execute('SELECT * FROM test.transactions');
expect(rows, hasLength(3));
});
test('can use prepared statements', () async {
await pool
.execute('CREATE TABLE IF NOT EXISTS test.statements (bar INTEGER);');
addTearDown(() => pool.execute('DROP TABLE test.statements;'));
final stmt = await pool.prepare('SELECT * FROM test.statements');
expect(await stmt.run([]), isEmpty);
await pool.execute('INSERT INTO test.statements VALUES (1), (2), (3);');
expect(await stmt.run([]), hasLength(3));
await stmt.dispose();
});
test('disables close()', () async {
late Connection leakedConnection;
await pool.withConnection((connection) async {
expect(connection.isOpen, isTrue);
await connection.close();
expect(connection.isOpen, isTrue);
leakedConnection = connection;
});
await pool.close();
expect(pool.isOpen, isFalse);
expect(leakedConnection.isOpen, isFalse);
});
});
withPostgresServer('handles session errors', (server) {
test('timeout unlocks pool', () async {
final db = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(
maxConnectionCount: 1,
connectTimeout: Duration(seconds: 3),
),
);
await expectLater(
() => db.run((_) async {
// NOTE: session is not used here
await db.execute('SELECT 1');
}),
throwsA(isA<TimeoutException>()),
);
});
test('bad query does not lock up pool instance', () async {
final db = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(
maxConnectionCount: 1,
),
);
for (var i = 0; i < 10; i++) {
await expectLater(
() => db.run((c) => c.execute('select x;')), throwsException);
}
await db.execute('SELECT 1');
});
test('empty query does not lock up pool instance', () async {
final db = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(
maxConnectionCount: 1,
),
);
await db.execute('-- test');
expect(await db.execute('SELECT 1'), [
[1]
]);
});
});
withPostgresServer('limit pool connections', (server) {
test('can limit concurrent connections', () async {
final pool = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(maxConnectionCount: 2),
);
addTearDown(pool.close);
final completeFirstTwo = Completer();
final didInvokeThird = Completer();
// Take two connections
unawaited(pool.withConnection((connection) => completeFirstTwo.future));
unawaited(pool.withConnection((connection) => completeFirstTwo.future));
// Creating a third one should block.
unawaited(pool.withConnection((connection) async {
didInvokeThird.complete();
}));
await pumpEventQueue();
expect(didInvokeThird.isCompleted, isFalse);
completeFirstTwo.complete();
await didInvokeThird.future;
});
});
withPostgresServer('closes old connections', (server) {
test('when new connection required it', () async {
final pool = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(maxConnectionCount: 1),
);
addTearDown(pool.close);
final results = <int>{};
final futures = <Future>{};
for (var i = 0; i < 10; i++) {
final f = pool.withConnection((c) async {
await c.execute('SELECT $i');
results.add(i);
}, settings: PoolSettings(applicationName: 'x$i'));
futures.add(f);
}
await Future.wait(futures);
expect(results, hasLength(10));
});
});
withPostgresServer('Connection settings', (server) {
test('runs connection.onOpen callback', () async {
final pool = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(
maxConnectionCount: 1,
onOpen: (c) async {
await c.execute('SET application_name TO myapp;');
},
),
);
addTearDown(pool.close);
final name = (await pool.execute('SHOW application_name;')).single.single;
expect(name, 'myapp');
});
});
group('force close', () {
Future<Pool> openPool(PostgresServer server) async {
final pool = Pool.withEndpoints(
[await server.endpoint()],
settings: PoolSettings(maxConnectionCount: 1),
);
addTearDown(pool.close);
return pool;
}
Future<void> expectPoolClosesForcefully(Pool pool) async {
await pool
.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(pool.isOpen, isFalse);
}
Future<void> runLongQuery(Session session) {
return session.execute('select pg_sleep(10) from pg_stat_activity;');
}
withPostgresServer('pool session', (server) {
test('pool session', () async {
final pool = await openPool(server);
final started = Completer();
final rs = pool.run((s) async {
started.complete();
await runLongQuery(s);
});
// let it start
await started.future;
await Future.delayed(const Duration(milliseconds: 100));
await expectPoolClosesForcefully(pool);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
withPostgresServer('tx session', (server) {
test('tx', () async {
final pool = await openPool(server);
final started = Completer();
final rs = pool.runTx((s) async {
started.complete();
await runLongQuery(s);
});
// let it start
await started.future;
await Future.delayed(const Duration(milliseconds: 100));
await expectPoolClosesForcefully(pool);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
withPostgresServer('inner connection close', (server) {
test('connection from inside withConnection', () async {
final pool = await openPool(server);
final rs = pool.withConnection((c) async {
await Future.wait([
Future.delayed(
Duration(milliseconds: 200),
() => c.close(force: true),
),
runLongQuery(c),
]);
});
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
});
}