from._fixturesimport_GenericBackendTest,_GenericMutexTestfrom.importeq_fromunittestimportTestCasefromthreadingimportThreadimporttimefromnoseimportSkipTestclass_TestMemcachedConn(object):@classmethoddef_check_backend_available(cls,backend):try:client=backend._create_client()client.set("x","y")assertclient.get("x")=="y"except:raiseSkipTest("memcached is not running or ""otherwise not functioning correctly")class_NonDistributedMemcachedTest(_TestMemcachedConn,_GenericBackendTest):region_args={"key_mangler":lambdax:x.replace(" ","_")}config_args={"arguments":{"url":"127.0.0.1:11211"}}class_DistributedMemcachedTest(_TestMemcachedConn,_GenericBackendTest):region_args={"key_mangler":lambdax:x.replace(" ","_")}config_args={"arguments":{"url":"127.0.0.1:11211","distributed_lock":True}}class_DistributedMemcachedMutexTest(_TestMemcachedConn,_GenericMutexTest):config_args={"arguments":{"url":"127.0.0.1:11211","distributed_lock":True}}classPylibmcTest(_NonDistributedMemcachedTest):backend="dogpile.cache.pylibmc"classPylibmcDistributedTest(_DistributedMemcachedTest):backend="dogpile.cache.pylibmc"classPylibmcDistributedMutexTest(_DistributedMemcachedMutexTest):backend="dogpile.cache.pylibmc"classBMemcachedTest(_NonDistributedMemcachedTest):backend="dogpile.cache.bmemcached"classBMemcachedDistributedTest(_DistributedMemcachedTest):backend="dogpile.cache.bmemcached"classBMemcachedDistributedMutexTest(_DistributedMemcachedMutexTest):backend="dogpile.cache.bmemcached"classMemcachedTest(_NonDistributedMemcachedTest):backend="dogpile.cache.memcached"classMemcachedDistributedTest(_DistributedMemcachedTest):backend="dogpile.cache.memcached"classMemcachedDistributedMutexTest(_DistributedMemcachedMutexTest):backend="dogpile.cache.memcached"fromdogpile.cache.backends.memcachedimportGenericMemcachedBackendfromdogpile.cache.backends.memcachedimportPylibmcBackendclassMockMemcachedBackend(GenericMemcachedBackend):def_imports(self):passdef_create_client(self):returnMockClient(self.url)classMockPylibmcBackend(PylibmcBackend):def_imports(self):passdef_create_client(self):returnMockClient(self.url,binary=self.binary,behaviors=self.behaviors)classMockClient(object):number_of_clients=0def__init__(self,*arg,**kw):self.arg=argself.kw=kwself.canary=[]self._cache={}MockClient.number_of_clients+=1defget(self,key):returnself._cache.get(key)defset(self,key,value,**kw):self.canary.append(kw)self._cache[key]=valuedefdelete(self,key):self._cache.pop(key,None)def__del__(self):MockClient.number_of_clients-=1classPylibmcArgsTest(TestCase):deftest_binary_flag(self):backend=MockPylibmcBackend(arguments={'url':'foo','binary':True})eq_(backend._create_client().kw["binary"],True)deftest_url_list(self):backend=MockPylibmcBackend(arguments={'url':["a","b","c"]})eq_(backend._create_client().arg[0],["a","b","c"])deftest_url_scalar(self):backend=MockPylibmcBackend(arguments={'url':"foo"})eq_(backend._create_client().arg[0],["foo"])deftest_behaviors(self):backend=MockPylibmcBackend(arguments={'url':"foo","behaviors":{"q":"p"}})eq_(backend._create_client().kw["behaviors"],{"q":"p"})deftest_set_time(self):backend=MockPylibmcBackend(arguments={'url':"foo","memcached_expire_time":20})backend.set("foo","bar")eq_(backend._clients.memcached.canary,[{"time":20}])deftest_set_min_compress_len(self):backend=MockPylibmcBackend(arguments={'url':"foo","min_compress_len":20})backend.set("foo","bar")eq_(backend._clients.memcached.canary,[{"min_compress_len":20}])deftest_no_set_args(self):backend=MockPylibmcBackend(arguments={'url':"foo"})backend.set("foo","bar")eq_(backend._clients.memcached.canary,[{}])classMemcachedArgstest(TestCase):deftest_set_time(self):backend=MockMemcachedBackend(arguments={'url':"foo","memcached_expire_time":20})backend.set("foo","bar")eq_(backend._clients.memcached.canary,[{"time":20}])deftest_set_min_compress_len(self):backend=MockMemcachedBackend(arguments={'url':"foo","min_compress_len":20})backend.set("foo","bar")eq_(backend._clients.memcached.canary,[{"min_compress_len":20}])classLocalThreadTest(TestCase):defsetUp(self):importgcgc.collect()eq_(MockClient.number_of_clients,0)deftest_client_cleanup_1(self):self._test_client_cleanup(1)deftest_client_cleanup_3(self):self._test_client_cleanup(3)deftest_client_cleanup_10(self):self._test_client_cleanup(10)def_test_client_cleanup(self,count):backend=MockMemcachedBackend(arguments={'url':'foo'})canary=[]deff():backend._clients.memcachedcanary.append(MockClient.number_of_clients)time.sleep(.05)threads=[Thread(target=f)foriinrange(count)]fortinthreads:t.start()fortinthreads:t.join()eq_(canary,[i+1foriinrange(count)])eq_(MockClient.number_of_clients,0)