Example #1
0
    def test_independent_item_expirations(self, tmpdir, monkeypatch):
        cache = suds.cache.FileCache(tmpdir.strpath, days=1)
        cache.put("unga1", value_p1)
        cache.put("unga2", value_p2)
        cache.put("unga3", value_f2)
        filepath1 = cache._FileCache__filename("unga1")
        filepath2 = cache._FileCache__filename("unga2")
        filepath3 = cache._FileCache__filename("unga3")
        file_timestamp1 = os.path.getctime(filepath1)
        file_timestamp2 = file_timestamp1 + 10 * 60  # in seconds
        file_timestamp3 = file_timestamp1 + 20 * 60  # in seconds
        file_time1 = datetime.datetime.fromtimestamp(file_timestamp1)
        file_time1_expiration = file_time1 + cache.duration

        original_getctime = os.path.getctime

        def mock_getctime(path):
            if path == filepath2:
                return file_timestamp2
            if path == filepath3:
                return file_timestamp3
            return original_getctime(path)

        timedelta = datetime.timedelta

        monkeypatch.setattr(os.path, "getctime", mock_getctime)
        monkeypatch.setattr(datetime, "datetime", MockDateTime)

        MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=15)
        assert cache._getf("unga2") is None
        assert os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        cache._getf("unga3").close()
        assert os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=25)
        assert cache._getf("unga1") is None
        assert not os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        assert cache._getf("unga3") is None
        assert not os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert not os.path.isfile(filepath3)
Example #2
0
    def test_independent_item_expirations(self, tmpdir, monkeypatch):
        cache = suds.cache.FileCache(tmpdir.strpath, days=1)
        cache.put("unga1", value_p1)
        cache.put("unga2", value_p2)
        cache.put("unga3", value_f2)
        filepath1 = cache._FileCache__filename("unga1")
        filepath2 = cache._FileCache__filename("unga2")
        filepath3 = cache._FileCache__filename("unga3")
        file_timestamp1 = os.path.getctime(filepath1)
        file_timestamp2 = file_timestamp1 + 10 * 60  # in seconds
        file_timestamp3 = file_timestamp1 + 20 * 60  # in seconds
        file_time1 = datetime.datetime.fromtimestamp(file_timestamp1)
        file_time1_expiration = file_time1 + cache.duration

        original_getctime = os.path.getctime
        def mock_getctime(path):
            if path == filepath2:
                return file_timestamp2
            if path == filepath3:
                return file_timestamp3
            return original_getctime(path)

        timedelta = datetime.timedelta

        monkeypatch.setattr(os.path, "getctime", mock_getctime)
        monkeypatch.setattr(datetime, "datetime", MockDateTime)

        MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=15)
        assert cache._getf("unga2") is None
        assert os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        cache._getf("unga3").close()
        assert os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=25)
        assert cache._getf("unga1") is None
        assert not os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        assert cache._getf("unga3") is None
        assert not os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert not os.path.isfile(filepath3)
Example #3
0
    def item_expiration_test_worker(cache, id, monkeypatch, current_time,
            expect_remove):
        """
        Test how a FileCache & its derived classes expire their item entries.

        Facts tested:
        * 0 duration should cause cache items never to expire.
        * Expired item files should be automatically removed from the cache
          folder.
        * Negative durations should be treated the same as positive ones.

        Requirements on the passed cache object:
        * Configures with the correct duration for this test.
        * Contains a valid cached item with the given id and its ctime
          timestamp + cache.duration must fall into the valid datetime.datetime
          value range.
        * Must use only public & protected FileCache interfaces to access its
          cache item data files.

        'current_time' values are expected to be either datetime.datetime or
        datetime.timedelta instances with the latter interpreted relative to
        the test file's expected expiration time.

        """
        assert isinstance(cache, suds.cache.FileCache)
        filepath = cache._FileCache__filename(id)
        assert os.path.isfile(filepath)
        file_timestamp = os.path.getctime(filepath)
        file_time = datetime.datetime.fromtimestamp(file_timestamp)

        MockDateTime.mock_counter = 0
        if isinstance(current_time, datetime.timedelta):
            expire_time = file_time + cache.duration
            MockDateTime.mock_value = expire_time + current_time
        else:
            MockDateTime.mock_value = current_time
        monkeypatch.setattr(datetime, "datetime", MockDateTime)
        assert (cache._getf(id) is None) == expect_remove
        monkeypatch.undo()
        if cache.duration:
            assert MockDateTime.mock_counter == 1
        else:
            assert MockDateTime.mock_counter == 0
        assert os.path.isfile(filepath) == (not expect_remove)
Example #4
0
    def item_expiration_test_worker(cache, id, monkeypatch, current_time,
            expect_remove):
        """
        Test how a FileCache & its derived classes expire their item entries.

        Facts tested:
        * 0 duration should cause cache items never to expire.
        * Expired item files should be automatically removed from the cache
          folder.
        * Negative durations should be treated the same as positive ones.

        Requirements on the passed cache object:
        * Configures with the correct duration for this test.
        * Contains a valid cached item with the given id and its ctime
          timestamp + cache.duration must fall into the valid datetime.datetime
          value range.
        * Must use only public & protected FileCache interfaces to access its
          cache item data files.

        'current_time' values are expected to be either datetime.datetime or
        datetime.timedelta instances with the latter interpreted relative to
        the test file's expected expiration time.

        """
        assert isinstance(cache, suds.cache.FileCache)
        filepath = cache._FileCache__filename(id)
        assert os.path.isfile(filepath)
        file_timestamp = os.path.getctime(filepath)
        file_time = datetime.datetime.fromtimestamp(file_timestamp)

        MockDateTime.mock_counter = 0
        if isinstance(current_time, datetime.timedelta):
            expire_time = file_time + cache.duration
            MockDateTime.mock_value = expire_time + current_time
        else:
            MockDateTime.mock_value = current_time
        monkeypatch.setattr(datetime, "datetime", MockDateTime)
        assert (cache._getf(id) is None) == expect_remove
        monkeypatch.undo()
        if cache.duration:
            assert MockDateTime.mock_counter == 1
        else:
            assert MockDateTime.mock_counter == 0
        assert os.path.isfile(filepath) == (not expect_remove)