def test_miss_from_missing_file2(self): ''' As above, but flush the entry to disk first. ''' root = self.mkdtemp() c = Cache(root) input = prime_ast_hash(dummy_ast()) _, tmp = tempfile.mkstemp() with open(tmp, 'wt') as f: f.write('foo bar') c.save(input, ['arg1', 'arg2'], [tmp], 'hello world') # Ensure we can find what we just saved. output = c.load(input, ['arg1', 'arg2'], [tmp]) self.assertEqual(output, 'hello world') c.flush() os.remove(tmp) # Now ensure we get a miss after removing one of its inputs. output = c.load(input, ['arg1', 'arg2'], [tmp]) self.assertIsNone(output)
def test_timestamps(self): ''' Ensure that modifying a timestamp on one of the inputs has no effect. ''' root = self.mkdtemp() c = Cache(root) input = prime_ast_hash(dummy_ast()) tmp = self.mkstemp() with open(tmp, 'wt') as f: f.write('foo bar') c.save(input, ['arg1', 'arg2'], [tmp], 'hello world') c.flush() # Bump the timestamps on the input. st = os.stat(tmp) os.utime(tmp, (st[stat.ST_ATIME] + 3600, st[stat.ST_MTIME] + 3600)) # Ensure we can find what we just saved. output = c.load(input, ['arg1', 'arg2'], [tmp]) self.assertEqual(output, 'hello world') # And after a flush. c.flush() output = c.load(input, ['arg1', 'arg2'], [tmp]) self.assertEqual(output, 'hello world')
def test_directory_creation(self): ''' The cache should be capable of creating necessary subdirectories under its root. ''' root = os.path.join(self.mkdtemp(), 'non-existent') c = Cache(root) input = prime_ast_hash(dummy_ast()) c.save(input, ['arg1', 'arg2'], [], 'hello world') c.flush()
def test_no_args(self): root = self.mkdtemp() c = Cache(root) input = prime_ast_hash(dummy_ast()) c.save(input, [], [], 'hello world') # Ensure we can find what we just saved. output = c.load(input, [], []) self.assertEqual(output, 'hello world') # Ensure it is preserved after a flush. c.flush() output = c.load(input, [], []) self.assertEqual(output, 'hello world')
def test_basic_with_flush(self): ''' Same as the basic test, but we'll flush in-between to ensure we perform an on-disk lookup. ''' root = self.mkdtemp() c = Cache(root) input = prime_ast_hash(dummy_ast()) c.save(input, ['arg1', 'arg2'], [], 'hello world') c.flush() # Ensure we can find what we just saved. output = c.load(input, ['arg1', 'arg2'], []) self.assertEqual(output, 'hello world')
def test_no_inputs(self): ''' Ensure we can handle an entry with no inputs. ''' root = self.mkdtemp() c = Cache(root) input = prime_ast_hash(LiftedAST([])) c.save(input, ['arg1', 'arg2'], [], 'hello world') # Ensure we can find what we just saved. output = c.load(input, ['arg1', 'arg2'], []) self.assertEqual(output, 'hello world') # Ensure it is preserved after a flush. c.flush() output = c.load(input, ['arg1', 'arg2'], []) self.assertEqual(output, 'hello world')
def test_miss_on_disk1(self): ''' Same as the in-memory miss test except we flush the cache in-between. ''' root = self.mkdtemp() c = Cache(root) input = prime_ast_hash(dummy_ast()) c.save(input, ['arg1', 'arg2'], [], 'hello world') c.flush() # Ensure we can find what we just saved. output = c.load(input, ['arg1', 'arg2'], []) self.assertEqual(output, 'hello world') # Now ensure we get a miss with a differing AST. input = prime_ast_hash(LiftedAST([])) output = c.load(input, ['arg1', 'arg2'], []) self.assertIsNone(output)