| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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:scheduled_test/scheduled_test.dart'; | 8 import 'package:scheduled_test/scheduled_test.dart'; |
| 9 import 'package:watcher/watcher.dart'; | 9 import 'package:watcher/watcher.dart'; |
| 10 | 10 |
| 11 import 'utils.dart'; | 11 import 'utils.dart'; |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 initConfig(); | 14 initConfig(); |
| 15 | 15 |
| 16 setUp(createSandbox); | 16 setUp(createSandbox); |
| 17 | 17 |
| 18 test('does not notify for changes when there were no subscribers', () { | 18 test('does not notify for changes when there were no subscribers', () { |
| 19 // Note that this test doesn't rely as heavily on the test functions in | 19 // Note that this test doesn't rely as heavily on the test functions in |
| 20 // utils.dart because it needs to be very explicit about when the event | 20 // utils.dart because it needs to be very explicit about when the event |
| 21 // stream is and is not subscribed. | 21 // stream is and is not subscribed. |
| 22 var watcher = createWatcher(); | 22 var watcher = createWatcher(); |
| 23 | 23 |
| 24 // Subscribe to the events. | 24 // Subscribe to the events. |
| 25 var completer = new Completer(); | 25 var completer = new Completer(); |
| 26 var subscription = watcher.events.listen((event) { | 26 var subscription = watcher.events.listen(wrapAsync((event) { |
| 27 expect(event.type, equals(ChangeType.ADD)); | 27 expect(event.type, equals(ChangeType.ADD)); |
| 28 expect(event.path, endsWith("file.txt")); | 28 expect(event.path, endsWith("file.txt")); |
| 29 completer.complete(); | 29 completer.complete(); |
| 30 }); | 30 })); |
| 31 | 31 |
| 32 writeFile("file.txt"); | 32 writeFile("file.txt"); |
| 33 | 33 |
| 34 // Then wait until we get an event for it. | 34 // Then wait until we get an event for it. |
| 35 schedule(() => completer.future); | 35 schedule(() => completer.future); |
| 36 | 36 |
| 37 // Unsubscribe. | 37 // Unsubscribe. |
| 38 schedule(() { | 38 schedule(() { |
| 39 subscription.cancel(); | 39 subscription.cancel(); |
| 40 }); | 40 }); |
| 41 | 41 |
| 42 // Now write a file while we aren't listening. | 42 // Now write a file while we aren't listening. |
| 43 writeFile("unwatched.txt"); | 43 writeFile("unwatched.txt"); |
| 44 | 44 |
| 45 // Then start listening again. | 45 // Then start listening again. |
| 46 schedule(() { | 46 schedule(() { |
| 47 completer = new Completer(); | 47 completer = new Completer(); |
| 48 subscription = watcher.events.listen((event) { | 48 subscription = watcher.events.listen(wrapAsync((event) { |
| 49 // We should get an event for the third file, not the one added while | 49 // We should get an event for the third file, not the one added while |
| 50 // we weren't subscribed. | 50 // we weren't subscribed. |
| 51 expect(event.type, equals(ChangeType.ADD)); | 51 expect(event.type, equals(ChangeType.ADD)); |
| 52 expect(event.path, endsWith("added.txt")); | 52 expect(event.path, endsWith("added.txt")); |
| 53 completer.complete(); | 53 completer.complete(); |
| 54 }); | 54 })); |
| 55 }); | 55 }); |
| 56 | 56 |
| 57 // The watcher will have been cancelled and then resumed in the middle of | 57 // The watcher will have been cancelled and then resumed in the middle of |
| 58 // its pause between polling loops. That means the second scan to skip | 58 // its pause between polling loops. That means the second scan to skip |
| 59 // what changed while we were unsubscribed won't happen until after that | 59 // what changed while we were unsubscribed won't happen until after that |
| 60 // delay is done. Wait long enough for that to happen. | 60 // delay is done. Wait long enough for that to happen. |
| 61 schedule(() => new Future.delayed(new Duration(seconds: 1))); | 61 schedule(() => new Future.delayed(watcher.pollingDelay * 2)); |
| 62 | 62 |
| 63 // And add a third file. | 63 // And add a third file. |
| 64 writeFile("added.txt"); | 64 writeFile("added.txt"); |
| 65 | 65 |
| 66 // Wait until we get an event for the third file. | 66 // Wait until we get an event for the third file. |
| 67 schedule(() => completer.future); | 67 schedule(() => completer.future); |
| 68 | 68 |
| 69 schedule(() { | 69 schedule(() { |
| 70 subscription.cancel(); | 70 subscription.cancel(); |
| 71 }); | 71 }); |
| 72 }); | 72 }); |
| 73 } | 73 } |
| OLD | NEW |