コード例 #1
0
    def test_multiple_watchers_same_file(self):
        """Test that we can have multiple watchers of the same file."""
        filename = "/this/is/my/file.py"

        mod_count = [0]

        def modify_mock_file():
            self.os.stat = lambda x: FakeStat(mod_count[0])
            self.mock_util.calc_md5_with_blocking_retries = (
                lambda x: "%d" % mod_count[0]
            )

            mod_count[0] += 1

        def sleep():
            try:
                # TODO: Remove depedency on time.sleep!
                time.sleep(5 * PollingFileWatcher._POLLING_PERIOD_SECS)
            except AssertionError:
                pass

        modify_mock_file()

        cb1 = mock.Mock()
        cb2 = mock.Mock()

        watcher1 = PollingFileWatcher.PollingFileWatcher(filename, cb1)
        watcher2 = PollingFileWatcher.PollingFileWatcher(filename, cb2)

        sleep()

        cb1.assert_not_called()
        cb2.assert_not_called()

        # "Modify" our file
        modify_mock_file()
        sleep()

        self.assertEqual(cb1.call_count, 1)
        self.assertEqual(cb2.call_count, 1)

        # Close watcher1. Only watcher2's callback should be called after this.
        watcher1.close()

        # Modify our file again
        modify_mock_file()
        sleep()

        self.assertEqual(cb1.call_count, 1)
        self.assertEqual(cb2.call_count, 2)

        watcher2.close()

        # Modify our file a final time
        modify_mock_file()

        # Both watchers are now closed, so their callback counts
        # should not have increased.
        self.assertEqual(cb1.call_count, 1)
        self.assertEqual(cb2.call_count, 2)
コード例 #2
0
    def test_callback_not_called_if_same_md5(self):
        """Test that we ignore files with same md5."""
        cb_marker = mock.Mock()

        def cb(x):
            cb_marker()

        self.os.stat = lambda x: FakeStat(101)
        self.mock_util.calc_md5_with_blocking_retries = lambda x: "1"

        ro = PollingFileWatcher.PollingFileWatcher("/this/is/my/file.py", cb)

        try:
            time.sleep(2 * PollingFileWatcher._POLLING_PERIOD_SECS)
        except AssertionError:
            pass
        cb_marker.assert_not_called()

        self.os.stat = lambda x: FakeStat(102)
        # Same MD5:
        # self.mock_util.calc_md5_with_blocking_retries = lambda x: '2'

        # This is the test:
        try:
            time.sleep(2 * PollingFileWatcher._POLLING_PERIOD_SECS)
        except AssertionError:
            pass
        cb_marker.assert_not_called()

        ro.close()
コード例 #3
0
    def test_file_watch_and_callback(self):
        """Test that when a file is modified, the callback is called."""
        cb_marker = mock.Mock()

        def cb(x):
            cb_marker()

        self.os.stat = lambda x: FakeStat(101)
        self.mock_util.calc_md5_with_blocking_retries = lambda x: "1"

        ro = PollingFileWatcher.PollingFileWatcher("/this/is/my/file.py", cb)

        try:
            time.sleep(2 * PollingFileWatcher._POLLING_PERIOD_SECS)
        except AssertionError:
            pass
        cb_marker.assert_not_called()

        self.os.stat = lambda x: FakeStat(102)
        self.mock_util.calc_md5_with_blocking_retries = lambda x: "2"

        time.sleep(4 * PollingFileWatcher._POLLING_PERIOD_SECS)
        cb_marker.assert_called_once()

        ro.close()