Skip to content

Commit

Permalink
Implemented the other functions and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek committed Jun 21, 2015
1 parent 8879147 commit 2470508
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 24 deletions.
68 changes: 61 additions & 7 deletions pymemcache/client/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,8 @@ def _get_client(self, key):
client = self.clients[server]
return client

def _run_cmd(self, cmd, key, *args, **kwargs):
def _safely_run_func(self, client, func, *args, **kwargs):
try:
can_run = True
client = self._get_client(key)
func = getattr(client, cmd)

if client.server in self._failed_clients:
# This server is currently failing, lets check if it is in retry
# or marked as dead
Expand All @@ -146,7 +142,7 @@ def _run_cmd(self, cmd, key, *args, **kwargs):
if time.time() - failed_time > self.retry_timeout:
print(failed_metadata)
print('retrying')
result = func(key, *args, **kwargs)
result = func(*args, **kwargs)
# we were successful, lets remove it from the failed
# clients
self._failed_clients.pop(client.server)
Expand All @@ -158,7 +154,7 @@ def _run_cmd(self, cmd, key, *args, **kwargs):
print('marking as dead')
self._remove_server(*client.server)

result = func(key, *args, **kwargs)
result = func(*args, **kwargs)
return result

# Connecting to the server fail, we should enter
Expand Down Expand Up @@ -194,8 +190,66 @@ def _run_cmd(self, cmd, key, *args, **kwargs):
failed_metadata['failed_time'] = time.time()
self._failed_clients[client.server] = failed_metadata

def _run_cmd(self, cmd, key, *args, **kwargs):
client = self._get_client(key)
func = getattr(client, cmd)
args = list(args)
args.insert(0, key)
return self._safely_run_func(client, func, *args, **kwargs)

def set(self, key, *args, **kwargs):
return self._run_cmd('set', key, *args, **kwargs)

def get(self, key, *args, **kwargs):
return self._run_cmd('get', key, *args, **kwargs)

def get(self, key, *args, **kwargs):
return self._run_cmd('get', key, *args, **kwargs)

def get_many(self, keys, *args, **kwargs):
client_batches = {}
for key in keys:
client = self._get_client(key)

if client.server not in client_batches:
client_batches[client.server] = []

client_batches[client.server].append(key)

end = {}

for server, keys in client_batches.items():
client = self.clients['%s:%s' % server]
new_args = [keys] + list(args)
result = self._safely_run_func(
client,
client.get_many, *new_args, **kwargs
)
end.update(result)

return end

def gets(self, key, *args, **kwargs):
return self._run_cmd('gets', key, *args, **kwargs)

def add(self, key, *args, **kwargs):
return self._run_cmd('add', key, *args, **kwargs)

def prepend(self, key, *args, **kwargs):
return self._run_cmd('prepend', key, *args, **kwargs)

def append(self, key, *args, **kwargs):
return self._run_cmd('append', key, *args, **kwargs)

def delete(self, key, *args, **kwargs):
return self._run_cmd('delete', key, *args, **kwargs)

def cas(self, key, *args, **kwargs):
return self._run_cmd('cas', key, *args, **kwargs)

def replace(self, key, *args, **kwargs):
return self._run_cmd('replace', key, *args, **kwargs)

def flush_all(self):
for _, client in self.clients.items():
self._safely_run_func(client, client.flush_all)
11 changes: 11 additions & 0 deletions pymemcache/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ def pytest_generate_tests(metafunc):
socket_modules.append(gevent_socket)

metafunc.parametrize("socket_module", socket_modules)

if 'client_class' in metafunc.fixturenames:
from pymemcache.client.base import PooledClient, Client
from pymemcache.client.hash import HashClient
class HashClientSingle(HashClient):
def __init__(self, server, *args, **kwargs):
super(HashClientSingle, self).__init__([server], *args, **kwargs)

metafunc.parametrize(
"client_class", [Client, PooledClient, HashClientSingle]
)
33 changes: 16 additions & 17 deletions pymemcache/test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@


@pytest.mark.integration()
def test_get_set(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_get_set(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

result = client.get('key')
Expand All @@ -47,8 +47,8 @@ def test_get_set(host, port, socket_module):


@pytest.mark.integration()
def test_add_replace(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_add_replace(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

result = client.add(b'key', b'value', noreply=False)
Expand All @@ -73,8 +73,8 @@ def test_add_replace(host, port, socket_module):


@pytest.mark.integration()
def test_append_prepend(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_append_prepend(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

result = client.append(b'key', b'value', noreply=False)
Expand All @@ -101,10 +101,9 @@ def test_append_prepend(host, port, socket_module):


@pytest.mark.integration()
def test_cas(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_cas(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

result = client.cas(b'key', b'value', b'1', noreply=False)
assert result is None

Expand All @@ -125,8 +124,8 @@ def test_cas(host, port, socket_module):


@pytest.mark.integration()
def test_gets(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_gets(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

result = client.gets(b'key')
Expand All @@ -139,8 +138,8 @@ def test_gets(host, port, socket_module):


@pytest.mark.delete()
def test_delete(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_delete(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

result = client.delete(b'key', noreply=False)
Expand All @@ -157,7 +156,7 @@ def test_delete(host, port, socket_module):


@pytest.mark.integration()
def test_incr_decr(host, port, socket_module):
def test_incr_decr(client_class, host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
client.flush_all()

Expand Down Expand Up @@ -185,7 +184,7 @@ def _bad_int():


@pytest.mark.integration()
def test_misc(host, port, socket_module):
def test_misc(client_class, host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
client.flush_all()

Expand All @@ -211,8 +210,8 @@ def _des(key, value, flags):


@pytest.mark.integration()
def test_errors(host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
def test_errors(client_class, host, port, socket_module):
client = client_class((host, port), socket_module=socket_module)
client.flush_all()

def _key_with_ws():
Expand Down

0 comments on commit 2470508

Please sign in to comment.