1 from __future__ import print_function
2 from __future__ import absolute_import
16 from . import run_test_server
20 return bytearray(random.getrandbits(8) for _ in xrange(n))
23 class CacheTestThread(threading.Thread):
24 def __init__(self, dir):
25 super(CacheTestThread, self).__init__()
29 c = arvados.cache.SafeHTTPCache(self._dir)
30 url = 'http://example.com/foo'
34 data_in = _random(128)
35 data_in = md5.new(data_in).hexdigest() + "\n" + str(data_in)
38 digest, content = data_out.split("\n", 1)
39 if digest != md5.new(content).hexdigest():
41 except Exception as err:
43 print("cache failed: {}".format(err), file=sys.stderr)
46 class CacheTest(unittest.TestCase):
48 self._dir = tempfile.mkdtemp()
51 shutil.rmtree(self._dir)
53 def test_cache_create_error(self):
54 _, filename = tempfile.mkstemp()
55 home_was = os.environ['HOME']
56 os.environ['HOME'] = filename
58 c = arvados.http_cache('test')
59 self.assertEqual(None, c)
61 os.environ['HOME'] = home_was
64 def test_cache_crud(self):
65 c = arvados.cache.SafeHTTPCache(self._dir, max_age=0)
66 url = 'https://example.com/foo?bar=baz'
69 self.assertEqual(None, c.get(url))
72 self.assertEqual(data1, c.get(url))
74 self.assertEqual(None, c.get(url))
77 self.assertEqual(data2, c.get(url))
79 def test_cache_threads(self):
82 t = CacheTestThread(dir=self._dir)
90 class CacheIntegrationTest(run_test_server.TestCaseWithServers):
93 def test_cache_used_by_default_client(self):
94 with mock.patch('arvados.cache.SafeHTTPCache.get') as getter:
95 arvados.api('v1')._rootDesc.get('foobar')
96 getter.assert_called()