|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:pool/pool.dart'; |
| 4 | +import 'package:postgres/src/utils/package_pool_ext.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('package:pool extensions', () { |
| 9 | + test('acquire with timeout succeeds - no parallel use', () async { |
| 10 | + final pool = Pool(1); |
| 11 | + final x = await pool.withRequestTimeout( |
| 12 | + timeout: Duration(seconds: 1), |
| 13 | + (_) async { |
| 14 | + return 1; |
| 15 | + }, |
| 16 | + ); |
| 17 | + expect(x, 1); |
| 18 | + final r = await pool.request(); |
| 19 | + r.release(); |
| 20 | + await pool.close(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('acquire with timeout succeeds - quick parallel use', () async { |
| 24 | + final pool = Pool(1); |
| 25 | + final other = await pool.request(); |
| 26 | + Timer(Duration(seconds: 1), other.release); |
| 27 | + var remainingMillis = 0; |
| 28 | + final x = await pool.withRequestTimeout( |
| 29 | + timeout: Duration(seconds: 2), |
| 30 | + (remaining) async { |
| 31 | + remainingMillis = remaining.inMilliseconds; |
| 32 | + return 1; |
| 33 | + }, |
| 34 | + ); |
| 35 | + expect(x, 1); |
| 36 | + final r = await pool.request(); |
| 37 | + r.release(); |
| 38 | + await pool.close(); |
| 39 | + expect(remainingMillis, greaterThan(500)); |
| 40 | + expect(remainingMillis, lessThan(1500)); |
| 41 | + }); |
| 42 | + |
| 43 | + test('acquire with timeout fails - long parallel use', () async { |
| 44 | + final pool = Pool(1); |
| 45 | + final other = await pool.request(); |
| 46 | + Timer(Duration(seconds: 2), other.release); |
| 47 | + await expectLater( |
| 48 | + pool.withRequestTimeout( |
| 49 | + timeout: Duration(seconds: 1), |
| 50 | + (_) async { |
| 51 | + return 1; |
| 52 | + }, |
| 53 | + ), |
| 54 | + throwsA(isA<TimeoutException>()), |
| 55 | + ); |
| 56 | + final sw = Stopwatch()..start(); |
| 57 | + final r = await pool.request(); |
| 58 | + sw.stop(); |
| 59 | + r.release(); |
| 60 | + await pool.close(); |
| 61 | + expect(sw.elapsedMilliseconds, greaterThan(500)); |
| 62 | + expect(sw.elapsedMilliseconds, lessThan(1500)); |
| 63 | + }); |
| 64 | + }); |
| 65 | +} |
0 commit comments