def test_hierarchy_cache(self):
        """ Test group hierarchy caching """
        group = SSMParameterGroup()  # without max age
        group.parameters(self.HIERARCHY_PREPATH)
        self.assertFalse(
            group._should_refresh(),
            "Cache-less groups shouldn't ever refresh",
        )

        group = SSMParameterGroup(max_age=10)  # wit max age
        group.parameters(self.HIERARCHY_PREPATH)
        self.assertFalse(
            group._should_refresh(),
            "Cache-full groups shouldn't need refresh immediately after initialization",
        )

        group = SSMParameterGroup(max_age=10)  # wit max age
        group.parameters(self.HIERARCHY_PREPATH)
        # freeze_time will pretend 10 seconds have passed!
        with freeze_time(lambda: datetime.utcnow() + timedelta(seconds=10)):
            self.assertTrue(
                group._should_refresh(),
                "Cache-full groups should need refresh after time has passed",
            )

        group = SSMParameterGroup(max_age=10)  # wit max age
        group.parameters(self.HIERARCHY_PREPATH)
        self.assertFalse(group._should_refresh())
        # freeze_time will pretend 10 seconds have passed!
        with freeze_time(lambda: datetime.utcnow() + timedelta(seconds=10)):
            group.parameters(self.HIERARCHY_PREPATH_LIST)
            self.assertTrue(
                group._should_refresh(),
                "Cache-full groups should need refresh based on the oldest fetched params",
            )
示例#2
0
    def test_main_with_expiration_group(self):
        """ Test group case with expiration """
        group = SSMParameterGroup(max_age=300)
        param_1 = group.parameter("my_param_1")
        param_2 = group.parameter("my_param_2")
        param_3 = group.parameter("my_param_3")

        # individual params don't share max_age internally (for now)
        for param in (param_1, param_2, param_3):
            self.assertEqual(param._max_age, None)

        # force fetch
        group.refresh()

        # pretend time has passed (for the group)
        group._last_refresh_time = datetime.utcnow() - timedelta(seconds=301)
        self.assertTrue(group._should_refresh())
        self.assertTrue(param_1._should_refresh())
        self.assertTrue(param_2._should_refresh())
        self.assertTrue(param_3._should_refresh())