예제 #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_mock.stat = lambda x: FakeStat(mod_count[0])
            self.util_mock.calc_md5_with_blocking_retries = (
                lambda x: "%d" % mod_count[0]
            )

            mod_count[0] += 1

        modify_mock_file()

        callback1 = mock.Mock()
        callback2 = mock.Mock()

        watcher1 = polling_file_watcher.PollingFileWatcher(filename, callback1)
        watcher2 = polling_file_watcher.PollingFileWatcher(filename, callback2)

        self._run_executor_tasks()

        callback1.assert_not_called()
        callback2.assert_not_called()

        # "Modify" our file
        modify_mock_file()
        self._run_executor_tasks()

        self.assertEqual(callback1.call_count, 1)
        self.assertEqual(callback2.call_count, 1)

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

        # Modify our file again
        modify_mock_file()
        self._run_executor_tasks()

        self.assertEqual(callback1.call_count, 1)
        self.assertEqual(callback2.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(callback1.call_count, 1)
        self.assertEqual(callback2.call_count, 2)
예제 #2
0
    def test_file_watch_and_callback(self):
        """Test that when a file is modified, the callback is called."""
        callback = mock.Mock()

        self.os_mock.stat = lambda x: FakeStat(101)
        self.util_mock.calc_md5_with_blocking_retries = lambda x: "1"

        watcher = polling_file_watcher.PollingFileWatcher(
            "/this/is/my/file.py", callback)

        self._run_executor_tasks()
        callback.assert_not_called()

        self.os_mock.stat = lambda x: FakeStat(102)
        self.util_mock.calc_md5_with_blocking_retries = lambda x: "2"

        self._run_executor_tasks()
        callback.assert_called_once()

        watcher.close()
예제 #3
0
    def test_callback_not_called_if_same_mtime(self):
        """Test that we ignore files with same mtime."""
        callback = mock.Mock()

        self.os_mock.stat = lambda x: FakeStat(101)
        self.util_mock.calc_md5_with_blocking_retries = lambda x: "1"

        watcher = polling_file_watcher.PollingFileWatcher(
            "/this/is/my/file.py", callback)

        self._run_executor_tasks()
        callback.assert_not_called()

        # self.os.stat = lambda x: FakeStat(102)  # Same mtime!
        self.util_mock.calc_md5_with_blocking_retries = lambda x: "2"

        # This is the test:
        self._run_executor_tasks()
        callback.assert_not_called()

        watcher.close()