示例#1
0
    def test_key_exists(self):
        with TempDir() as cache_dir, TempDir() as d:
            cache = Directory(path=cache_dir)

            foo_file = os.path.join(d, 'foo_file')
            with open(foo_file, 'w') as f:
                f.write('foo')
            key = 'foo_key'

            self.assertFalse(cache.exists(key))
            cache.store_file(foo_file, key)
            self.assertTrue(cache.exists(key))
示例#2
0
    def test_key_lock_creates(self):
        with TempDir() as cache_dir, TempDir() as d:
            cache = Directory(path=cache_dir)

            key = 'foo_key'
            self.assertFalse(cache.exists(key))

            with cache.lease(key, create=True):
                self.assertTrue(cache.exists(key))

            self.assertTrue(cache.exists(key))
            self.assertTrue(os.listdir(cache_dir))
示例#3
0
    def test_lease_file(self):
        with TempDir() as cache_dir:
            cache = Directory(path=cache_dir)

            key = 'foo'

            self.assertFalse(cache.exists(key))

            with cache.lease_file(key, create=True) as path:
                self.assertTrue(cache.exists(key))
                self.assertTrue(os.path.exists(path))
                with open(path, 'r') as f:
                    self.assertNotEqual(f.read(), key)
                with open(path, 'w') as f:
                    f.write(key)

            self.assertTrue(cache.exists(key))

            with cache.lease_file(key) as path:
                with open(path, 'r') as f:
                    self.assertEqual(f.read(), key)