/
with_guarantees_test.dart
193 lines (157 loc) · 5.97 KB
/
with_guarantees_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
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';
import 'utils.dart';
void main() {
var streamController;
var sinkController;
var channel;
setUp(() {
streamController = new StreamController();
sinkController = new StreamController();
channel = new StreamChannel.withGuarantees(
streamController.stream, sinkController.sink);
});
group("with a broadcast stream", () {
setUp(() {
streamController = new StreamController.broadcast();
channel = new StreamChannel.withGuarantees(
streamController.stream, sinkController.sink);
});
test("buffers events", () async {
streamController.add(1);
streamController.add(2);
streamController.add(3);
await pumpEventQueue();
expect(channel.stream.toList(), completion(equals([1, 2, 3])));
streamController.close();
});
test("only allows a single subscription", () {
channel.stream.listen(null);
expect(() => channel.stream.listen(null), throwsStateError);
});
});
test("closing the event sink causes the stream to close before it emits any "
"more events", () {
streamController.add(1);
streamController.add(2);
streamController.add(3);
expect(channel.stream.listen(expectAsync((event) {
if (event == 2) channel.sink.close();
}, count: 2)).asFuture(), completes);
});
test("after the stream closes, the sink ignores events", () async {
streamController.close();
// Wait for the done event to be delivered.
await channel.stream.toList();
channel.sink.add(1);
channel.sink.add(2);
channel.sink.add(3);
channel.sink.close();
// None of our channel.sink additions should make it to the other endpoint.
sinkController.stream.listen(
expectAsync((_) {}, count: 0),
onDone: expectAsync(() {}, count: 0));
await pumpEventQueue();
});
test("canceling the stream's subscription has no effect on the sink",
() async {
channel.stream.listen(null).cancel();
await pumpEventQueue();
channel.sink.add(1);
channel.sink.add(2);
channel.sink.add(3);
channel.sink.close();
expect(sinkController.stream.toList(), completion(equals([1, 2, 3])));
});
test("canceling the stream's subscription doesn't stop a done event",
() async {
channel.stream.listen(null).cancel();
await pumpEventQueue();
streamController.close();
await pumpEventQueue();
channel.sink.add(1);
channel.sink.add(2);
channel.sink.add(3);
channel.sink.close();
// The sink should be ignoring events because the stream closed.
sinkController.stream.listen(
expectAsync((_) {}, count: 0),
onDone: expectAsync(() {}, count: 0));
await pumpEventQueue();
});
test("forwards errors to the other endpoint", () {
channel.sink.addError("error");
expect(sinkController.stream.first, throwsA("error"));
});
test("Sink.done completes once the stream is done", () {
channel.stream.listen(null);
expect(channel.sink.done, completes);
streamController.close();
});
test("events can't be added to an explicitly-closed sink", () {
sinkController.stream.listen(null); // Work around sdk#19095.
expect(channel.sink.close(), completes);
expect(() => channel.sink.add(1), throwsStateError);
expect(() => channel.sink.addError("oh no"), throwsStateError);
expect(() => channel.sink.addStream(new Stream.fromIterable([])),
throwsStateError);
});
test("events can't be added while a stream is being added", () {
var controller = new StreamController();
channel.sink.addStream(controller.stream);
expect(() => channel.sink.add(1), throwsStateError);
expect(() => channel.sink.addError("oh no"), throwsStateError);
expect(() => channel.sink.addStream(new Stream.fromIterable([])),
throwsStateError);
expect(() => channel.sink.close(), throwsStateError);
controller.close();
});
group("with allowSinkErrors: false", () {
setUp(() {
streamController = new StreamController();
sinkController = new StreamController();
channel = new StreamChannel.withGuarantees(
streamController.stream, sinkController.sink, allowSinkErrors: false);
});
test("forwards errors to Sink.done but not the stream", () {
channel.sink.addError("oh no");
expect(channel.sink.done, throwsA("oh no"));
sinkController.stream.listen(null,
onError: expectAsync((_) {}, count: 0));
});
test("adding an error causes the stream to emit a done event", () {
expect(channel.sink.done, throwsA("oh no"));
streamController.add(1);
streamController.add(2);
streamController.add(3);
expect(channel.stream.listen(expectAsync((event) {
if (event == 2) channel.sink.addError("oh no");
}, count: 2)).asFuture(), completes);
});
test("adding an error closes the inner sink", () {
channel.sink.addError("oh no");
expect(channel.sink.done, throwsA("oh no"));
expect(sinkController.stream.toList(), completion(isEmpty));
});
test("adding an error via via addStream causes the stream to emit a done "
"event", () async {
var canceled = false;
var controller = new StreamController(onCancel: () {
canceled = true;
});
// This future shouldn't get the error, because it's sent to [Sink.done].
expect(channel.sink.addStream(controller.stream), completes);
controller.addError("oh no");
expect(channel.sink.done, throwsA("oh no"));
await pumpEventQueue();
expect(canceled, isTrue);
// Even though the sink is closed, this shouldn't throw an error because
// the user didn't explicitly close it.
channel.sink.add(1);
});
});
}