1 from __future__ import print_function
13 import run_test_server
17 return bytearray(random.getrandbits(8) for _ in xrange(n))
20 class CacheTestThread(threading.Thread):
21 def __init__(self, dir):
22 super(CacheTestThread, self).__init__()
26 c = arvados.cache.SafeHTTPCache(self._dir)
27 url = 'http://example.com/foo'
31 data_in = _random(128)
32 data_in = md5.new(data_in).hexdigest() + "\n" + str(data_in)
35 digest, content = data_out.split("\n", 1)
36 if digest != md5.new(content).hexdigest():
38 except Exception as err:
40 print("cache failed: {}".format(err), file=sys.stderr)
43 class CacheTest(unittest.TestCase):
45 self._dir = tempfile.mkdtemp()
48 shutil.rmtree(self._dir)
50 def test_cache_crud(self):
51 c = arvados.cache.SafeHTTPCache(self._dir, max_age=0)
52 url = 'https://example.com/foo?bar=baz'
55 self.assertEqual(None, c.get(url))
58 self.assertEqual(data1, c.get(url))
60 self.assertEqual(None, c.get(url))
63 self.assertEqual(data2, c.get(url))
65 def test_cache_threads(self):
68 t = CacheTestThread(dir=self._dir)
76 class CacheIntegrationTest(run_test_server.TestCaseWithServers):
79 def test_cache_used_by_default_client(self):
80 with mock.patch('arvados.cache.SafeHTTPCache.get') as getter:
81 arvados.api('v1')._rootDesc.get('foobar')
82 getter.assert_called()