Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import "dart:async"; | 5 import "dart:async"; |
| 6 import "package:sequence_zip/stream_zip.dart"; | 6 import "package:sequence_zip/stream_zip.dart"; |
| 7 import "package:unittest/unittest.dart"; | 7 import "package:unittest/unittest.dart"; |
| 8 | 8 |
| 9 /// Create an error with the same values as [base], except that it throwsA | 9 /// Create an error with the same values as [base], except that it throwsA |
| 10 /// when seeing the value [errorValue]. | 10 /// when seeing the value [errorValue]. |
| 11 Stream streamError(Stream base, int errorValue) { | 11 Stream streamError(Stream base, int errorValue, error) { |
| 12 return base.map((x) => (x == errorValue) ? throw "BAD" : x); | 12 return base.map((x) => (x == errorValue) ? throw error : x); |
| 13 } | 13 } |
| 14 | 14 |
| 15 /// Make a [Stream] from an [Iterable] by adding events to a stream controller | 15 /// Make a [Stream] from an [Iterable] by adding events to a stream controller |
| 16 /// at periodic intervals. | 16 /// at periodic intervals. |
| 17 Stream mks(Iterable iterable) { | 17 Stream mks(Iterable iterable) { |
| 18 Iterator iterator = iterable.iterator; | 18 Iterator iterator = iterable.iterator; |
| 19 StreamController controller = new StreamController(); | 19 StreamController controller = new StreamController(); |
| 20 // Some varying time between 3 and 10 ms. | 20 // Some varying time between 3 and 10 ms. |
| 21 int ms = ((++ctr) * 5) % 7 + 3; | 21 int ms = ((++ctr) * 5) % 7 + 3; |
| 22 new Timer.periodic(new Duration(milliseconds: ms), (Timer timer) { | 22 new Timer.periodic(new Duration(milliseconds: ms), (Timer timer) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 Stream st2 = new Stream.periodic(const Duration(milliseconds: 5), | 93 Stream st2 = new Stream.periodic(const Duration(milliseconds: 5), |
| 94 (x) => x + 4).take(3); | 94 (x) => x + 4).take(3); |
| 95 StreamController c = new StreamController.broadcast(); | 95 StreamController c = new StreamController.broadcast(); |
| 96 Stream st3 = c.stream; | 96 Stream st3 = c.stream; |
| 97 testZip([st1, st2, st3], | 97 testZip([st1, st2, st3], |
| 98 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]); | 98 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]); |
| 99 c..add(7)..add(8)..add(9)..close(); | 99 c..add(7)..add(8)..add(9)..close(); |
| 100 }); | 100 }); |
| 101 | 101 |
| 102 test("Error 1", () { | 102 test("Error 1", () { |
| 103 expect(new StreamZip([streamError(mks([1, 2, 3]), 2), | 103 expect(new StreamZip([streamError(mks([1, 2, 3]), 2, "BAD-1"), |
| 104 mks([4, 5, 6]), | 104 mks([4, 5, 6]), |
| 105 mks([7, 8, 9])]).toList(), | 105 mks([7, 8, 9])]).toList(), |
| 106 throwsA(equals("BAD"))); | 106 throwsA(equals("BAD-1"))); |
| 107 }); | 107 }); |
| 108 | 108 |
| 109 test("Error 2", () { | 109 test("Error 2", () { |
| 110 expect(new StreamZip([mks([1, 2, 3]), | 110 expect(new StreamZip([mks([1, 2, 3]), |
| 111 streamError(mks([4, 5, 6]), 5), | 111 streamError(mks([4, 5, 6]), 5, "BAD-2"), |
| 112 mks([7, 8, 9])]).toList(), | 112 mks([7, 8, 9])]).toList(), |
| 113 throwsA(equals("BAD"))); | 113 throwsA(equals("BAD-2"))); |
| 114 }); | 114 }); |
| 115 | 115 |
| 116 test("Error 3", () { | 116 test("Error 3", () { |
| 117 expect(new StreamZip([mks([1, 2, 3]), | 117 expect(new StreamZip([mks([1, 2, 3]), |
| 118 mks([4, 5, 6]), | 118 mks([4, 5, 6]), |
| 119 streamError(mks([7, 8, 9]), 8)]).toList(), | 119 streamError(mks([7, 8, 9]), 8, "BAD-3")]).toList(), |
| 120 throwsA(equals("BAD"))); | 120 throwsA(equals("BAD-3"))); |
| 121 }); | 121 }); |
| 122 | 122 |
| 123 test("Error at end", () { | 123 test("Error at end", () { |
| 124 expect(new StreamZip([mks([1, 2, 3]), | 124 expect(new StreamZip([mks([1, 2, 3]), |
| 125 streamError(mks([4, 5, 6]), 6), | 125 streamError(mks([4, 5, 6]), 6, "BAD-4"), |
| 126 mks([7, 8, 9])]).toList(), | 126 mks([7, 8, 9])]).toList(), |
| 127 throwsA(equals("BAD"))); | 127 throwsA(equals("BAD-4"))); |
| 128 }); | 128 }); |
| 129 | 129 |
| 130 test("Error before first end", () { | 130 test("Error before first end", () { |
| 131 // StreamControllers' streams with no "close" called will never be done, | 131 // StreamControllers' streams with no "close" called will never be done, |
| 132 // so the fourth event of the first stream is guaranteed to come first. | 132 // so the fourth event of the first stream is guaranteed to come first. |
| 133 expect(new StreamZip( | 133 expect(new StreamZip( |
| 134 [streamError(mks([1, 2, 3, 4]), 4), | 134 [streamError(mks([1, 2, 3, 4]), 4, "BAD-5"), |
| 135 (new StreamController()..add(4)..add(5)..add(6)).stream, | 135 (new StreamController()..add(4)..add(5)..add(6)).stream, |
| 136 (new StreamController()..add(7)..add(8)..add(9)).stream] | 136 (new StreamController()..add(7)..add(8)..add(9)).stream] |
| 137 ).toList(), | 137 ).toList(), |
| 138 throwsA(equals("BAD"))); | 138 throwsA(equals("BAD-5"))); |
| 139 }); | 139 }); |
| 140 | 140 |
| 141 test("Error after first end", () { | 141 test("Error after first end", () { |
| 142 StreamController controller = new StreamController(); | 142 StreamController controller = new StreamController(); |
| 143 controller..add(7)..add(8)..add(9); | 143 controller..add(7)..add(8)..add(9); |
| 144 int ctr = 2; | 144 // Transformer that puts error into controller one of the first two streams |
|
Søren Gjesse
2013/10/07 08:19:31
Comment missing a word or two.
Lasse Reichstein Nielsen
2013/10/07 08:27:27
One word added. :)
| |
| 145 Function addErrorIf(x) { | 145 // have sent a done event. |
| 146 return (y) { | 146 StreamTransformer trans = new StreamTransformer(handleDone: (EventSink s) { |
| 147 // Adds error to controller after both of the first two streams have | 147 Timer.run(() { controller.addError("BAD-6"); }); |
| 148 // provided all three elements. | 148 s.close(); |
| 149 if (x == y && --ctr == 0) Timer.run(() { controller.addError("BAD"); }); | 149 }); |
| 150 return y; | 150 testZip([mks([1, 2, 3]).transform(trans), |
| 151 }; | 151 mks([4, 5, 6]).transform(trans), |
| 152 } | |
| 153 testZip([mks([1, 2, 3].map(addErrorIf(3))), | |
| 154 mks([4, 5, 6].map(addErrorIf(6))), | |
| 155 controller.stream], | 152 controller.stream], |
| 156 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]); | 153 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]); |
| 157 }); | 154 }); |
| 158 | 155 |
| 159 test("Pause/Resume", () { | 156 test("Pause/Resume", () { |
| 160 var done = expectAsync0((){}); // Call to complete test. | 157 var done = expectAsync0((){}); // Call to complete test. |
| 161 | 158 |
| 162 int sc1p = 0; | 159 int sc1p = 0; |
| 163 StreamController c1 = new StreamController( | 160 StreamController c1 = new StreamController( |
| 164 onPause: () { | 161 onPause: () { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 } else if (ctr == 2) { | 221 } else if (ctr == 2) { |
| 225 sub.pause(); | 222 sub.pause(); |
| 226 new Future.delayed(const Duration(milliseconds: 25)).then((_) { | 223 new Future.delayed(const Duration(milliseconds: 25)).then((_) { |
| 227 sub.resume(); | 224 sub.resume(); |
| 228 }); | 225 }); |
| 229 } | 226 } |
| 230 ctr++; | 227 ctr++; |
| 231 }, count: 4)); | 228 }, count: 4)); |
| 232 }); | 229 }); |
| 233 } | 230 } |
| OLD | NEW |