~ubuntuone-control-tower/ubuntuone-client/trunk

« back to all changes in this revision

Viewing changes to tests/platform/filesystem_notifications/test_darwin.py

  • Committer: Tarmac
  • Author(s): mike.mccracken at canonical
  • Date: 2013-01-18 16:26:47 UTC
  • mfrom: (1363.6.15 neuter-watchmanager)
  • Revision ID: tarmac-20130118162647-saon8t6q47kdmunx
- Clean up test suite for filesystem_notifications on darwin and windows.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import thread
36
36
 
37
37
from twisted.internet import defer
 
38
 
 
39
import fsevents
 
40
 
 
41
from contrib.testing.testcase import BaseTwistedTestCase
 
42
 
38
43
from ubuntuone.devtools.handlers import MementoHandler
39
44
from ubuntuone.platform.filesystem_notifications.monitor import (
40
45
    common,
87
92
        self.name = name
88
93
 
89
94
 
 
95
class FakeObserver(object):
 
96
    """Fake fsevents.py Observer for tests that don't need real events."""
 
97
 
 
98
    def __init__(self, latency=0, process_asap=True):
 
99
        """Do nothing."""
 
100
 
 
101
    def start(self):
 
102
        """Do nothing."""
 
103
 
 
104
    def stop(self):
 
105
        """Do nothing."""
 
106
 
 
107
    def join(self):
 
108
        """Do nothing."""
 
109
 
 
110
    def schedule(self, stream):
 
111
        """Ignore"""
 
112
 
 
113
    def unschedule(self, stream):
 
114
        """Ignore"""
 
115
 
 
116
 
90
117
class TestCaseHandler(ProcessEvent):
91
118
    """ProcessEvent used for test cases."""
92
119
 
490
517
        """Test that the exclude filter works as expectd."""
491
518
        handler = TestCaseHandler(number_events=0)
492
519
        manager = WatchManager(handler)
 
520
        self.addCleanup(manager.stop)
493
521
        # add a watch that will always exclude all actions
494
522
        manager.add_watch(self.basedir, self.mask,
495
523
            exclude_filter=lambda x: True)
596
624
        self.assertTrue(path not in watch._subdirs)
597
625
 
598
626
 
599
 
class TestWatchManager(common_tests.TestWatchManager):
600
 
    """Test the watch manager."""
 
627
class TestWatchManagerStopping(common_tests.TestWatchManagerStopping):
 
628
    """Test stopping the watch manager."""
601
629
 
602
630
    @defer.inlineCallbacks
603
631
    def setUp(self):
604
632
        """Set each of the tests."""
605
 
        yield super(TestWatchManager, self).setUp()
 
633
        yield super(TestWatchManagerStopping, self).setUp()
606
634
        self.parent_path = '/Users/username/'
607
635
        self.path = self.parent_path + 'path'
608
636
        self.watch = Watch(1, self.path, None)
609
 
        self.manager = WatchManager(None)
610
637
        self.manager._wdm = {1: self.watch}
611
638
        self.stream = None
612
639
        self.fake_events_processor = FakeEventsProcessor()
 
640
        self.patch(self.manager.platform_manager.observer, "unschedule",
 
641
                   lambda x: None)
613
642
 
614
643
    @defer.inlineCallbacks
615
644
    def test_stop(self):
616
645
        """Test that the different watches are stopped."""
617
 
        self.patch(self.manager.manager.observer, "unschedule",
618
 
            lambda x: None)
619
 
        self.patch(self.manager.manager.observer, "unschedule", lambda x: None)
620
 
        yield super(TestWatchManager, self).test_stop()
 
646
        yield super(TestWatchManagerStopping, self).test_stop()
621
647
 
622
648
    def test_stop_multiple(self):
623
649
        """Watches should became watching=False and the observer stopped."""
624
 
        self.patch(self.manager.manager.observer, "unschedule", lambda x: None)
625
650
        second_path = self.parent_path + "second_path"
626
651
        second_watch = Watch(2, second_path, None)
627
652
        second_watch._watching = True
630
655
        self.assertFalse(second_watch.platform_watch.watching)
631
656
        self.assertEqual(second_watch._subdirs, set())
632
657
        # Give time to the thread to be finished.
633
 
        self.manager.manager.observer.join()
634
 
        self.assertFalse(self.manager.manager.observer.is_alive())
 
658
        self.manager.platform_manager.observer.join()
 
659
        self.assertFalse(self.manager.platform_manager.observer.is_alive())
 
660
 
 
661
 
 
662
class TestWatchManager(common_tests.TestWatchManager):
 
663
    """Test the watch manager."""
 
664
 
 
665
    @defer.inlineCallbacks
 
666
    def setUp(self):
 
667
        """Setup."""
 
668
 
 
669
        # Note: patching Observer before super.setUp because it is
 
670
        # used by the WatchManager we create there.
 
671
        self.patch(fsevents, "Observer", FakeObserver)
 
672
 
 
673
        yield super(TestWatchManager, self).setUp()
 
674
 
 
675
        self.parent_path = '/Users/username/'
 
676
        self.path = self.parent_path + 'path'
 
677
        self.watch = Watch(1, self.path, None)
 
678
        self.manager._wdm = {1: self.watch}
 
679
        self.stream = None
 
680
        self.fake_events_processor = FakeEventsProcessor()
635
681
 
636
682
    def test_get_present_watch(self):
637
683
        """Test that we can get a Watch using is wd."""
678
724
 
679
725
    def test_rm_present_wd(self):
680
726
        """Test the removal of a present watch."""
681
 
        self.patch(self.manager.manager.observer, "unschedule", lambda x: None)
 
727
        self.patch(self.manager.platform_manager.observer,
 
728
                   "unschedule", lambda x: None)
682
729
        super(TestWatchManager, self).test_rm_present_wd()
683
730
 
684
731
    def test_rm_child_path(self):
688
735
        super(TestWatchManager, self).test_rm_child_path()
689
736
 
690
737
 
691
 
class TestWatchManagerAddWatches(common_tests.TestWatchManagerAddWatches):
 
738
class TestWatchManagerAddWatches(BaseTwistedTestCase):
692
739
    """Test the watch manager."""
693
740
    timeout = 5
694
741
 
697
744
        self.patch(Watch, "start_watching", lambda self: None)
698
745
        self.patch(Watch, "started", lambda self: True)
699
746
        manager = WatchManager(None)
700
 
        # no need to stop watching because start_watching is fake
 
747
        self.addCleanup(manager.stop)
701
748
 
702
749
        path = '/Users/username/path'
703
750
        mask = 'fake bit mask'
1074
1121
        self.patch(Watch, "start_watching", lambda self: None)
1075
1122
        self.patch(Watch, "started", lambda self: True)
1076
1123
        manager = WatchManager(None)
1077
 
        # no need to stop watching because start_watching is fake
 
1124
        self.addCleanup(manager.stop)
1078
1125
 
1079
1126
        path = '/Users/username/path'
1080
1127
        mask = 'fake bit mask'