def test_basic(self): ''' Test we can look up something we've just saved. Note that this test will not actually perform an on-disk lookup. ''' root = self.mkdtemp() c = Cache(root) input = self.mkstemp() with open(input, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input]) cwd = os.getcwd() c.save(['arg1', 'arg2'], cwd, 'hello world', inputs) # Ensure we can find what we just saved. output = c.load(['arg1', 'arg2'], cwd) self.assertEqual(output, 'hello world')
def test_no_args(self): root = self.mkdtemp() c = Cache(root) input = self.mkstemp() with open(input, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input]) cwd = os.getcwd() c.save([], cwd, 'hello world', inputs) # Ensure we can find what we just saved. output = c.load([], cwd) self.assertEqual(output, 'hello world') # Ensure it is preserved after a flush. c.flush() output = c.load([], cwd) self.assertEqual(output, 'hello world')
def test_cache_miss_cwd_valgrind(self): # As for the basic test case... root = self.mkdtemp() internal_root = os.path.join(root, version(), 'cachea') c = Cache(internal_root) input1 = self.mkstemp() with open(input1, 'wt') as f: f.write('hello world') input2 = self.mkstemp() with open(input2, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input1, input2]) cwd = self.mkdtemp() output = self.mkstemp() args = ['--cache-dir', root, '--outfile', output] c.save(args[:-2], cwd, 'moo cow', inputs) c.flush() del c # Use a different working directory this time around. cwd = self.mkdtemp() _, _, stderr = self.execute(VALGRIND + [self.debug_accelerator] + args, cwd=cwd) if valgrind_found_leak(stderr): self.fail('camkes-accelerator %s leaks memory:\n%s' % (' '.join(args), stderr)) _, _, stderr = self.execute(VALGRIND + [self.accelerator] + args, cwd=cwd) if valgrind_found_leak(stderr): self.fail('camkes-accelerator %s leaks memory (not reproducible ' 'in debug mode):\n%s' % (' '.join(args), stderr))
def test_cache_miss_inputs(self): ''' Test that we correctly miss when one of the inputs has changed. ''' # As for the basic test case... root = self.mkdtemp() internal_root = os.path.join(root, version(), 'cachea') c = Cache(internal_root) input1 = self.mkstemp() with open(input1, 'wt') as f: f.write('hello world') input2 = self.mkstemp() with open(input2, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input1, input2]) cwd = self.mkdtemp() output = self.mkstemp() args = ['--cache-dir', root, '--outfile', output] c.save(args[:-2], cwd, 'moo cow', inputs) c.flush() del c # Now let's modify one of the inputs. with open(input2, 'at') as f: f.write('foo bar') ret, stdout, stderr = self.execute([self.accelerator] + args, cwd=cwd) # It should have missed (== non-zero return value with no output). self.assertNotEqual(ret, 0) self.assertEqual(stdout, '') self.assertEqual(stderr, '')
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 = self.mkstemp() with open(input, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input]) cwd = os.getcwd() c.save(['arg1', 'arg2'], cwd, 'hello world', inputs) c.flush() # Ensure we can find what we just saved. output = c.load(['arg1', 'arg2'], cwd) self.assertEqual(output, 'hello world')
def test_miss_from_missing_file1(self): ''' Ensure cache misses from missing files function correctly. ''' root = self.mkdtemp() c = Cache(root) _, input = tempfile.mkstemp() with open(input, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input]) cwd = os.getcwd() c.save(['arg1', 'arg2'], cwd, 'hello world', inputs) # Now cause the entry to be invalid by deleting its input. os.remove(input) # Ensure we miss when now performing a lookup. output = c.load(['arg1', 'arg2'], cwd) self.assertIsNone(output)
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 = self.mkstemp() with open(input, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input]) cwd = os.getcwd() c.save(['arg1', 'arg2'], cwd, 'hello world', inputs) c.flush() # Now cause the entry to be invalid by modifying inputs. with open(input, 'wt') as f: f.write('bar foo') # Ensure we miss when now performing a lookup. output = c.load(['arg1', 'arg2'], cwd) self.assertIsNone(output)
def test_miss_in_memory(self): ''' Test that an induced cache miss while the cache entry is still in memory works correctly. ''' root = self.mkdtemp() c = Cache(root) input = self.mkstemp() with open(input, 'wt') as f: f.write('foo bar') inputs = prime_inputs([input]) cwd = os.getcwd() c.save(['arg1', 'arg2'], cwd, 'hello world', inputs) # Now cause the entry to be invalid by modifying inputs. with open(input, 'wt') as f: f.write('bar foo') # Ensure we miss when now performing a lookup. output = c.load(['arg1', 'arg2'], cwd) self.assertIsNone(output)