Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: test/subscription_stream_test.dart

Issue 1149563010: Add new features to package:async. (Closed) Base URL: https://github.com/dart-lang/async@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/src/stream_events.dart ('K') | « test/stream_events_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library async.subscription_stream_test;
6
7 import "dart:async";
8
9 import "package:async/async.dart" show SubscriptionStream;
10 import "package:test/test.dart";
ahe 2015/06/08 16:55:16 This most likely makes this a really slow test to
Lasse Reichstein Nielsen 2015/06/09 07:33:53 If the test package is slow on dart2js, then it sh
11
12 main() {
13 test("simple", () async {
14 var stream = createStream();
15 var sub = stream.listen(null);
16 var ss = new SubscriptionStream<int>(sub);
17 await sleep(10);
18 expect(ss.toList(), completion([1, 2, 3, 4]));
19 });
20
21 test("after 2", () async {
22 var stream = createStream();
23 int skips = 0;
24 var c = new Completer();
25 var sub;
26 sub = stream.listen((v) {
27 ++skips;
28 expect(v, skips);
29 if (skips == 2) c.complete(new SubscriptionStream<int>(sub));
30 });
31 Stream<int> ss = await c.future;
32 await sleep(10);
33 expect(ss.toList(), completion([3, 4]));
34 });
35
36 group("cancelOnError", () {
37 for (var original in [false, true]) {
38 group(original ? "yes" : "no", () {
39 test("no", () async {
40 var stream = createErrorStream();
41 var sub = stream.listen(null, cancelOnError: original);
42 var ss = new SubscriptionStream(sub, isCancelOnError: original);
43 var c = new Completer();
44 var l = [];
45 var sub2 = ss.listen(l.add, onError: l.add, onDone: c.complete);
46 await c.future;
47 var expected = [1, 2, "To err is divine!"];
48 if (!original) expected.add(4);
49 expect(l, expected);
50 });
51
52 test("yes", () async {
53 var stream = createErrorStream();
54 var sub = stream.listen(null, cancelOnError: original);
55 var ss = new SubscriptionStream(sub, isCancelOnError: original);
56 var c = new Completer();
57 var l = [];
58 var sub2 = ss.listen(l.add,
59 onError: (v) { l.add(v); c.complete(); },
60 onDone: () => throw "should not happen",
61 cancelOnError: true);
62 await c.future;
63 await sleep(10);
64 expect(l, [1, 2, "To err is divine!"]);
65 });
66 });
67
68 test("no-asFuture-noerr", () async {
69 var stream = createStream();
70 var sub = stream.listen(null, cancelOnError: original);
71 var ss = new SubscriptionStream(sub, isCancelOnError: original);
72 var sub2 = ss.listen(null);
73 expect(sub2.asFuture(42), completion(42));
74 });
75
76 test("no-asFuture-err", () async {
77 var stream = createErrorStream();
78 var sub = stream.listen(null, cancelOnError: original);
79 var ss = new SubscriptionStream(sub, isCancelOnError: original);
80 var sub2 = ss.listen(null);
81 expect(sub2.asFuture(), throws);
82 });
83
84 test("yes-asFuture-noerr", () async {
85 var stream = createStream();
86 var sub = stream.listen(null, cancelOnError: original);
87 var ss = new SubscriptionStream(sub, isCancelOnError: original);
88 var sub2 = ss.listen(null, cancelOnError: true);
89 expect(sub2.asFuture(42), completion(42));
90 });
91
92 test("yes-asFuture-err", () async {
93 var stream = createErrorStream();
94 var sub = stream.listen(null, cancelOnError: original);
95 var ss = new SubscriptionStream(sub, isCancelOnError: original);
96 var sub2 = ss.listen(null, cancelOnError: true);
97 expect(sub2.asFuture(), throws);
98 });
99 }
100 });
101 }
102
103 const MS = const Duration(milliseconds: 1);
104 Future sleep(int n) => new Future.delayed(MS * n);
105
106 Stream<int> createStream() async* {
107 yield 1;
108 await sleep(20);
109 yield 2;
110 await sleep(10);
111 yield 3;
112 await sleep(15);
113 yield 4;
114 }
115
116 Stream<int> createErrorStream() {
117 StreamController controller = new StreamController<int>();
118 () async {
119 controller.add(1);
120 await sleep(20);
121 controller.add(2);
122 await sleep(10);
123 controller.addError("To err is divine!");
124 await sleep(15);
125 controller.add(4);
126 await sleep(5);
127 controller.close();
128 }();
129 return controller.stream;
130 }
131
132 Stream<int> createLongStream() async* {
133 for (int i = 0; i < 200; i++) yield i;
134 }
OLDNEW
« lib/src/stream_events.dart ('K') | « test/stream_events_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698