def testListKeys(self):
        """Verifies that ListKeys() returns any items present in the cache."""
        osutils.Touch(os.path.join(self.tempdir, 'file1'))
        cache.CacheReference(self.cache, ('key1', )).Assign(
            os.path.join(self.tempdir, 'file1'))
        osutils.Touch(os.path.join(self.tempdir, 'file2'))
        cache.CacheReference(self.cache, ('key2', )).Assign(
            os.path.join(self.tempdir, 'file2'))

        keys = self.cache.ListKeys()
        self.assertEqual(len(keys), 2)
        self.assertIn(('key1', ), keys)
        self.assertIn(('key2', ), keys)
    def testDeleteStale(self):
        """Ensures that DeleteStale removes a sufficiently old item in the cache."""
        osutils.Touch(os.path.join(self.tempdir, 'file1'))
        cache_ref = cache.CacheReference(self.cache, ('key1', ))
        cache_ref.Assign(os.path.join(self.tempdir, 'file1'))
        now = datetime.datetime.now()

        # 'Now' will be 10 days in the future, but max_age is 20 days. So no items
        # should be deleted.
        ten_days_ahead = now + datetime.timedelta(days=10)
        with mock.patch('chromite.lib.cache.datetime') as mock_datetime:
            mock_datetime.datetime.now.return_value = ten_days_ahead
            mock_datetime.datetime.fromtimestamp.side_effect = (
                datetime.datetime.fromtimestamp)
            mock_datetime.timedelta = datetime.timedelta
            self.cache.DeleteStale(datetime.timedelta(days=20))
        self.assertTrue(cache_ref.Exists())

        # Running it again 30 days in the future should delete everything.
        thirty_days_ahead = now + datetime.timedelta(days=30)
        with mock.patch('chromite.lib.cache.datetime') as mock_datetime:
            mock_datetime.datetime.now.return_value = thirty_days_ahead
            mock_datetime.datetime.fromtimestamp.side_effect = (
                datetime.datetime.fromtimestamp)
            mock_datetime.timedelta = datetime.timedelta
            self.cache.DeleteStale(datetime.timedelta(days=20))
        self.assertFalse(cache_ref.Exists())
Example #3
0
    def testPath(self):
        """Verify we get a file path for the ref."""
        self.cache._GetKeyPath.return_value = '/foo/bar'

        ref = cache.CacheReference(self.cache, 'key')
        self.assertEqual(ref.path, '/foo/bar')

        self.cache._GetKeyPath.assert_called_once_with('key')
Example #4
0
 def testContext(self):
     """Verify we can use it as a context manager."""
     # We should set the acquire member and grab/release the lock.
     ref = cache.CacheReference(self.cache, 'key')
     self.assertFalse(ref.acquired)
     self.assertFalse(self.lock.__enter__.called)
     with ref as newref:
         self.assertEqual(ref, newref)
         self.assertTrue(ref.acquired)
         self.assertTrue(self.lock.__enter__.called)
         self.assertFalse(self.lock.__exit__.called)
     self.assertFalse(ref.acquired)
     self.assertTrue(self.lock.__exit__.called)
Example #5
0
    def testLocking(self):
        """Verify Acquire & Release work as expected."""
        ref = cache.CacheReference(self.cache, 'key')

        # Check behavior when the lock is free.
        self.assertRaises(AssertionError, ref.Release)
        self.assertFalse(ref.acquired)

        # Check behavior when the lock is held.
        self.assertEqual(ref.Acquire(), None)
        self.assertRaises(AssertionError, ref.Acquire)
        self.assertTrue(ref.acquired)

        # Check behavior after the lock is freed.
        self.assertEqual(ref.Release(), None)
        self.assertFalse(ref.acquired)
Example #6
0
 def testAssignText(self):
     """Verify AssignText works as expected."""
     ref = cache.CacheReference(self.cache, 'key')
     ref.AssignText('text!')
     self.cache._InsertText.assert_called_once_with('key', 'text!')
Example #7
0
 def testAssign(self):
     """Verify Assign works as expected."""
     ref = cache.CacheReference(self.cache, 'key')
     ref.Assign('/foo')
     self.cache._Insert.assert_called_once_with('key', '/foo')
Example #8
0
 def testExistsMissing(self):
     """Verify Exists works when the entry is in the cache."""
     ref = cache.CacheReference(self.cache, 'key')
     self.cache._KeyExists.return_value = True
     self.assertTrue(ref.Exists())
Example #9
0
 def testExists(self):
     """Verify Exists works when the entry is not in the cache."""
     ref = cache.CacheReference(self.cache, 'key')
     self.cache._KeyExists.return_value = False
     self.assertFalse(ref.Exists())
Example #10
0
 def testSetDefaultExists(self):
     """Verify SetDefault works when the entry is in the cache."""
     ref = cache.CacheReference(self.cache, 'key')
     self.cache._KeyExists.return_value = True
     ref.SetDefault('/foo')
     self.assertFalse(self.cache._Insert.called)
Example #11
0
 def testSetDefault(self):
     """Verify SetDefault works when the entry is not in the cache."""
     ref = cache.CacheReference(self.cache, 'key')
     self.cache._KeyExists.return_value = False
     ref.SetDefault('/foo')
     self.cache._Insert.assert_called_once_with('key', '/foo')
Example #12
0
 def testRemove(self):
     """Verify Remove works as expected."""
     ref = cache.CacheReference(self.cache, 'key')
     ref.Remove()
     self.cache._Remove.assert_called_once_with('key')