| 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:io'; |
| 7 |
| 8 import 'package:scheduled_test/scheduled_test.dart'; |
| 9 import 'package:watcher/watcher.dart'; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 main() { |
| 14 initConfig(); |
| 15 |
| 16 setUp(createSandbox); |
| 17 |
| 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 |
| 20 // utils.dart because it needs to be very explicit about when the event |
| 21 // stream is and is not subscribed. |
| 22 var watcher = createWatcher(); |
| 23 |
| 24 // Subscribe to the events. |
| 25 var completer = new Completer(); |
| 26 var subscription = watcher.events.listen((event) { |
| 27 expect(event.type, equals(ChangeType.ADD)); |
| 28 expect(event.path, endsWith("file.txt")); |
| 29 completer.complete(); |
| 30 }); |
| 31 |
| 32 writeFile("file.txt"); |
| 33 |
| 34 // Then wait until we get an event for it. |
| 35 schedule(() => completer.future); |
| 36 |
| 37 // Unsubscribe. |
| 38 schedule(() { |
| 39 subscription.cancel(); |
| 40 }); |
| 41 |
| 42 // Now write a file while we aren't listening. |
| 43 writeFile("unwatched.txt"); |
| 44 |
| 45 // Then start listening again. |
| 46 schedule(() { |
| 47 completer = new Completer(); |
| 48 subscription = watcher.events.listen((event) { |
| 49 // We should get an event for the third file, not the one added while |
| 50 // we weren't subscribed. |
| 51 expect(event.type, equals(ChangeType.ADD)); |
| 52 expect(event.path, endsWith("added.txt")); |
| 53 completer.complete(); |
| 54 }); |
| 55 }); |
| 56 |
| 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 |
| 59 // what changed while we were unsubscribed won't happen until after that |
| 60 // delay is done. Wait long enough for that to happen. |
| 61 schedule(() => new Future.delayed(new Duration(seconds: 1))); |
| 62 |
| 63 // And add a third file. |
| 64 writeFile("added.txt"); |
| 65 |
| 66 // Wait until we get an event for the third file. |
| 67 schedule(() => completer.future); |
| 68 |
| 69 schedule(() { |
| 70 subscription.cancel(); |
| 71 }); |
| 72 }); |
| 73 } |
| OLD | NEW |