OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 import 'package:async_helper/async_helper.dart'; |
| 6 import "package:expect/expect.dart"; |
| 7 import 'dart:async'; |
| 8 |
| 9 /// StreamController in nested runZoned. Trivial. |
| 10 test1() { |
| 11 var events = []; |
| 12 var done = new Completer(); |
| 13 runZoned(() { |
| 14 runZoned(() { |
| 15 var c = new StreamController(); |
| 16 c.stream.listen( |
| 17 (x) => events.add("stream: $x"), |
| 18 onError: (x) => events.add("stream: error $x"), |
| 19 onDone: done.complete); |
| 20 c.add(1); |
| 21 c.addError(2); |
| 22 c.close(); |
| 23 }, onError: (x) => events.add("rza: error $x")); |
| 24 }, onError: (x) => events.add("rzb: error $x")); |
| 25 return [done.future, () { |
| 26 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 27 }]; |
| 28 } |
| 29 |
| 30 /// Adding errors to the stream controller from an outside zone. |
| 31 test2() { |
| 32 var events = []; |
| 33 var done = new Completer(); |
| 34 runZoned(() { |
| 35 var c; |
| 36 runZoned(() { |
| 37 c = new StreamController(); |
| 38 c.stream.listen( |
| 39 (x) => events.add("stream: $x"), |
| 40 onError: (x) => events.add("stream: error $x"), |
| 41 onDone: done.complete); |
| 42 }, onError: (x) => events.add("rza: error $x")); |
| 43 c.add(1); |
| 44 c.addError(2); |
| 45 c.close(); |
| 46 }, onError: (x) => events.add("rzb: error $x")); |
| 47 return [done.future, () { |
| 48 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 49 }]; |
| 50 } |
| 51 |
| 52 /// Adding errors to the stream controller from a more nested zone. |
| 53 test3() { |
| 54 var events = []; |
| 55 var done = new Completer(); |
| 56 runZoned(() { |
| 57 var c = new StreamController(); |
| 58 c.stream.listen( |
| 59 (x) => events.add("stream: $x"), |
| 60 onError: (x) => events.add("stream: error $x"), |
| 61 onDone: done.complete); |
| 62 runZoned(() { |
| 63 c.add(1); |
| 64 c.addError(2); |
| 65 c.close(); |
| 66 }, onError: (x) => events.add("rza: error $x")); |
| 67 }, onError: (x) => events.add("rzb: error $x")); |
| 68 return [done.future, () { |
| 69 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 70 }]; |
| 71 } |
| 72 |
| 73 /// Feeding a stream from a different zone into another controller. |
| 74 test4() { |
| 75 var events = []; |
| 76 var done = new Completer(); |
| 77 runZoned(() { |
| 78 var c = new StreamController(); |
| 79 c.stream.listen( |
| 80 (x) => events.add("stream: $x"), |
| 81 onError: (x) => events.add("stream: error $x"), |
| 82 onDone: done.complete); |
| 83 runZoned(() { |
| 84 var c2 = new StreamController(); |
| 85 c.addStream(c2.stream).whenComplete(c.close); |
| 86 c2.add(1); |
| 87 c2.addError(2); |
| 88 c2.close(); |
| 89 }, onError: (x) => events.add("rza: error $x")); |
| 90 }, onError: (x) => events.add("rzb: error $x")); |
| 91 return [done.future, () { |
| 92 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 93 }]; |
| 94 } |
| 95 |
| 96 /// Feeding a stream from a different zone into another controller. |
| 97 /// This time nesting is reversed wrt test4. |
| 98 test5() { |
| 99 var events = []; |
| 100 var done = new Completer(); |
| 101 runZoned(() { |
| 102 var c; |
| 103 runZoned(() { |
| 104 c = new StreamController(); |
| 105 c.stream.listen( |
| 106 (x) => events.add("stream: $x"), |
| 107 onError: (x) => events.add("stream: error $x"), |
| 108 onDone: done.complete); |
| 109 }, onError: (x) => events.add("rza: error $x")); |
| 110 var c2 = new StreamController(); |
| 111 c.addStream(c2.stream).whenComplete(c.close); |
| 112 c2.add(1); |
| 113 c2.addError(2); |
| 114 c2.close(); |
| 115 }, onError: (x) => events.add("rzb: error $x")); |
| 116 return [done.future, () { |
| 117 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 118 }]; |
| 119 } |
| 120 |
| 121 |
| 122 test6() { |
| 123 var events = []; |
| 124 var done = new Completer(); |
| 125 var c; |
| 126 runZoned(() { |
| 127 c = new StreamController(); |
| 128 c.stream.listen( |
| 129 (x) => events.add("stream: $x"), |
| 130 onError: (x) => events.add("stream: error $x"), |
| 131 onDone: done.complete); |
| 132 }, onError: (x) => events.add("rza: error $x")); |
| 133 runZoned(() { |
| 134 var c2 = new StreamController(); |
| 135 c.addStream(c2.stream).whenComplete(c.close); |
| 136 c2.add(1); |
| 137 c2.addError(2); |
| 138 c2.close(); |
| 139 }, onError: (x) => events.add("rzb: error $x")); |
| 140 return [done.future, () { |
| 141 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 142 }]; |
| 143 } |
| 144 |
| 145 /// Adding errors to the stream controller from a parallel zone. |
| 146 test7() { |
| 147 var events = []; |
| 148 var done = new Completer(); |
| 149 var c; |
| 150 runZoned(() { |
| 151 c = new StreamController(); |
| 152 c.stream.listen( |
| 153 (x) => events.add("stream: $x"), |
| 154 onError: (x) => events.add("stream: error $x"), |
| 155 onDone: done.complete); |
| 156 }, onError: (x) => events.add("rza: error $x")); |
| 157 runZoned(() { |
| 158 c.add(1); |
| 159 c.addError(2); |
| 160 c.close(); |
| 161 }, onError: (x) => events.add("rzb: error $x")); |
| 162 return [done.future, () { |
| 163 Expect.listEquals(["stream: 1", "stream: error 2"], events); |
| 164 }]; |
| 165 } |
| 166 |
| 167 main() { |
| 168 asyncStart(); |
| 169 |
| 170 var tests = [ |
| 171 test1(), |
| 172 test2(), |
| 173 test3(), |
| 174 test4(), |
| 175 test5(), |
| 176 test6(), |
| 177 test7(), |
| 178 ]; |
| 179 |
| 180 Future.wait(tests.map((l) => l.first)).then((_) { |
| 181 // Give time to complete all pending actions. |
| 182 Timer.run(() { |
| 183 tests.forEach((l) => (l.last)()); |
| 184 asyncEnd(); |
| 185 }); |
| 186 }); |
| 187 } |
OLD | NEW |