| 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 "dart:io"; | 6 import "dart:io"; |
| 7 | 7 |
| 8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 | 10 |
| 11 void testPauseResumeCancelStream() { | 11 void testPauseResumeCancelStream() { |
| 12 asyncStart(); | 12 asyncStart(); |
| 13 Directory.systemTemp.createTemp('file_stream').then((d) { | 13 Directory.systemTemp.createTemp('dart_file_stream').then((d) { |
| 14 var file = new File("${d.path}/file"); | 14 var file = new File("${d.path}/file"); |
| 15 new File(Platform.executable).openRead().pipe(file.openWrite()) | 15 new File(Platform.executable).openRead().pipe(file.openWrite()) |
| 16 .then((_) { | 16 .then((_) { |
| 17 var subscription; | 17 var subscription; |
| 18 subscription = file.openRead().listen((data) { | 18 subscription = file.openRead().listen((data) { |
| 19 subscription.pause(); | 19 subscription.pause(); |
| 20 subscription.resume(); | 20 subscription.resume(); |
| 21 void close() { | 21 void close() { |
| 22 d.deleteSync(recursive: true); | 22 d.deleteSync(recursive: true); |
| 23 asyncEnd(); | 23 asyncEnd(); |
| 24 } | 24 } |
| 25 var future = subscription.cancel(); | 25 var future = subscription.cancel(); |
| 26 if (future != null) { | 26 if (future != null) { |
| 27 future.whenComplete(close); | 27 future.whenComplete(close); |
| 28 } else { | 28 } else { |
| 29 close(); | 29 close(); |
| 30 } | 30 } |
| 31 }, onDone: () { | 31 }, onDone: () { |
| 32 Expect.fail('the stream was canceled, onDone should not happend'); | 32 Expect.fail('the stream was canceled, onDone should not happend'); |
| 33 }); | 33 }); |
| 34 }); | 34 }); |
| 35 }); | 35 }); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void testStreamIsEmpty() { | 38 void testStreamIsEmpty() { |
| 39 asyncStart(); | 39 asyncStart(); |
| 40 Directory.systemTemp.createTemp('file_stream').then((d) { | 40 Directory.systemTemp.createTemp('dart_file_stream').then((d) { |
| 41 var file = new File("${d.path}/file"); | 41 var file = new File("${d.path}/file"); |
| 42 new File(Platform.executable).openRead().pipe(file.openWrite()) | 42 new File(Platform.executable).openRead().pipe(file.openWrite()) |
| 43 .then((_) { | 43 .then((_) { |
| 44 // isEmpty will cancel the stream after first data event. | 44 // isEmpty will cancel the stream after first data event. |
| 45 file.openRead().isEmpty.then((empty) { | 45 file.openRead().isEmpty.then((empty) { |
| 46 Expect.isFalse(empty); | 46 Expect.isFalse(empty); |
| 47 d.deleteSync(recursive: true); | 47 d.deleteSync(recursive: true); |
| 48 asyncEnd(); | 48 asyncEnd(); |
| 49 }); | 49 }); |
| 50 }); | 50 }); |
| 51 }); | 51 }); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void main() { | 54 void main() { |
| 55 testPauseResumeCancelStream(); | 55 testPauseResumeCancelStream(); |
| 56 testStreamIsEmpty(); | 56 testStreamIsEmpty(); |
| 57 } | 57 } |
| OLD | NEW |