| 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:unittest/unittest.dart"; | 6 import "package:unittest/unittest.dart"; |
| 7 | 7 |
| 8 main() { | 8 main() { |
| 9 const ms5 = const Duration(milliseconds: 5); | 9 const ms5 = const Duration(milliseconds: 5); |
| 10 const twoSecs = const Duration(seconds: 2); | 10 const twoSecs = const Duration(seconds: 2); |
| 11 | 11 |
| 12 test("stream timeout", () { | 12 test("stream timeout", () { |
| 13 StreamController c = new StreamController(); | 13 StreamController c = new StreamController(); |
| 14 Stream tos = c.stream.timeout(ms5); | 14 Stream tos = c.stream.timeout(ms5); |
| 15 expect(tos.isBroadcast, false); | 15 expect(tos.isBroadcast, false); |
| 16 tos.handleError(expectAsync2((e, s) { | 16 tos.handleError(expectAsync((e, s) { |
| 17 expect(e, new isInstanceOf<TimeoutException>()); | 17 expect(e, new isInstanceOf<TimeoutException>()); |
| 18 expect(s, null); | 18 expect(s, null); |
| 19 })).listen((v){ fail("Unexpected event"); }); | 19 })).listen((v){ fail("Unexpected event"); }); |
| 20 }); | 20 }); |
| 21 | 21 |
| 22 test("stream timeout add events", () { | 22 test("stream timeout add events", () { |
| 23 StreamController c = new StreamController(); | 23 StreamController c = new StreamController(); |
| 24 Stream tos = c.stream.timeout(ms5, onTimeout: (sink) { | 24 Stream tos = c.stream.timeout(ms5, onTimeout: (sink) { |
| 25 sink.add(42); | 25 sink.add(42); |
| 26 sink.addError("ERROR"); | 26 sink.addError("ERROR"); |
| 27 sink.close(); | 27 sink.close(); |
| 28 }); | 28 }); |
| 29 expect(tos.isBroadcast, false); | 29 expect(tos.isBroadcast, false); |
| 30 tos.listen(expectAsync1((v) { expect(v, 42); }), | 30 tos.listen(expectAsync((v) { expect(v, 42); }), |
| 31 onError: expectAsync2((e, s) { expect(e, "ERROR"); }), | 31 onError: expectAsync((e, s) { expect(e, "ERROR"); }), |
| 32 onDone: expectAsync0((){})); | 32 onDone: expectAsync((){})); |
| 33 }); | 33 }); |
| 34 | 34 |
| 35 test("stream no timeout", () { | 35 test("stream no timeout", () { |
| 36 StreamController c = new StreamController(); | 36 StreamController c = new StreamController(); |
| 37 Stream tos = c.stream.timeout(twoSecs); | 37 Stream tos = c.stream.timeout(twoSecs); |
| 38 int ctr = 0; | 38 int ctr = 0; |
| 39 tos.listen((v) { | 39 tos.listen((v) { |
| 40 expect(v, 42); | 40 expect(v, 42); |
| 41 ctr++; | 41 ctr++; |
| 42 }, | 42 }, |
| 43 onError: (e, s) { fail("No error expected"); }, | 43 onError: (e, s) { fail("No error expected"); }, |
| 44 onDone: expectAsync0(() { | 44 onDone: expectAsync(() { |
| 45 expect(ctr, 2); | 45 expect(ctr, 2); |
| 46 })); | 46 })); |
| 47 expect(tos.isBroadcast, false); | 47 expect(tos.isBroadcast, false); |
| 48 c..add(42)..add(42)..close(); // Faster than a timeout! | 48 c..add(42)..add(42)..close(); // Faster than a timeout! |
| 49 }); | 49 }); |
| 50 | 50 |
| 51 test("stream timeout after events", () { | 51 test("stream timeout after events", () { |
| 52 StreamController c = new StreamController(); | 52 StreamController c = new StreamController(); |
| 53 Stream tos = c.stream.timeout(twoSecs); | 53 Stream tos = c.stream.timeout(twoSecs); |
| 54 expect(tos.isBroadcast, false); | 54 expect(tos.isBroadcast, false); |
| 55 int ctr = 0; | 55 int ctr = 0; |
| 56 tos.listen((v) { | 56 tos.listen((v) { |
| 57 expect(v, 42); | 57 expect(v, 42); |
| 58 ctr++; | 58 ctr++; |
| 59 }, | 59 }, |
| 60 onError: expectAsync2((e, s) { | 60 onError: expectAsync((e, s) { |
| 61 expect(ctr, 2); | 61 expect(ctr, 2); |
| 62 expect(e, new isInstanceOf<TimeoutException>()); | 62 expect(e, new isInstanceOf<TimeoutException>()); |
| 63 })); | 63 })); |
| 64 c..add(42)..add(42); // No close, timeout after two events. | 64 c..add(42)..add(42); // No close, timeout after two events. |
| 65 }); | 65 }); |
| 66 | 66 |
| 67 test("broadcast stream timeout", () { | 67 test("broadcast stream timeout", () { |
| 68 StreamController c = new StreamController.broadcast(); | 68 StreamController c = new StreamController.broadcast(); |
| 69 Stream tos = c.stream.timeout(ms5); | 69 Stream tos = c.stream.timeout(ms5); |
| 70 expect(tos.isBroadcast, false); | 70 expect(tos.isBroadcast, false); |
| 71 tos.handleError(expectAsync2((e, s) { | 71 tos.handleError(expectAsync((e, s) { |
| 72 expect(e, new isInstanceOf<TimeoutException>()); | 72 expect(e, new isInstanceOf<TimeoutException>()); |
| 73 expect(s, null); | 73 expect(s, null); |
| 74 })).listen((v){ fail("Unexpected event"); }); | 74 })).listen((v){ fail("Unexpected event"); }); |
| 75 }); | 75 }); |
| 76 | 76 |
| 77 test("asBroadcast stream timeout", () { | 77 test("asBroadcast stream timeout", () { |
| 78 StreamController c = new StreamController.broadcast(); | 78 StreamController c = new StreamController.broadcast(); |
| 79 Stream tos = c.stream.asBroadcastStream().timeout(ms5); | 79 Stream tos = c.stream.asBroadcastStream().timeout(ms5); |
| 80 expect(tos.isBroadcast, false); | 80 expect(tos.isBroadcast, false); |
| 81 tos.handleError(expectAsync2((e, s) { | 81 tos.handleError(expectAsync((e, s) { |
| 82 expect(e, new isInstanceOf<TimeoutException>()); | 82 expect(e, new isInstanceOf<TimeoutException>()); |
| 83 expect(s, null); | 83 expect(s, null); |
| 84 })).listen((v){ fail("Unexpected event"); }); | 84 })).listen((v){ fail("Unexpected event"); }); |
| 85 }); | 85 }); |
| 86 | 86 |
| 87 test("mapped stream timeout", () { | 87 test("mapped stream timeout", () { |
| 88 StreamController c = new StreamController(); | 88 StreamController c = new StreamController(); |
| 89 Stream tos = c.stream.map((x) => 2 * x).timeout(ms5); | 89 Stream tos = c.stream.map((x) => 2 * x).timeout(ms5); |
| 90 expect(tos.isBroadcast, false); | 90 expect(tos.isBroadcast, false); |
| 91 tos.handleError(expectAsync2((e, s) { | 91 tos.handleError(expectAsync((e, s) { |
| 92 expect(e, new isInstanceOf<TimeoutException>()); | 92 expect(e, new isInstanceOf<TimeoutException>()); |
| 93 expect(s, null); | 93 expect(s, null); |
| 94 })).listen((v){ fail("Unexpected event"); }); | 94 })).listen((v){ fail("Unexpected event"); }); |
| 95 }); | 95 }); |
| 96 | 96 |
| 97 test("events prevent timeout", () { | 97 test("events prevent timeout", () { |
| 98 Stopwatch sw = new Stopwatch(); | 98 Stopwatch sw = new Stopwatch(); |
| 99 StreamController c = new StreamController(); | 99 StreamController c = new StreamController(); |
| 100 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) { | 100 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) { |
| 101 int elapsed = sw.elapsedMilliseconds; | 101 int elapsed = sw.elapsedMilliseconds; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 114 new Timer.periodic(ms5, (timer) { | 114 new Timer.periodic(ms5, (timer) { |
| 115 sw.reset(); | 115 sw.reset(); |
| 116 c.add(42); | 116 c.add(42); |
| 117 if (--ctr == 0) { | 117 if (--ctr == 0) { |
| 118 timer.cancel(); | 118 timer.cancel(); |
| 119 c.close(); | 119 c.close(); |
| 120 } | 120 } |
| 121 }); | 121 }); |
| 122 sw.start(); | 122 sw.start(); |
| 123 | 123 |
| 124 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){})); | 124 tos.listen((v) { expect(v, 42);}, onDone: expectAsync((){})); |
| 125 }); | 125 }); |
| 126 | 126 |
| 127 test("errors prevent timeout", () { | 127 test("errors prevent timeout", () { |
| 128 Stopwatch sw = new Stopwatch(); | 128 Stopwatch sw = new Stopwatch(); |
| 129 StreamController c = new StreamController(); | 129 StreamController c = new StreamController(); |
| 130 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) { | 130 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) { |
| 131 int elapsed = sw.elapsedMilliseconds; | 131 int elapsed = sw.elapsedMilliseconds; |
| 132 if (elapsed > 250) { | 132 if (elapsed > 250) { |
| 133 // This should not happen, but it does occasionally. | 133 // This should not happen, but it does occasionally. |
| 134 // Starving the periodic timer has made the test useless. | 134 // Starving the periodic timer has made the test useless. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 timer.cancel(); | 148 timer.cancel(); |
| 149 c.close(); | 149 c.close(); |
| 150 } | 150 } |
| 151 }); | 151 }); |
| 152 sw.start(); | 152 sw.start(); |
| 153 | 153 |
| 154 tos.listen((_) {}, | 154 tos.listen((_) {}, |
| 155 onError: (e, s) { | 155 onError: (e, s) { |
| 156 expect(e, "ERROR"); | 156 expect(e, "ERROR"); |
| 157 }, | 157 }, |
| 158 onDone: expectAsync0((){})); | 158 onDone: expectAsync((){})); |
| 159 }); | 159 }); |
| 160 | 160 |
| 161 test("closing prevents timeout", () { | 161 test("closing prevents timeout", () { |
| 162 StreamController c = new StreamController(); | 162 StreamController c = new StreamController(); |
| 163 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) { | 163 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) { |
| 164 fail("Timeout not prevented by close"); | 164 fail("Timeout not prevented by close"); |
| 165 }); | 165 }); |
| 166 tos.listen((_) {}, onDone: expectAsync0((){})); | 166 tos.listen((_) {}, onDone: expectAsync((){})); |
| 167 c.close(); | 167 c.close(); |
| 168 }); | 168 }); |
| 169 | 169 |
| 170 test("pausing prevents timeout", () { | 170 test("pausing prevents timeout", () { |
| 171 StreamController c = new StreamController(); | 171 StreamController c = new StreamController(); |
| 172 Stream tos = c.stream.timeout(ms5, onTimeout: (_) { | 172 Stream tos = c.stream.timeout(ms5, onTimeout: (_) { |
| 173 fail("Timeout not prevented by close"); | 173 fail("Timeout not prevented by close"); |
| 174 }); | 174 }); |
| 175 var subscription = tos.listen((_) {}, onDone: expectAsync0((){})); | 175 var subscription = tos.listen((_) {}, onDone: expectAsync((){})); |
| 176 subscription.pause(); | 176 subscription.pause(); |
| 177 new Timer(twoSecs, () { | 177 new Timer(twoSecs, () { |
| 178 c.close(); | 178 c.close(); |
| 179 subscription.resume(); | 179 subscription.resume(); |
| 180 }); | 180 }); |
| 181 }); | 181 }); |
| 182 } | 182 } |
| OLD | NEW |