OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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 'dart:async'; |
| 6 import 'dart:isolate'; |
| 7 |
| 8 testFutureAsStreamCompleteAfter() { |
| 9 var completer = new Completer(); |
| 10 bool gotValue = false; |
| 11 var port = new ReceivePort(); |
| 12 completer.future.asStream().listen( |
| 13 (data) { |
| 14 Expect.isFalse(gotValue); |
| 15 gotValue = true; |
| 16 Expect.equals("value", data); |
| 17 }, |
| 18 onDone: () { |
| 19 Expect.isTrue(gotValue); |
| 20 port.close(); |
| 21 }); |
| 22 completer.complete("value"); |
| 23 } |
| 24 |
| 25 testFutureAsStreamCompleteBefore() { |
| 26 var completer = new Completer(); |
| 27 bool gotValue = false; |
| 28 var port = new ReceivePort(); |
| 29 completer.complete("value"); |
| 30 completer.future.asStream().listen( |
| 31 (data) { |
| 32 Expect.isFalse(gotValue); |
| 33 gotValue = true; |
| 34 Expect.equals("value", data); |
| 35 }, |
| 36 onDone: () { |
| 37 Expect.isTrue(gotValue); |
| 38 port.close(); |
| 39 }); |
| 40 } |
| 41 |
| 42 testFutureAsStreamCompleteImmediate() { |
| 43 bool gotValue = false; |
| 44 var port = new ReceivePort(); |
| 45 new Future.immediate("value").asStream().listen( |
| 46 (data) { |
| 47 Expect.isFalse(gotValue); |
| 48 gotValue = true; |
| 49 Expect.equals("value", data); |
| 50 }, |
| 51 onDone: () { |
| 52 Expect.isTrue(gotValue); |
| 53 port.close(); |
| 54 }); |
| 55 } |
| 56 |
| 57 testFutureAsStreamCompleteErrorAfter() { |
| 58 var completer = new Completer(); |
| 59 bool gotError = false; |
| 60 var port = new ReceivePort(); |
| 61 completer.future.asStream().listen( |
| 62 (data) { |
| 63 Expect.fail("Unexpected data"); |
| 64 }, |
| 65 onError: (error) { |
| 66 Expect.isFalse(gotError); |
| 67 gotError = true; |
| 68 Expect.equals("error", error.error); |
| 69 }, |
| 70 onDone: () { |
| 71 Expect.isTrue(gotError); |
| 72 port.close(); |
| 73 }); |
| 74 completer.completeError("error"); |
| 75 } |
| 76 |
| 77 testFutureAsStreamWrapper() { |
| 78 var completer = new Completer(); |
| 79 bool gotValue = false; |
| 80 var port = new ReceivePort(); |
| 81 completer.complete("value"); |
| 82 completer.future |
| 83 .catchError((_) { throw "not possible"; }) // Returns a future wrapper. |
| 84 .asStream().listen( |
| 85 (data) { |
| 86 Expect.isFalse(gotValue); |
| 87 gotValue = true; |
| 88 Expect.equals("value", data); |
| 89 }, |
| 90 onDone: () { |
| 91 Expect.isTrue(gotValue); |
| 92 port.close(); |
| 93 }); |
| 94 } |
| 95 |
| 96 testFutureWhenCompleteValue() { |
| 97 var port = new ReceivePort(); |
| 98 int counter = 2; |
| 99 countDown() { |
| 100 if (--counter == 0) port.close(); |
| 101 } |
| 102 var completer = new Completer(); |
| 103 Future future = completer.future; |
| 104 Future later = future.whenComplete(countDown); |
| 105 later.then((v) { |
| 106 Expect.equals(42, v); |
| 107 countDown(); |
| 108 }); |
| 109 completer.complete(42); |
| 110 } |
| 111 |
| 112 testFutureWhenCompleteError() { |
| 113 var port = new ReceivePort(); |
| 114 int counter = 2; |
| 115 countDown() { |
| 116 if (--counter == 0) port.close(); |
| 117 } |
| 118 var completer = new Completer(); |
| 119 Future future = completer.future; |
| 120 Future later = future.whenComplete(countDown); |
| 121 later.catchError((AsyncError e) { |
| 122 Expect.equals("error", e.error); |
| 123 countDown(); |
| 124 }); |
| 125 completer.completeError("error"); |
| 126 } |
| 127 |
| 128 testFutureWhenCompleteValueNewError() { |
| 129 var port = new ReceivePort(); |
| 130 int counter = 2; |
| 131 countDown() { |
| 132 if (--counter == 0) port.close(); |
| 133 } |
| 134 var completer = new Completer(); |
| 135 Future future = completer.future; |
| 136 Future later = future.whenComplete(() { |
| 137 countDown(); |
| 138 throw "new error"; |
| 139 }); |
| 140 later.catchError((AsyncError e) { |
| 141 Expect.equals("new error", e.error); |
| 142 countDown(); |
| 143 }); |
| 144 completer.complete(42); |
| 145 } |
| 146 |
| 147 testFutureWhenCompleteErrorNewError() { |
| 148 var port = new ReceivePort(); |
| 149 int counter = 2; |
| 150 countDown() { |
| 151 if (--counter == 0) port.close(); |
| 152 } |
| 153 var completer = new Completer(); |
| 154 Future future = completer.future; |
| 155 Future later = future.whenComplete(() { |
| 156 countDown(); |
| 157 throw "new error"; |
| 158 }); |
| 159 later.catchError((AsyncError e) { |
| 160 Expect.equals("new error", e.error); |
| 161 countDown(); |
| 162 }); |
| 163 completer.completeError("error"); |
| 164 } |
| 165 |
| 166 testFutureWhenCompletePreValue() { |
| 167 var port = new ReceivePort(); |
| 168 int counter = 2; |
| 169 countDown() { |
| 170 if (--counter == 0) port.close(); |
| 171 } |
| 172 var completer = new Completer(); |
| 173 Future future = completer.future; |
| 174 completer.complete(42); |
| 175 new Timer(0, () { |
| 176 Future later = future.whenComplete(countDown); |
| 177 later.then((v) { |
| 178 Expect.equals(42, v); |
| 179 countDown(); |
| 180 }); |
| 181 }); |
| 182 } |
| 183 |
| 184 main() { |
| 185 testFutureAsStreamCompleteAfter(); |
| 186 testFutureAsStreamCompleteBefore(); |
| 187 testFutureAsStreamCompleteImmediate(); |
| 188 testFutureAsStreamCompleteErrorAfter(); |
| 189 testFutureAsStreamWrapper(); |
| 190 |
| 191 testFutureWhenCompleteValue(); |
| 192 testFutureWhenCompleteError(); |
| 193 testFutureWhenCompleteValueNewError(); |
| 194 testFutureWhenCompleteErrorNewError(); |
| 195 } |
| 196 |
OLD | NEW |