forked from isoos/postgresql-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification_test.dart
121 lines (96 loc) · 3.34 KB
/
notification_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
import 'dart:async';
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';
import 'docker.dart';
void main() {
withPostgresServer('Successful notifications', (server) {
late Connection connection;
setUp(() async {
connection = await server.newConnection();
});
tearDown(() async {
await connection.close();
});
test('Notification Response', () async {
final channel = 'virtual';
final payload = 'This is the payload';
final futureMsg = connection.channels.all.first;
await connection.execute(
'LISTEN $channel;'
"NOTIFY $channel, '$payload';",
queryMode: QueryMode.simple,
);
final msg = await futureMsg.timeout(Duration(milliseconds: 200));
expect(msg.channel, channel);
expect(msg.payload, payload);
});
test('Notification Response empty payload', () async {
final channel = 'virtual';
final futureMsg = connection.channels.all.first;
await connection.execute(
'LISTEN $channel;'
'NOTIFY $channel;',
queryMode: QueryMode.simple,
);
final msg = await futureMsg.timeout(Duration(milliseconds: 200));
expect(msg.channel, channel);
expect(msg.payload, '');
});
test('Notification UNLISTEN', () async {
final channel = 'virtual';
final payload = 'This is the payload';
var futureMsg = connection.channels.all.first;
await connection.execute(
'LISTEN $channel;'
"NOTIFY $channel, '$payload';",
queryMode: QueryMode.simple,
);
final msg = await futureMsg.timeout(Duration(milliseconds: 200));
expect(msg.channel, channel);
expect(msg.payload, payload);
await connection.execute('UNLISTEN $channel;');
futureMsg = connection.channels.all.first;
try {
await connection.execute(
"NOTIFY $channel, '$payload';",
queryMode: QueryMode.simple,
);
await futureMsg.timeout(Duration(milliseconds: 200));
fail('There should be no notification');
} on TimeoutException catch (_) {}
});
test('Notification many channel', () async {
final countResponse = <String, int>{};
var totalCountResponse = 0;
final finishExecute = Completer();
connection.channels.all.listen((msg) {
final count = countResponse[msg.channel];
countResponse[msg.channel] = (count ?? 0) + 1;
totalCountResponse++;
if (totalCountResponse == 20) finishExecute.complete();
});
final channel1 = 'virtual1';
final channel2 = 'virtual2';
Future<void> notifier() async {
for (var i = 0; i < 5; i++) {
await connection.execute(
'NOTIFY $channel1;'
'NOTIFY $channel2;',
queryMode: QueryMode.simple,
);
}
}
await connection.execute('LISTEN $channel1;');
await notifier();
await connection.execute('LISTEN $channel2;');
await notifier();
await connection.execute('UNLISTEN $channel1;');
await notifier();
await connection.execute('UNLISTEN $channel2;');
await notifier();
await finishExecute.future.timeout(Duration(milliseconds: 200));
expect(countResponse[channel1], 10);
expect(countResponse[channel2], 10);
}, timeout: Timeout(Duration(seconds: 5)));
});
}