-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupdate_notification_test.dart
154 lines (122 loc) · 4.77 KB
/
update_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
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
import 'dart:async';
import 'package:fake_async/fake_async.dart';
import 'package:sqlite_async/src/update_notification.dart';
import 'package:test/test.dart';
void main() {
group('Update notifications', () {
const timeout = Duration(seconds: 10);
const halfTimeout = Duration(seconds: 5);
group('throttle', () {
test('can add initial', () {
fakeAsync((control) {
final source = StreamController<UpdateNotification>(sync: true);
final events = <UpdateNotification>[];
UpdateNotification.throttleStream(source.stream, timeout,
addOne: UpdateNotification({'a'})).listen(events.add);
control.flushMicrotasks();
expect(events, hasLength(1));
control.elapse(halfTimeout);
source.add(UpdateNotification({'b'}));
expect(events, hasLength(1)); // Still a delay from the initial one
control.elapse(halfTimeout);
expect(events, hasLength(2));
});
});
test('sends events after initial throttle', () {
fakeAsync((control) {
final source = StreamController<UpdateNotification>(sync: true);
final events = <UpdateNotification>[];
UpdateNotification.throttleStream(source.stream, timeout)
.listen(events.add);
source.add(UpdateNotification({'a'}));
control.elapse(halfTimeout);
expect(events, isEmpty);
control.elapse(halfTimeout);
expect(events, hasLength(1));
});
});
test('merges events', () {
fakeAsync((control) {
final source = StreamController<UpdateNotification>(sync: true);
final events = <UpdateNotification>[];
UpdateNotification.throttleStream(source.stream, timeout)
.listen(events.add);
source.add(UpdateNotification({'a'}));
control.elapse(halfTimeout);
expect(events, isEmpty);
source.add(UpdateNotification({'b'}));
control.elapse(halfTimeout);
expect(events, [
UpdateNotification({'a', 'b'})
]);
});
});
test('forwards cancellations', () {
fakeAsync((control) {
var cancelled = false;
final source = StreamController<UpdateNotification>(sync: true)
..onCancel = () => cancelled = true;
final sub = UpdateNotification.throttleStream(source.stream, timeout)
.listen((_) => fail('unexpected event'),
onDone: () => fail('unexpected done'));
source.add(UpdateNotification({'a'}));
control.elapse(halfTimeout);
sub.cancel();
control.flushTimers();
expect(cancelled, isTrue);
expect(control.pendingTimers, isEmpty);
});
});
test('closes when source closes', () {
fakeAsync((control) {
final source = StreamController<UpdateNotification>(sync: true)
..onCancel = () => Future.value();
final events = <UpdateNotification>[];
var done = false;
UpdateNotification.throttleStream(source.stream, timeout)
.listen(events.add, onDone: () => done = true);
source
// These two are combined due to throttleFirst
..add(UpdateNotification({'a'}))
..add(UpdateNotification({'b'}))
..close();
control.flushTimers();
expect(events, [
UpdateNotification({'a', 'b'})
]);
expect(done, isTrue);
expect(control.pendingTimers, isEmpty);
});
});
test('closes when source closes after delay', () {
fakeAsync((control) {
final source = StreamController<UpdateNotification>(sync: true)
..onCancel = () => Future.value();
final events = <UpdateNotification>[];
var done = false;
UpdateNotification.throttleStream(source.stream, timeout)
.listen(events.add, onDone: () => done = true);
control.elapse(const Duration(hours: 1));
source.close();
control.flushTimers();
expect(events, isEmpty);
expect(done, isTrue);
expect(control.pendingTimers, isEmpty);
});
});
});
test('filter tables', () async {
final source = StreamController<UpdateNotification>(sync: true);
final events = <UpdateNotification>[];
final subscription = UpdateNotification.filterTablesTransformer(['a'])
.bind(source.stream)
.listen(events.add);
source.add(UpdateNotification({'a', 'b'}));
expect(events, hasLength(1));
source.add(UpdateNotification({'b'}));
expect(events, hasLength(1));
await subscription.cancel();
expect(source.hasListener, isFalse);
});
});
}