Skip to content

Commit 4971a73

Browse files
committed
Testing socket.error handling
1 parent 95d1a10 commit 4971a73

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
nose
22
coverage
33
hacking
4+
mock

tests/test_memcache.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import multiprocessing
55
import os
66
import signal
7+
import socket
78
import sys
89
import traceback
910
import unittest
@@ -12,6 +13,8 @@
1213

1314
from memcache import Client, SERVER_MAX_KEY_LENGTH, SERVER_MAX_VALUE_LENGTH # noqa: H301
1415

16+
from mock import Mock, patch
17+
1518

1619
class FooStruct(object):
1720

@@ -172,6 +175,41 @@ def test_disconnect_all_delete_multi(self):
172175
ret = self.mc.delete_multi({'keyhere': 'a', 'keythere': 'b'})
173176
self.assertEqual(ret, 1)
174177

178+
def test_socket_error(self):
179+
"""Tests case when socket.error exception was raised"""
180+
self.mc.set('socket.error', 1)
181+
server = Mock(
182+
# TODO should we catch secket.error when establishing connection?
183+
# connect=Mock(side_effect=socket.error(-1, 'connect error')),
184+
send_cmd=Mock(side_effect=socket.error(-1, 'send cmd error')),
185+
send_cmds=Mock(side_effect=socket.error(-1, 'send cmds error')),
186+
flush=Mock(side_effect=socket.error(-1, 'flush error')),
187+
)
188+
with patch.object(self.mc, 'servers', [server]), \
189+
patch.object(self.mc, 'buckets', [server]):
190+
self.assertEqual(self.mc.set('socket.error', 2), 0)
191+
self.assertEqual(
192+
self.mc.set_multi({'socket.error': 2}),
193+
['socket.error']
194+
)
195+
self.assertIs(self.mc.incr('socket.error'), None)
196+
self.assertIs(self.mc.decr('socket.error'), None)
197+
self.assertEqual(self.mc.add('socket.error', 5), 0)
198+
self.assertEqual(self.mc.append('socket.error', 9), 0)
199+
self.assertEqual(self.mc.prepend('socket.error', 1), 0)
200+
self.assertEqual(self.mc.replace('socket.error', 100), 0)
201+
self.assertEqual(self.mc.cas('socket.error', 100), 0)
202+
self.assertEqual(self.mc.delete('socket.error'), 0)
203+
self.assertEqual(self.mc.delete_multi(['socket.error']), 0)
204+
self.assertEqual(self.mc.touch('socket.error'), 0)
205+
self.assertIs(self.mc.get('socket.error'), None)
206+
self.assertIs(self.mc.gets('socket.error'), None)
207+
self.assertEqual(self.mc.get_multi(['socket.error']), {})
208+
self.assertRaises(socket.error, self.mc.get_stats)
209+
self.assertRaises(socket.error, self.mc.get_slab_stats)
210+
self.assertRaises(socket.error, self.mc.get_slabs)
211+
self.assertRaises(socket.error, self.mc.flush_all)
212+
175213
def test_exception_handling(self):
176214
"""Tests closing socket when custom exception raised"""
177215
queue = multiprocessing.Queue()

0 commit comments

Comments
 (0)