当前位置: 首页>>代码示例>>Python>>正文


Python Service.clearCache方法代码示例

本文整理汇总了Python中WMCore.Services.Service.Service.clearCache方法的典型用法代码示例。如果您正苦于以下问题:Python Service.clearCache方法的具体用法?Python Service.clearCache怎么用?Python Service.clearCache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WMCore.Services.Service.Service的用法示例。


在下文中一共展示了Service.clearCache方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testUsingStaleCache

# 需要导入模块: from WMCore.Services.Service import Service [as 别名]
# 或者: from WMCore.Services.Service.Service import clearCache [as 别名]
    def testUsingStaleCache(self):
        myConfig = {'logger': self.logger,
                'endpoint': 'http://cmssw.cvs.cern.ch',
                'cacheduration': 0.0005,  # cache file lasts 1.8 secs
                'timeout': 10,
                'usestalecache': True,
                # 'cachepath' : self.cache_path,
                # 'req_cache_path': '%s/requests' % self.cache_path
                }
        service = Service(myConfig)
        cache = 'stalecachetest'

        # Start test from a clear cache
        service.clearCache(cache)

        cachefile = service.cacheFileName(cache)

        self.logger.info('1st call to refreshCache - should fail, there is no cache file')
        self.assertRaises(HTTPException, service.refreshCache, cache, '/lies')

        cacheddata = 'this data is mouldy'
        with open(cachefile, 'w') as f:
            f.write(cacheddata)

        self.logger.info('2nd call to refreshCache - should pass, data comes from the valid cache')
        data = service.refreshCache(cache, '/lies').read()
        self.assertEqual(cacheddata, data)

        # power nap to avoid letting the cache expire
        time.sleep(1)
        self.logger.info('3rd call to refreshCache - should pass, cache is still valid')
        data = service.refreshCache(cache, '/lies').read()
        self.assertEqual(cacheddata, data)

        # sleep a while longer so the cache dies out
        time.sleep(2)
        self.logger.info('4th call to refreshCache - should fail, cache is dead now')
        self.assertRaises(HTTPException, service.refreshCache, cache, '/lies')

        # touch/renew the file again
        cacheddata = 'foo'
        with open(cachefile, 'w') as f:
            f.write(cacheddata)

        # disable usage of stale cache, so doesn't call the endpoint if cache is valid
        service['usestalecache'] = False
        self.logger.info('5th call to refreshCache - should pass, cache is still valid')
        data = service.refreshCache(cache, '/lies').read()
        self.assertEqual(cacheddata, data)

        # consider the cache dead
        service['cacheduration'] = 0
        time.sleep(1)
        self.logger.info('6th call to refreshCache - should fail, cache is dead now')
        self.assertRaises(HTTPException, service.refreshCache, cache, '/lies')
开发者ID:vkuznet,项目名称:WMCore,代码行数:57,代码来源:Service_t.py

示例2: ServiceTest

# 需要导入模块: from WMCore.Services.Service import Service [as 别名]
# 或者: from WMCore.Services.Service.Service import clearCache [as 别名]
class ServiceTest(unittest.TestCase):
    def setUp(self):
        """
        Setup for unit tests
        """
        self.testInit = TestInit(__file__)
        self.testDir = self.testInit.generateWorkDir()
        testname = self.id().split('.')[-1]

        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                            datefmt='%m-%d %H:%M',
                            filename='service_unittests.log',
                            filemode='w')

        logger_name = 'Service%s' % testname.replace('test', '', 1)

        self.logger = logging.getLogger(logger_name)

        # self.cache_path = tempfile.mkdtemp()
        test_dict = {'logger': self.logger,
                     'endpoint': 'https://github.com/dmwm'}

        self.myService = Service(test_dict)

        test_dict['endpoint'] = 'http://cmssw-test.cvs.cern.ch/cgi-bin/cmssw.cgi'
        self.myService2 = Service(test_dict)
        self.testUrl = 'http://cern.ch'

        self.port = 8888
        cherrypy.config.update({'server.socket_port': self.port})

    def tearDown(self):
        self.testInit.delWorkDir()
        # There was old code here to see if the test passed and send a message to
        # self.logger.info It broke in 2.7, so if needed find a supported way to do it
        return

    def testIsFile(self):
        """
        Test the `isfile` utilitarian function
        """
        f = tempfile.NamedTemporaryFile(prefix="testIsFile", delete=True)
        self.assertTrue(isfile(f))
        f.close()
        self.assertTrue(isfile(f))

        strio = StringIO.StringIO()
        self.assertTrue(isfile(strio))
        strio.close()
        self.assertTrue(isfile(strio))

        self.assertFalse(isfile("/data/srv/alan.txt"))
        self.assertFalse(isfile(1))
        self.assertFalse(isfile(None))

    def testCacheExpired(self):
        """
        Test the `cache_expired` utilitarian function. Delta is in hours
        """
        # file-like object is always considered expired
        fcache = tempfile.NamedTemporaryFile(prefix="testIsFile", delete=True)
        self.assertTrue(cache_expired(fcache, delta=0))
        self.assertTrue(cache_expired(fcache, delta=100))
        fcache.close()
        self.assertTrue(cache_expired(fcache, delta=0))
        self.assertTrue(cache_expired(fcache, delta=100))

        # path to a file that does not exist, always expired
        newfile = fcache.name + 'testCacheExpired'
        self.assertTrue(cache_expired(newfile, delta=0))
        self.assertTrue(cache_expired(newfile, delta=100))

        # now create and write something to it
        with open(newfile, 'w') as f:
            f.write("whatever")

        self.assertFalse(cache_expired(newfile, delta=1))
        time.sleep(1)
        self.assertTrue(cache_expired(newfile, delta=0))
        self.assertFalse(cache_expired(newfile, delta=1))

    def testClear(self):
        """
        Populate the cache, and then check that it's deleted
        """
        f = self.myService.refreshCache('testClear', '/WMCore/blob/master/setup.py#L11')
        self.assertTrue(os.path.exists(f.name))
        f.close()

        self.myService.clearCache('testClear')
        self.assertFalse(os.path.exists(f.name))

    def testClearAndRepopulate(self):
        """
        Populate the cache, and then check that it's deleted
        """
        f = self.myService.refreshCache('testClear', '/WMCore/blob/master/setup.py#L11')
        self.assertTrue(os.path.exists(f.name))
        f.close()
#.........这里部分代码省略.........
开发者ID:vkuznet,项目名称:WMCore,代码行数:103,代码来源:Service_t.py


注:本文中的WMCore.Services.Service.Service.clearCache方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。