class TestWebGatewayCache(object): @pytest.fixture(autouse=True) def setUp (self, request): def fin (): os.system('rm -fr test_cache') request.addfinalizer(fin) self.wcache = WebGatewayCache(backend=FileCache, basedir='test_cache') class r: def __init__ (self): self.REQUEST = {'c':'1|292:1631$FF0000,2|409:5015$0000FF','m':'c', 'q':'0.9'} def new (self, q): rv = self.__class__() rv.REQUEST.update(q) return rv self.request = r() def testCacheSettings (self): uid = 123 #empty_size, cache_block = _testCacheFSBlockSize(self.wcache._thumb_cache) self.wcache._updateCacheSettings(self.wcache._thumb_cache, timeout=2, max_entries=5, max_size=0 ) cachestr = 'abcdefgh'*127 self.wcache._thumb_cache.wipe() for i in range(6): self.wcache.setThumb(self.request, 'test', uid, i, cachestr) max_size = self.wcache._thumb_cache._du() self.wcache._updateCacheSettings(self.wcache._thumb_cache, timeout=2, max_entries=5, max_size=max_size ) self.wcache._thumb_cache.wipe() for i in range(6): self.wcache.setThumb(self.request, 'test', uid, i, cachestr) for i in range(4): assert self.wcache.getThumb(self.request, 'test', uid, i) == cachestr,\ 'Key %d not properly cached' % i assert self.wcache.getThumb(self.request, 'test', uid, 5) is None, 'Size limit failed' for i in range(10): self.wcache.setThumb(self.request, 'test', uid, i, 'abcdefgh') for i in range(5): assert self.wcache.getThumb(self.request, 'test', uid, i) == 'abcdefgh', 'Key %d not properly cached' % i assert self.wcache.getThumb(self.request, 'test', uid, 5) is None, 'Entries limit failed' time.sleep(2) assert self.wcache.getThumb(self.request, 'test', uid, 0) is None, 'Time limit failed' def testThumbCache (self): uid = 123 assert self.wcache.getThumb(self.request, 'test', uid, 1) is None self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') assert self.wcache.getThumb(self.request, 'test', uid, 1) == 'thumbdata',\ 'Thumb not properly cached (%s)' % self.wcache.getThumb(self.request, 'test', uid, 1) self.wcache.clearThumb(self.request, 'test', uid, 1) assert self.wcache.getThumb(self.request, 'test', uid, 1) is None # Make sure clear() nukes this self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') assert self.wcache.getThumb(self.request, 'test', uid, 1) == 'thumbdata', 'Thumb not properly cached' assert self.wcache._thumb_cache._num_entries != 0 self.wcache.clear() assert self.wcache._thumb_cache._num_entries == 0 def testImageCache (self): uid = 123 # Also add a thumb, a split channel and a projection, as it should get deleted with image preq = self.request.new({'p':'intmax'}) assert self.wcache.getThumb(self.request, 'test', uid, 1) is None self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') assert self.wcache.getThumb(self.request, 'test', uid, 1) == 'thumbdata' img = omero.gateway.ImageWrapper(None, omero.model.ImageI(1,False)) assert self.wcache.getImage(self.request, 'test', img, 2, 3) is None self.wcache.setImage(self.request, 'test', img, 2, 3, 'imagedata') assert self.wcache.getImage(self.request, 'test', img, 2, 3) == 'imagedata' assert self.wcache.getImage(preq, 'test', img, 2, 3) is None self.wcache.setImage(preq, 'test', img, 2, 3, 'imagedata') assert self.wcache.getImage(preq, 'test', img, 2, 3) == 'imagedata' assert self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3) is None self.wcache.setSplitChannelImage(self.request, 'test', img, 2, 3, 'imagedata') assert self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3) == 'imagedata' self.wcache.clearImage(self.request, 'test', uid, img) assert self.wcache.getImage(self.request, 'test', img, 2, 3) is None assert self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3) is None assert self.wcache.getImage(preq, 'test', img, 2, 3) is None assert self.wcache.getThumb(self.request, 'test', uid, 1) is None # The exact same behaviour, using invalidateObject self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') assert self.wcache.getThumb(self.request, 'test', uid, 1) == 'thumbdata' self.wcache.setImage(self.request, 'test', img, 2, 3, 'imagedata') assert self.wcache.getImage(self.request, 'test', img, 2, 3) == 'imagedata' assert self.wcache.getImage(preq, 'test', img, 2, 3) is None self.wcache.setImage(preq, 'test', img, 2, 3, 'imagedata') assert self.wcache.getImage(preq, 'test', img, 2, 3) == 'imagedata' assert self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3) is None self.wcache.setSplitChannelImage(self.request, 'test', img, 2, 3, 'imagedata') assert self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3) == 'imagedata' self.wcache.invalidateObject('test', uid, img) assert self.wcache.getImage(self.request, 'test', img, 2, 3) is None assert self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3) is None assert self.wcache.getImage(preq, 'test', img, 2, 3) is None assert self.wcache.getThumb(self.request, 'test', uid, 1) is None # Make sure clear() nukes this assert self.wcache.getImage(self.request, 'test', img, 2, 3) is None self.wcache.setImage(self.request, 'test', img, 2, 3, 'imagedata') assert self.wcache.getImage(self.request, 'test', img, 2, 3) == 'imagedata' assert self.wcache._img_cache._num_entries != 0 self.wcache.clear() assert self.wcache._img_cache._num_entries == 0 def testLocks (self): wcache2 = WebGatewayCache(backend=FileCache, basedir=self.wcache._basedir) #wcache2 will hold the lock assert wcache2.tryLock() assert not self.wcache.tryLock() assert wcache2.tryLock() del wcache2 # The lock should have been removed assert self.wcache.tryLock() def testJsonCache (self): uid = 123 ds = omero.gateway.DatasetWrapper(None, omero.model.DatasetI(1,False)) assert self.wcache.getDatasetContents(self.request, 'test', ds) is None self.wcache.setDatasetContents(self.request, 'test', ds, 'datasetdata') assert self.wcache.getDatasetContents(self.request, 'test', ds) == 'datasetdata' self.wcache.clearDatasetContents(self.request, 'test', ds) assert self.wcache.getDatasetContents(self.request, 'test', ds) is None # The exact same behaviour, using invalidateObject assert self.wcache.getDatasetContents(self.request, 'test', ds) is None self.wcache.setDatasetContents(self.request, 'test', ds, 'datasetdata') assert self.wcache.getDatasetContents(self.request, 'test', ds) == 'datasetdata' self.wcache.invalidateObject('test', uid, ds) assert self.wcache.getDatasetContents(self.request, 'test', ds) is None # Make sure clear() nukes this assert self.wcache.getDatasetContents(self.request, 'test', ds) is None self.wcache.setDatasetContents(self.request, 'test', ds, 'datasetdata') assert self.wcache.getDatasetContents(self.request, 'test', ds) == 'datasetdata' assert self.wcache._json_cache._num_entries != 0 self.wcache.clear() assert self.wcache._json_cache._num_entries == 0
class WebGatewayCacheTest(unittest.TestCase): def setUp (self): self.wcache = WebGatewayCache(backend=FileCache, basedir='test_cache') class r: def __init__ (self): self.REQUEST = {'c':'1|292:1631$FF0000,2|409:5015$0000FF','m':'c', 'q':'0.9'} def new (self, q): rv = self.__class__() rv.REQUEST.update(q) return rv self.request = r() def tearDown (self): os.system('rm -fr test_cache') def testCacheSettings (self): uid = 123 #empty_size, cache_block = _testCacheFSBlockSize(self.wcache._thumb_cache) self.wcache._updateCacheSettings(self.wcache._thumb_cache, timeout=2, max_entries=5, max_size=0 ) cachestr = 'abcdefgh'*127 self.wcache._thumb_cache.wipe() for i in range(6): self.wcache.setThumb(self.request, 'test', uid, i, cachestr) max_size = self.wcache._thumb_cache._du() self.wcache._updateCacheSettings(self.wcache._thumb_cache, timeout=2, max_entries=5, max_size=max_size ) self.wcache._thumb_cache.wipe() for i in range(6): self.wcache.setThumb(self.request, 'test', uid, i, cachestr) for i in range(4): self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, i), cachestr, 'Key %d not properly cached' % i) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 5), None, 'Size limit failed') for i in range(10): self.wcache.setThumb(self.request, 'test', uid, i, 'abcdefgh') for i in range(5): self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, i), 'abcdefgh', 'Key %d not properly cached' % i) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 5), None, 'Entries limit failed') time.sleep(2) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 0), None, 'Time limit failed') def testThumbCache (self): uid = 123 self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), None) self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), 'thumbdata', 'Thumb not properly cached (%s)' % self.wcache.getThumb(self.request, 'test', uid, 1)) self.wcache.clearThumb(self.request, 'test', uid, 1) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), None) # Make sure clear() nukes this self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), 'thumbdata', 'Thumb not properly cached') self.assertNotEqual(self.wcache._thumb_cache._num_entries, 0) self.wcache.clear() self.assertEqual(self.wcache._thumb_cache._num_entries, 0) def testImageCache (self): uid = 123 # Also add a thumb, a split channel and a projection, as it should get deleted with image preq = self.request.new({'p':'intmax'}) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), None) self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), 'thumbdata') img = omero.gateway.ImageWrapper(None, omero.model.ImageI(1,False)) self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), None) self.wcache.setImage(self.request, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), 'imagedata') self.assertEqual(self.wcache.getImage(preq, 'test', img, 2, 3), None) self.wcache.setImage(preq, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getImage(preq, 'test', img, 2, 3), 'imagedata') self.assertEqual(self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3), None) self.wcache.setSplitChannelImage(self.request, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3), 'imagedata') self.wcache.clearImage(self.request, 'test', uid, img) self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), None) self.assertEqual(self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3), None) self.assertEqual(self.wcache.getImage(preq, 'test', img, 2, 3), None) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), None) # The exact same behaviour, using invalidateObject self.wcache.setThumb(self.request, 'test', uid, 1, 'thumbdata') self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), 'thumbdata') self.wcache.setImage(self.request, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), 'imagedata') self.assertEqual(self.wcache.getImage(preq, 'test', img, 2, 3), None) self.wcache.setImage(preq, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getImage(preq, 'test', img, 2, 3), 'imagedata') self.assertEqual(self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3), None) self.wcache.setSplitChannelImage(self.request, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3), 'imagedata') self.wcache.invalidateObject('test', uid, img) self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), None) self.assertEqual(self.wcache.getSplitChannelImage(self.request, 'test', img, 2, 3), None) self.assertEqual(self.wcache.getImage(preq, 'test', img, 2, 3), None) self.assertEqual(self.wcache.getThumb(self.request, 'test', uid, 1), None) # Make sure clear() nukes this self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), None) self.wcache.setImage(self.request, 'test', img, 2, 3, 'imagedata') self.assertEqual(self.wcache.getImage(self.request, 'test', img, 2, 3), 'imagedata') self.assertNotEqual(self.wcache._img_cache._num_entries, 0) self.wcache.clear() self.assertEqual(self.wcache._img_cache._num_entries, 0) def testLocks (self): wcache2 = WebGatewayCache(backend=FileCache, basedir=self.wcache._basedir) #wcache2 will hold the lock self.assert_(wcache2.tryLock()) self.assert_(not self.wcache.tryLock()) self.assert_(wcache2.tryLock()) del wcache2 # The lock should have been removed self.assert_(self.wcache.tryLock()) def testJsonCache (self): uid = 123 ds = omero.gateway.DatasetWrapper(None, omero.model.DatasetI(1,False)) self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), None) self.wcache.setDatasetContents(self.request, 'test', ds, 'datasetdata') self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), 'datasetdata') self.wcache.clearDatasetContents(self.request, 'test', ds) self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), None) # The exact same behaviour, using invalidateObject self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), None) self.wcache.setDatasetContents(self.request, 'test', ds, 'datasetdata') self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), 'datasetdata') self.wcache.invalidateObject('test', uid, ds) self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), None) # Make sure clear() nukes this self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), None) self.wcache.setDatasetContents(self.request, 'test', ds, 'datasetdata') self.assertEqual(self.wcache.getDatasetContents(self.request, 'test', ds), 'datasetdata') self.assertNotEqual(self.wcache._json_cache._num_entries, 0) self.wcache.clear() self.assertEqual(self.wcache._json_cache._num_entries, 0)
class WebGatewayCacheTest(unittest.TestCase): def setUp(self): self.wcache = WebGatewayCache(backend=FileCache, basedir="test_cache") class r: def __init__(self): self.REQUEST = {"c": "1|292:1631$FF0000,2|409:5015$0000FF", "m": "c", "q": "0.9"} def new(self, q): rv = self.__class__() rv.REQUEST.update(q) return rv self.request = r() def tearDown(self): os.system("rm -fr test_cache") def testCacheSettings(self): empty_size, cache_block = _testCacheFSBlockSize(self.wcache._thumb_cache) max_size = empty_size + 4 * cache_block + 1 self.wcache._updateCacheSettings(self.wcache._thumb_cache, timeout=2, max_entries=5, max_size=max_size) for i in range(6): self.wcache.setThumb(self.request, "test", i, "abcdefgh" * 127 * cache_block) for i in range(4): self.assertEqual( self.wcache.getThumb(self.request, "test", i), "abcdefgh" * 127 * cache_block, "Key %d not properly cached" % i, ) self.assertEqual(self.wcache.getThumb(self.request, "test", 5), None, "Size limit failed") for i in range(10): self.wcache.setThumb(self.request, "test", i, "abcdefgh") for i in range(5): self.assertEqual( self.wcache.getThumb(self.request, "test", i), "abcdefgh", "Key %d not properly cached" % i ) self.assertEqual(self.wcache.getThumb(self.request, "test", 5), None, "Entries limit failed") time.sleep(2) self.assertEqual(self.wcache.getThumb(self.request, "test", 0), None, "Time limit failed") def testThumbCache(self): self.assertEqual(self.wcache.getThumb(self.request, "test", 1), None) self.wcache.setThumb(self.request, "test", 1, "thumbdata") self.assertEqual( self.wcache.getThumb(self.request, "test", 1), "thumbdata", "Thumb not properly cached (%s)" % self.wcache.getThumb(self.request, "test", 1), ) self.wcache.clearThumb(self.request, "test", 1) self.assertEqual(self.wcache.getThumb(self.request, "test", 1), None) # Make sure clear() nukes this self.wcache.setThumb(self.request, "test", 1, "thumbdata") self.assertEqual(self.wcache.getThumb(self.request, "test", 1), "thumbdata", "Thumb not properly cached") self.assertNotEqual(self.wcache._thumb_cache._num_entries, 0) self.wcache.clear() self.assertEqual(self.wcache._thumb_cache._num_entries, 0) def testImageCache(self): # Also add a thumb, a split channel and a projection, as it should get deleted with image preq = self.request.new({"p": "intmax"}) self.assertEqual(self.wcache.getThumb(self.request, "test", 1), None) self.wcache.setThumb(self.request, "test", 1, "thumbdata") self.assertEqual(self.wcache.getThumb(self.request, "test", 1), "thumbdata") img = omero.gateway.ImageWrapper(None, omero.model.ImageI(1, False)) self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), None) self.wcache.setImage(self.request, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), "imagedata") self.assertEqual(self.wcache.getImage(preq, "test", img, 2, 3), None) self.wcache.setImage(preq, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getImage(preq, "test", img, 2, 3), "imagedata") self.assertEqual(self.wcache.getSplitChannelImage(self.request, "test", img, 2, 3), None) self.wcache.setSplitChannelImage(self.request, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getSplitChannelImage(self.request, "test", img, 2, 3), "imagedata") self.wcache.clearImage(self.request, "test", img) self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), None) self.assertEqual(self.wcache.getSplitChannelImage(self.request, "test", img, 2, 3), None) self.assertEqual(self.wcache.getImage(preq, "test", img, 2, 3), None) self.assertEqual(self.wcache.getThumb(self.request, "test", 1), None) # The exact same behaviour, using invalidateObject self.wcache.setThumb(self.request, "test", 1, "thumbdata") self.assertEqual(self.wcache.getThumb(self.request, "test", 1), "thumbdata") self.wcache.setImage(self.request, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), "imagedata") self.assertEqual(self.wcache.getImage(preq, "test", img, 2, 3), None) self.wcache.setImage(preq, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getImage(preq, "test", img, 2, 3), "imagedata") self.assertEqual(self.wcache.getSplitChannelImage(self.request, "test", img, 2, 3), None) self.wcache.setSplitChannelImage(self.request, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getSplitChannelImage(self.request, "test", img, 2, 3), "imagedata") self.wcache.invalidateObject("test", img) self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), None) self.assertEqual(self.wcache.getSplitChannelImage(self.request, "test", img, 2, 3), None) self.assertEqual(self.wcache.getImage(preq, "test", img, 2, 3), None) self.assertEqual(self.wcache.getThumb(self.request, "test", 1), None) # Make sure clear() nukes this self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), None) self.wcache.setImage(self.request, "test", img, 2, 3, "imagedata") self.assertEqual(self.wcache.getImage(self.request, "test", img, 2, 3), "imagedata") self.assertNotEqual(self.wcache._img_cache._num_entries, 0) self.wcache.clear() self.assertEqual(self.wcache._img_cache._num_entries, 0) def testLocks(self): wcache2 = WebGatewayCache(backend=FileCache, basedir=self.wcache._basedir) # wcache2 will hold the lock self.assert_(wcache2.tryLock()) self.assert_(not self.wcache.tryLock()) self.assert_(wcache2.tryLock()) del wcache2 # The lock should have been removed self.assert_(self.wcache.tryLock()) def testJsonCache(self): ds = omero.gateway.DatasetWrapper(None, omero.model.DatasetI(1, False)) self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), None) self.wcache.setDatasetContents(self.request, "test", ds, "datasetdata") self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), "datasetdata") self.wcache.clearDatasetContents(self.request, "test", ds) self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), None) # The exact same behaviour, using invalidateObject self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), None) self.wcache.setDatasetContents(self.request, "test", ds, "datasetdata") self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), "datasetdata") self.wcache.invalidateObject("test", ds) self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), None) # Make sure clear() nukes this self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), None) self.wcache.setDatasetContents(self.request, "test", ds, "datasetdata") self.assertEqual(self.wcache.getDatasetContents(self.request, "test", ds), "datasetdata") self.assertNotEqual(self.wcache._json_cache._num_entries, 0) self.wcache.clear() self.assertEqual(self.wcache._json_cache._num_entries, 0)