def bootstrapCaches(self): self.stage = 'Initializing Caches' from mythbox.util import NativeTranslator from mythbox.filecache import FileCache, HttpResolver, MythThumbnailFileCache from mythbox.mythtv.resolver import MythChannelIconResolver, MythThumbnailResolver from os.path import join from mythbox.mythtv.cache import DomainCache self.domainCache = DomainCache(bus=self.bus) cacheDir = self.platform.getCacheDir() self.translator = NativeTranslator(self.platform.getScriptDir()) self.mythThumbnailCache = MythThumbnailFileCache( join(cacheDir, 'thumbnail'), MythThumbnailResolver(), self.bus, self.domainCache) self.mythChannelIconCache = FileCache(join(cacheDir, 'channel'), MythChannelIconResolver()) self.httpCache = FileCache(join(cacheDir, 'http'), HttpResolver()) self.cachesByName = { 'mythThumbnailCache': self.mythThumbnailCache, 'mythChannelIconCache': self.mythChannelIconCache, 'httpCache': self.httpCache, 'domainCache': self.domainCache }
def test_get_When_file_in_cache_Then_return_filepath(self): # Setup - create file and place in cache fileUrl = self.createSomeFile() resolver = FileSystemResolver() digest = resolver.hash(fileUrl) resolver.store(fileUrl, os.path.join(self.cacheRootDir, digest)) cache = FileCache(self.cacheRootDir, resolver) try: # Test filePath = cache.get(fileUrl) # Verify - request for cached file should not pass through to resolver self.assertEquals(os.path.join(self.cacheRootDir, digest), filePath) finally: os.remove(fileUrl)
def setUp(self): self.nextProvider = Mock() self.dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, self.dir, ignore_errors=True) self.httpCache = FileCache(self.dir, HttpResolver()) self.program = TVProgram({'title': 'Not Important', 'category_type':'series'}, translator=Mock()) self.provider = HttpCachingFanartProvider(self.httpCache, self.nextProvider) self.addCleanup(self.provider.close)
def test_init_When_rootDir_does_not_exist_Then_rootDir_is_created(self): # Setup shutil.rmtree(self.cacheRootDir) self.assertFalse(os.path.exists(self.cacheRootDir)) # Test FileCache(self.cacheRootDir, FileSystemResolver()) # Verify self.assertTrue(os.path.exists(self.cacheRootDir)) self.assertTrue(os.path.isdir(self.cacheRootDir))
def test_clear_When_cache_cleared_Then_file_no_longer_in_cache(self): # Setup - create file and place in cache fileUrl = self.createSomeFile() cache = FileCache(self.cacheRootDir, FileSystemResolver()) cache.get(fileUrl) self.assertTrue(cache.contains(fileUrl)) try: # Test cache.clear() # Verify self.assertFalse(cache.contains(fileUrl)) finally: os.remove(fileUrl)
def test_end_to_end_tvdb_flow(self): no_op = NoOpFanartProvider() tvdb = TvdbFanartProvider(self.platform) fileCache = FileCache(os.path.join(self.sandbox, 'http'), HttpResolver()) http = HttpCachingFanartProvider(fileCache, tvdb) superfast = SuperFastFanartProvider(self.platform, http, filename='tvdb') strike = OneStrikeAndYoureOutFanartProvider(self.platform, superfast, no_op, filename='tvdb') fields = { 'title': u'The Real World', 'subtitle': u'', 'starttime': socketDateTime(2008, 11, 4, 22, 45, 00), 'endtime': socketDateTime(2008, 11, 4, 23, 50, 00), 'airdate': u'2010-07-14' } #self.assertSeasonAndEpisode(RecordedProgram(data=pdata(fields), **self.deps), u'24', u'3') program = RecordedProgram(data=pdata(fields), **self.deps) season, episode = strike.getSeasonAndEpisode(program) log.info(season) log.info(episode) self.assertEqual(('24', '3'), (season, episode)) method = 'getSeasonAndEpisode' # verify strikeout provider strikeKey = strike.createKey(method, program) self.assertFalse(strike.hasStruckOut(strikeKey)) # verify superfast provider superfastKey = superfast.createEpisodeKey(method, program) self.assertEqual(superfast.pcache[superfastKey], ('24', '3')) strike.close()
def test_init_When_rootDir_exists_as_file_Then_raise_error(self): # Setup shutil.rmtree(self.cacheRootDir) dummyFile = open(self.cacheRootDir, "w") dummyFile.write( 'i am a file blocking the creation of the root cache dir') dummyFile.close() self.assertTrue(os.path.isfile(self.cacheRootDir)) try: try: # Test FileCache(self.cacheRootDir, FileSystemResolver()) # Verify self.fail( 'Should have failed because rootCacheDir exists as a file') except Exception: log.debug( 'SUCCESS: Error thrown when file already exists as root cache dir' ) finally: os.remove(self.cacheRootDir)
def test_get_When_multiple_threads_want_the_same_resource_Then_first_thread_stores_resource_and_the_remaining_threads_block_and_return_cached_resource( self): fileToCache = self.createSomeFile() resolver = DelayedFileResolver() cache = FileCache(self.cacheRootDir, resolver) numThreads = 50 results = [None] * numThreads threads = [None] * numThreads @run_async def getFromCache(url, slot): results[slot] = cache.get(url) for i in range(numThreads): results[i] = None threads[i] = getFromCache(fileToCache, i) for t in threads: t.join() for r in results: self.assertFalse(r is None) self.assertEquals(1, resolver.timesCalled)