test_client.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. # Copyright 2012 Pinterest.com
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from builtins import bytes as newbytes
  16. import collections
  17. import errno
  18. import functools
  19. import json
  20. import os
  21. import mock
  22. import platform
  23. import re
  24. import socket
  25. import sys
  26. import unittest
  27. import pytest
  28. from library.pymemcache.client.base import (
  29. PooledClient,
  30. Client,
  31. normalize_server_spec,
  32. KeepaliveOpts,
  33. check_key_helper
  34. )
  35. from library.pymemcache.exceptions import (
  36. MemcacheClientError,
  37. MemcacheServerError,
  38. MemcacheUnexpectedCloseError,
  39. MemcacheUnknownCommandError,
  40. MemcacheUnknownError,
  41. MemcacheIllegalInputError
  42. )
  43. from library.pymemcache import pool
  44. from library.pymemcache.test.utils import MockMemcacheClient
  45. # TODO: Use ipaddress module when dropping support for Python < 3.3
  46. def is_ipv6(address):
  47. return re.match(r'^[0-9a-f:]+$', address)
  48. @pytest.mark.parametrize(
  49. 'key,allow_unicode_keys,key_prefix,ex_exception,ex_excinfo,ignore_py27',
  50. [
  51. (u'b'*251, True, b'',
  52. MemcacheIllegalInputError, 'Key is too long', False),
  53. (u'foo bar', True, b'',
  54. MemcacheIllegalInputError, 'Key contains whitespace', False),
  55. (u'\00', True, b'',
  56. MemcacheIllegalInputError, 'Key contains null', False),
  57. (None, True, b'', TypeError, None, False),
  58. # The following test won't fail with a TypeError with python 2.7
  59. (b"", False, '', TypeError, None, True),
  60. ])
  61. @pytest.mark.unit()
  62. def test_check_key_helper_failing_conditions(key, allow_unicode_keys,
  63. key_prefix, ex_exception,
  64. ex_excinfo, ignore_py27):
  65. if ignore_py27 and sys.version_info < (3, 0, 0):
  66. pytest.skip("skipping for Python 2.7")
  67. with pytest.raises(ex_exception) as excinfo:
  68. check_key_helper(key, allow_unicode_keys, key_prefix)
  69. # Allow to ignore the excinfo value. Different implementation (2.7,
  70. # pypy, 3.x) comes with various error messages for the same thing.
  71. # We just check the kind of the exception.
  72. if ex_excinfo:
  73. assert ex_excinfo in str(excinfo.value)
  74. @pytest.mark.unit()
  75. def test_check_key_helper():
  76. assert check_key_helper(b"key", True, b'') == b"key"
  77. assert check_key_helper("key", True) == b"key"
  78. assert isinstance(check_key_helper(b"key", True), bytes)
  79. assert check_key_helper("", True) == b""
  80. class MockSocket(object):
  81. def __init__(self, recv_bufs, connect_failure=None, close_failure=None):
  82. self.recv_bufs = collections.deque(recv_bufs)
  83. self.send_bufs = []
  84. self.closed = False
  85. self.timeouts = []
  86. self.connect_failure = connect_failure
  87. self.close_failure = close_failure
  88. self.connections = []
  89. self.socket_options = []
  90. @property
  91. def family(self):
  92. any_ipv6 = any(is_ipv6(c[0]) for c in self.connections)
  93. return socket.AF_INET6 if any_ipv6 else socket.AF_INET
  94. def sendall(self, value):
  95. self.send_bufs.append(value)
  96. def close(self):
  97. if isinstance(self.close_failure, Exception):
  98. raise self.close_failure
  99. self.closed = True
  100. def recv(self, size):
  101. value = self.recv_bufs.popleft()
  102. if isinstance(value, Exception):
  103. raise value
  104. return value
  105. def settimeout(self, timeout):
  106. self.timeouts.append(timeout)
  107. def connect(self, server):
  108. if isinstance(self.connect_failure, Exception):
  109. raise self.connect_failure
  110. self.connections.append(server)
  111. def setsockopt(self, level, option, value):
  112. self.socket_options.append((level, option, value))
  113. class MockUnixSocketServer(object):
  114. def __init__(self, socket_path):
  115. if os.path.exists(socket_path):
  116. os.remove(socket_path)
  117. self.socket_path = socket_path
  118. self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  119. def __enter__(self):
  120. self.socket.bind(self.socket_path)
  121. self.socket.listen(1)
  122. return self.socket
  123. def __exit__(self, *args):
  124. self.socket.close()
  125. os.remove(self.socket_path)
  126. class MockSocketModule(object):
  127. def __init__(self, connect_failure=None, close_failure=None):
  128. self.connect_failure = connect_failure
  129. self.close_failure = close_failure
  130. self.sockets = []
  131. def socket(self, family, type, proto=0, fileno=None):
  132. socket = MockSocket(
  133. [],
  134. connect_failure=self.connect_failure,
  135. close_failure=self.close_failure)
  136. self.sockets.append(socket)
  137. return socket
  138. def getaddrinfo(self, host, port, family=0, type=0, proto=0, flags=0):
  139. family = family or (
  140. socket.AF_INET6 if is_ipv6(host) else socket.AF_INET
  141. )
  142. type = type or socket.SOCK_STREAM
  143. proto = proto or socket.IPPROTO_TCP
  144. sockaddr = (
  145. ('::1', 11211, 0, 0)
  146. if family == socket.AF_INET6
  147. else ('127.0.0.1', 11211)
  148. )
  149. return [(family, type, proto, '', sockaddr)]
  150. def __getattr__(self, name):
  151. return getattr(socket, name)
  152. class CustomizedClient(Client):
  153. def _extract_value(self, expect_cas, line, buf, remapped_keys,
  154. prefixed_keys):
  155. return b'key', b'value', b'END\r\n'
  156. @pytest.mark.unit()
  157. class ClientTestMixin(object):
  158. def make_client(self, mock_socket_values, **kwargs):
  159. client = Client(None, **kwargs)
  160. # mock out client._connect() rather than hard-settting client.sock to
  161. # ensure methods are checking whether self.sock is None before
  162. # attempting to use it
  163. sock = MockSocket(list(mock_socket_values))
  164. client._connect = mock.Mock(side_effect=functools.partial(
  165. setattr, client, "sock", sock))
  166. return client
  167. def make_customized_client(self, mock_socket_values, **kwargs):
  168. client = CustomizedClient(None, **kwargs)
  169. # mock out client._connect() rather than hard-settting client.sock to
  170. # ensure methods are checking whether self.sock is None before
  171. # attempting to use it
  172. sock = MockSocket(list(mock_socket_values))
  173. client._connect = mock.Mock(side_effect=functools.partial(
  174. setattr, client, "sock", sock))
  175. return client
  176. def test_set_success(self):
  177. client = self.make_client([b'STORED\r\n'])
  178. result = client.set(b'key', b'value', noreply=False)
  179. assert result is True
  180. # unit test for encoding passed in __init__()
  181. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  182. result = client.set(b'key', b'value', noreply=False)
  183. assert result is True
  184. # unit test for set operation with parameter flags
  185. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  186. result = client.set(b'key', b'value', noreply=False, flags=0x00000030)
  187. assert result is True
  188. def test_set_future(self):
  189. client = self.make_client([b'STORED\r\n'])
  190. result = client.set(newbytes(b'key'), newbytes(b'value'), noreply=False)
  191. assert result is True
  192. # unit test for encoding passed in __init__()
  193. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  194. result = client.set(newbytes(b'key'), newbytes(b'value'), noreply=False)
  195. assert result is True
  196. # unit test for set operation with parameter flags
  197. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  198. result = client.set(newbytes(b'key'), newbytes(b'value'), noreply=False,
  199. flags=0x00000030)
  200. assert result is True
  201. def test_set_unicode_key(self):
  202. client = self.make_client([b''])
  203. def _set():
  204. client.set(u'\u0FFF', b'value', noreply=False)
  205. with pytest.raises(MemcacheIllegalInputError):
  206. _set()
  207. def test_set_unicode_key_ok(self):
  208. client = self.make_client([b'STORED\r\n'], allow_unicode_keys=True)
  209. result = client.set(u'\u0FFF', b'value', noreply=False)
  210. assert result is True
  211. def test_set_unicode_key_ok_snowman(self):
  212. client = self.make_client([b'STORED\r\n'], allow_unicode_keys=True)
  213. result = client.set('my☃', b'value', noreply=False)
  214. assert result is True
  215. def test_set_unicode_char_in_middle_of_key(self):
  216. client = self.make_client([b'STORED\r\n'])
  217. def _set():
  218. client.set('helloworld_\xb1901520_%c3', b'value', noreply=False)
  219. with pytest.raises(MemcacheIllegalInputError):
  220. _set()
  221. def test_set_unicode_char_in_middle_of_key_snowman(self):
  222. client = self.make_client([b'STORED\r\n'])
  223. def _set():
  224. client.set('my☃', b'value', noreply=False)
  225. with pytest.raises(MemcacheIllegalInputError):
  226. _set()
  227. def test_set_unicode_value(self):
  228. client = self.make_client([b''])
  229. def _set():
  230. client.set(b'key', u'\u0FFF', noreply=False)
  231. with pytest.raises(MemcacheIllegalInputError):
  232. _set()
  233. def test_set_unicode_char_in_middle_of_key_ok(self):
  234. client = self.make_client([b'STORED\r\n'], allow_unicode_keys=True)
  235. result = client.set('helloworld_\xb1901520_%c3', b'value',
  236. noreply=False)
  237. assert result is True
  238. def test_set_noreply(self):
  239. client = self.make_client([])
  240. result = client.set(b'key', b'value', noreply=True)
  241. assert result is True
  242. # unit test for encoding passed in __init__()
  243. client = self.make_client([], encoding='utf-8')
  244. result = client.set(b'key', b'value', noreply=True)
  245. assert result is True
  246. def test_set_many_success(self):
  247. client = self.make_client([b'STORED\r\n'])
  248. result = client.set_many({b'key': b'value'}, noreply=False)
  249. assert result == []
  250. # unit test for encoding passed in __init__()
  251. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  252. result = client.set_many({b'key': b'value'}, noreply=False)
  253. assert result == []
  254. def test_set_multi_success(self):
  255. # Should just map to set_many
  256. client = self.make_client([b'STORED\r\n'])
  257. result = client.set_multi({b'key': b'value'}, noreply=False)
  258. assert result == []
  259. # unit test for encoding passed in __init__()
  260. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  261. result = client.set_multi({b'key': b'value'}, noreply=False)
  262. assert result == []
  263. def test_add_stored(self):
  264. client = self.make_client([b'STORED\r', b'\n'])
  265. result = client.add(b'key', b'value', noreply=False)
  266. assert result is True
  267. # unit test for encoding passed in __init__()
  268. client = self.make_client([b'STORED\r', b'\n'], encoding='utf-8')
  269. result = client.add(b'key', b'value', noreply=False)
  270. assert result is True
  271. def test_add_not_stored(self):
  272. client = self.make_client([b'STORED\r', b'\n',
  273. b'NOT_', b'STOR', b'ED', b'\r\n'])
  274. client.add(b'key', b'value', noreply=False)
  275. result = client.add(b'key', b'value', noreply=False)
  276. assert result is False
  277. # unit test for encoding passed in __init__()
  278. client = self.make_client([b'STORED\r', b'\n',
  279. b'NOT_', b'STOR', b'ED', b'\r\n'],
  280. encoding='utf-8')
  281. client.add(b'key', b'value', noreply=False)
  282. result = client.add(b'key', b'value', noreply=False)
  283. assert result is False
  284. def test_get_not_found(self):
  285. client = self.make_client([b'END\r\n'])
  286. result = client.get(b'key')
  287. assert result is None
  288. # Unit test for customized client (override _extract_value)
  289. client = self.make_customized_client([b'END\r\n'])
  290. result = client.get(b'key')
  291. assert result is None
  292. def test_space_key(self):
  293. client = self.make_client([b''])
  294. with pytest.raises(MemcacheIllegalInputError):
  295. client.get(b'space key')
  296. with pytest.raises(MemcacheIllegalInputError):
  297. client.set(b'space key', b'value')
  298. def test_get_not_found_default(self):
  299. client = self.make_client([b'END\r\n'])
  300. result = client.get(b'key', default='foobar')
  301. assert result == 'foobar'
  302. # Unit test for customized client (override _extract_value)
  303. client = self.make_customized_client([b'END\r\n'])
  304. result = client.get(b'key', default='foobar')
  305. assert result == 'foobar'
  306. def test_get_found(self):
  307. client = self.make_client([
  308. b'STORED\r\n',
  309. b'VALUE key 0 5\r\nvalue\r\nEND\r\n',
  310. ])
  311. client.set(b'key', b'value', noreply=False)
  312. result = client.get(b'key')
  313. assert result == b'value'
  314. # Unit test for customized client (override _extract_value)
  315. client = self.make_customized_client([
  316. b'STORED\r\n',
  317. b'VALUE key 0 5\r\nvalue\r\nEND\r\n',
  318. ])
  319. client.set(b'key', b'value', noreply=False)
  320. result = client.get(b'key')
  321. assert result == b'value'
  322. def test_get_many_none_found(self):
  323. client = self.make_client([b'END\r\n'])
  324. result = client.get_many([b'key1', b'key2'])
  325. assert result == {}
  326. def test_get_multi_none_found(self):
  327. client = self.make_client([b'END\r\n'])
  328. result = client.get_multi([b'key1', b'key2'])
  329. assert result == {}
  330. def test_get_many_some_found(self):
  331. client = self.make_client([
  332. b'STORED\r\n',
  333. b'VALUE key1 0 6\r\nvalue1\r\nEND\r\n',
  334. ])
  335. client.set(b'key1', b'value1', noreply=False)
  336. result = client.get_many([b'key1', b'key2'])
  337. assert result == {b'key1': b'value1'}
  338. def test_get_many_all_found(self):
  339. client = self.make_client([
  340. b'STORED\r\n',
  341. b'STORED\r\n',
  342. b'VALUE key1 0 6\r\nvalue1\r\n',
  343. b'VALUE key2 0 6\r\nvalue2\r\nEND\r\n',
  344. ])
  345. client.set(b'key1', b'value1', noreply=False)
  346. client.set(b'key2', b'value2', noreply=False)
  347. result = client.get_many([b'key1', b'key2'])
  348. assert result == {b'key1': b'value1', b'key2': b'value2'}
  349. def test_get_unicode_key(self):
  350. client = self.make_client([b''])
  351. def _get():
  352. client.get(u'\u0FFF')
  353. with pytest.raises(MemcacheIllegalInputError):
  354. _get()
  355. def test_delete_not_found(self):
  356. client = self.make_client([b'NOT_FOUND\r\n'])
  357. result = client.delete(b'key', noreply=False)
  358. assert result is False
  359. def test_delete_found(self):
  360. client = self.make_client([b'STORED\r', b'\n', b'DELETED\r\n'])
  361. client.add(b'key', b'value', noreply=False)
  362. result = client.delete(b'key', noreply=False)
  363. assert result is True
  364. def test_delete_noreply(self):
  365. client = self.make_client([])
  366. result = client.delete(b'key', noreply=True)
  367. assert result is True
  368. def test_delete_many_no_keys(self):
  369. client = self.make_client([])
  370. result = client.delete_many([], noreply=False)
  371. assert result is True
  372. def test_delete_many_none_found(self):
  373. client = self.make_client([b'NOT_FOUND\r\n'])
  374. result = client.delete_many([b'key'], noreply=False)
  375. assert result is True
  376. def test_delete_many_found(self):
  377. client = self.make_client([b'STORED\r', b'\n', b'DELETED\r\n'])
  378. client.add(b'key', b'value', noreply=False)
  379. result = client.delete_many([b'key'], noreply=False)
  380. assert result is True
  381. def test_delete_many_some_found(self):
  382. client = self.make_client([
  383. b'STORED\r\n',
  384. b'DELETED\r\n',
  385. b'NOT_FOUND\r\n'
  386. ])
  387. client.add(b'key', b'value', noreply=False)
  388. result = client.delete_many([b'key', b'key2'], noreply=False)
  389. assert result is True
  390. def test_delete_multi_some_found(self):
  391. client = self.make_client([
  392. b'STORED\r\n',
  393. b'DELETED\r\n',
  394. b'NOT_FOUND\r\n'
  395. ])
  396. client.add(b'key', b'value', noreply=False)
  397. result = client.delete_multi([b'key', b'key2'], noreply=False)
  398. assert result is True
  399. def test_incr_not_found(self):
  400. client = self.make_client([b'NOT_FOUND\r\n'])
  401. result = client.incr(b'key', 1, noreply=False)
  402. assert result is None
  403. def test_incr_found(self):
  404. client = self.make_client([b'STORED\r\n', b'1\r\n'])
  405. client.set(b'key', 0, noreply=False)
  406. result = client.incr(b'key', 1, noreply=False)
  407. assert result == 1
  408. def test_incr_noreply(self):
  409. client = self.make_client([b'STORED\r\n'])
  410. client.set(b'key', 0, noreply=False)
  411. client = self.make_client([])
  412. result = client.incr(b'key', 1, noreply=True)
  413. assert result is None
  414. def test_decr_not_found(self):
  415. client = self.make_client([b'NOT_FOUND\r\n'])
  416. result = client.decr(b'key', 1, noreply=False)
  417. assert result is None
  418. def test_decr_found(self):
  419. client = self.make_client([b'STORED\r\n', b'1\r\n'])
  420. client.set(b'key', 2, noreply=False)
  421. result = client.decr(b'key', 1, noreply=False)
  422. assert result == 1
  423. class TestClient(ClientTestMixin, unittest.TestCase):
  424. Client = Client
  425. def test_append_stored(self):
  426. client = self.make_client([b'STORED\r\n'])
  427. result = client.append(b'key', b'value', noreply=False)
  428. assert result is True
  429. # unit test for encoding passed in __init__()
  430. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  431. result = client.append(b'key', b'value', noreply=False)
  432. assert result is True
  433. def test_prepend_stored(self):
  434. client = self.make_client([b'STORED\r\n'])
  435. result = client.prepend(b'key', b'value', noreply=False)
  436. assert result is True
  437. # unit test for encoding passed in __init__()
  438. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  439. result = client.prepend(b'key', b'value', noreply=False)
  440. assert result is True
  441. def test_cas_malformed(self):
  442. client = self.make_client([b'STORED\r\n'])
  443. with pytest.raises(MemcacheIllegalInputError):
  444. client.cas(b'key', b'value', None, noreply=False)
  445. with pytest.raises(MemcacheIllegalInputError):
  446. client.cas(b'key', b'value', 'nonintegerstring', noreply=False)
  447. with pytest.raises(MemcacheIllegalInputError):
  448. # even a space makes it a noninteger string
  449. client.cas(b'key', b'value', '123 ', noreply=False)
  450. with pytest.raises(MemcacheIllegalInputError):
  451. # non-ASCII digit
  452. client.cas(b'key', b'value', u'⁰', noreply=False)
  453. def test_cas_stored(self):
  454. client = self.make_client([b'STORED\r\n'])
  455. result = client.cas(b'key', b'value', b'123', noreply=False)
  456. assert result is True
  457. # unit test for encoding passed in __init__()
  458. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  459. result = client.cas(b'key', b'value', b'123', noreply=False)
  460. assert result is True
  461. def test_cas_exists(self):
  462. client = self.make_client([b'EXISTS\r\n'])
  463. result = client.cas(b'key', b'value', b'123', noreply=False)
  464. assert result is False
  465. # unit test for encoding passed in __init__()
  466. client = self.make_client([b'EXISTS\r\n'], encoding='utf-8')
  467. result = client.cas(b'key', b'value', b'123', noreply=False)
  468. assert result is False
  469. def test_cas_not_found(self):
  470. client = self.make_client([b'NOT_FOUND\r\n'])
  471. result = client.cas(b'key', b'value', b'123', noreply=False)
  472. assert result is None
  473. # unit test for encoding passed in __init__()
  474. client = self.make_client([b'NOT_FOUND\r\n'], encoding='utf-8')
  475. result = client.cas(b'key', b'value', b'123', noreply=False)
  476. assert result is None
  477. def test_cr_nl_boundaries(self):
  478. client = self.make_client([b'VALUE key1 0 6\r',
  479. b'\nvalue1\r\n'
  480. b'VALUE key2 0 6\r\n',
  481. b'value2\r\n'
  482. b'END\r\n'])
  483. result = client.get_many([b'key1', b'key2'])
  484. assert result == {b'key1': b'value1', b'key2': b'value2'}
  485. client = self.make_client([b'VALUE key1 0 6\r\n',
  486. b'value1\r',
  487. b'\nVALUE key2 0 6\r\n',
  488. b'value2\r\n',
  489. b'END\r\n'])
  490. result = client.get_many([b'key1', b'key2'])
  491. assert result == {b'key1': b'value1', b'key2': b'value2'}
  492. client = self.make_client([b'VALUE key1 0 6\r\n',
  493. b'value1\r\n',
  494. b'VALUE key2 0 6\r',
  495. b'\nvalue2\r\n',
  496. b'END\r\n'])
  497. result = client.get_many([b'key1', b'key2'])
  498. assert result == {b'key1': b'value1', b'key2': b'value2'}
  499. client = self.make_client([b'VALUE key1 0 6\r\n',
  500. b'value1\r\n',
  501. b'VALUE key2 0 6\r\n',
  502. b'value2\r',
  503. b'\nEND\r\n'])
  504. result = client.get_many([b'key1', b'key2'])
  505. assert result == {b'key1': b'value1', b'key2': b'value2'}
  506. client = self.make_client([b'VALUE key1 0 6\r\n',
  507. b'value1\r\n',
  508. b'VALUE key2 0 6\r\n',
  509. b'value2\r\n',
  510. b'END\r',
  511. b'\n'])
  512. result = client.get_many([b'key1', b'key2'])
  513. assert result == {b'key1': b'value1', b'key2': b'value2'}
  514. client = self.make_client([b'VALUE key1 0 6\r',
  515. b'\nvalue1\r',
  516. b'\nVALUE key2 0 6\r',
  517. b'\nvalue2\r',
  518. b'\nEND\r',
  519. b'\n'])
  520. result = client.get_many([b'key1', b'key2'])
  521. assert result == {b'key1': b'value1', b'key2': b'value2'}
  522. def test_delete_exception(self):
  523. client = self.make_client([Exception('fail')])
  524. def _delete():
  525. client.delete(b'key', noreply=False)
  526. with pytest.raises(Exception):
  527. _delete()
  528. assert client.sock is None
  529. def test_flush_all(self):
  530. client = self.make_client([b'OK\r\n'])
  531. result = client.flush_all(noreply=False)
  532. assert result is True
  533. def test_incr_exception(self):
  534. client = self.make_client([Exception('fail')])
  535. def _incr():
  536. client.incr(b'key', 1)
  537. with pytest.raises(Exception):
  538. _incr()
  539. assert client.sock is None
  540. def test_get_error(self):
  541. client = self.make_client([b'ERROR\r\n'])
  542. def _get():
  543. client.get(b'key')
  544. with pytest.raises(MemcacheUnknownCommandError):
  545. _get()
  546. def test_get_recv_chunks(self):
  547. client = self.make_client([b'VALUE key', b' 0 5\r', b'\nvalue',
  548. b'\r\n', b'END', b'\r', b'\n'])
  549. result = client.get(b'key')
  550. assert result == b'value'
  551. def test_get_unknown_error(self):
  552. client = self.make_client([b'foobarbaz\r\n'])
  553. def _get():
  554. client.get(b'key')
  555. with pytest.raises(MemcacheUnknownError):
  556. _get()
  557. def test_gets_not_found(self):
  558. client = self.make_client([b'END\r\n'])
  559. result = client.gets(b'key')
  560. assert result == (None, None)
  561. def test_gets_not_found_defaults(self):
  562. client = self.make_client([b'END\r\n'])
  563. result = client.gets(b'key', default='foo', cas_default='bar')
  564. assert result == ('foo', 'bar')
  565. def test_gets_found(self):
  566. client = self.make_client([b'VALUE key 0 5 10\r\nvalue\r\nEND\r\n'])
  567. result = client.gets(b'key')
  568. assert result == (b'value', b'10')
  569. def test_gets_many_none_found(self):
  570. client = self.make_client([b'END\r\n'])
  571. result = client.gets_many([b'key1', b'key2'])
  572. assert result == {}
  573. def test_gets_many_some_found(self):
  574. client = self.make_client([b'VALUE key1 0 6 11\r\nvalue1\r\nEND\r\n'])
  575. result = client.gets_many([b'key1', b'key2'])
  576. assert result == {b'key1': (b'value1', b'11')}
  577. def test_touch_not_found(self):
  578. client = self.make_client([b'NOT_FOUND\r\n'])
  579. result = client.touch(b'key', noreply=False)
  580. assert result is False
  581. def test_touch_found(self):
  582. client = self.make_client([b'TOUCHED\r\n'])
  583. result = client.touch(b'key', noreply=False)
  584. assert result is True
  585. def test_quit(self):
  586. client = self.make_client([])
  587. result = client.quit()
  588. assert result is None
  589. assert client.sock is None
  590. def test_shutdown(self):
  591. client = self.make_client([MemcacheUnexpectedCloseError('shutdown')])
  592. result = client.shutdown()
  593. assert result is None
  594. def test_shutdown_disabled(self):
  595. def _shutdown():
  596. client = self.make_client([b'ERROR: shutdown not enabled\r\n'])
  597. client.shutdown()
  598. with pytest.raises(MemcacheUnknownCommandError):
  599. _shutdown()
  600. def test_replace_stored(self):
  601. client = self.make_client([b'STORED\r\n'])
  602. result = client.replace(b'key', b'value', noreply=False)
  603. assert result is True
  604. # unit test for encoding passed in __init__()
  605. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  606. result = client.replace(b'key', b'value', noreply=False)
  607. assert result is True
  608. def test_replace_not_stored(self):
  609. client = self.make_client([b'NOT_STORED\r\n'])
  610. result = client.replace(b'key', b'value', noreply=False)
  611. assert result is False
  612. # unit test for encoding passed in __init__
  613. client = self.make_client([b'NOT_STORED\r\n'], encoding='utf-8')
  614. result = client.replace(b'key', b'value', noreply=False)
  615. assert result is False
  616. def test_serialization(self):
  617. class JsonSerde(object):
  618. def serialize(self, key, value):
  619. return json.dumps(value).encode('ascii'), 0
  620. def deserialize(self, key, value, flags):
  621. return json.loads(value.decode('ascii'))
  622. client = self.make_client([b'STORED\r\n'], serde=JsonSerde())
  623. client.set('key', {'c': 'd'})
  624. assert client.sock.send_bufs == [
  625. b'set key 0 0 10 noreply\r\n{"c": "d"}\r\n'
  626. ]
  627. def test_serialization_flags(self):
  628. def _ser(key, value):
  629. return value, 1 if isinstance(value, int) else 0
  630. client = self.make_client(
  631. [b'STORED\r\n', b'STORED\r\n'], serializer=_ser)
  632. client.set_many(
  633. collections.OrderedDict([(b'a', b's'), (b'b', 0)]), noreply=False)
  634. assert client.sock.send_bufs == [
  635. b'set a 0 0 1\r\ns\r\nset b 1 0 1\r\n0\r\n'
  636. ]
  637. def test_serialization_overridden_flags(self):
  638. def _ser(key, value):
  639. return value, 1 if isinstance(value, int) else 0
  640. client = self.make_client(
  641. [b'STORED\r\n', b'STORED\r\n'], serializer=_ser)
  642. client.set_many(
  643. collections.OrderedDict([(b'a', b's'), (b'b', 0)]),
  644. noreply=False, flags=5)
  645. assert client.sock.send_bufs == [
  646. b'set a 5 0 1\r\ns\r\nset b 5 0 1\r\n0\r\n'
  647. ]
  648. def test_explicit_flags(self):
  649. client = self.make_client([b'STORED\r\n', b'STORED\r\n'])
  650. client.set_many(
  651. collections.OrderedDict([(b'a', b's'), (b'b', 0)]),
  652. noreply=False, flags=5)
  653. assert client.sock.send_bufs == [
  654. b'set a 5 0 1\r\ns\r\nset b 5 0 1\r\n0\r\n'
  655. ]
  656. def test_set_socket_handling(self):
  657. client = self.make_client([b'STORED\r\n'])
  658. result = client.set(b'key', b'value', noreply=False)
  659. assert result is True
  660. assert client.sock.closed is False
  661. assert len(client.sock.send_bufs) == 1
  662. def test_set_error(self):
  663. client = self.make_client([b'ERROR\r\n'])
  664. def _set():
  665. client.set(b'key', b'value', noreply=False)
  666. with pytest.raises(MemcacheUnknownCommandError):
  667. _set()
  668. def test_set_exception(self):
  669. client = self.make_client([Exception('fail')])
  670. def _set():
  671. client.set(b'key', b'value', noreply=False)
  672. with pytest.raises(Exception):
  673. _set()
  674. assert client.sock is None
  675. def test_set_client_error(self):
  676. client = self.make_client([b'CLIENT_ERROR some message\r\n'])
  677. def _set():
  678. client.set('key', 'value', noreply=False)
  679. with pytest.raises(MemcacheClientError):
  680. _set()
  681. def test_set_server_error(self):
  682. client = self.make_client([b'SERVER_ERROR some message\r\n'])
  683. def _set():
  684. client.set(b'key', b'value', noreply=False)
  685. with pytest.raises(MemcacheServerError):
  686. _set()
  687. def test_closing_socket_on_unexpected_closed_error(self):
  688. client = self.make_client([
  689. b'VALUE ',
  690. MemcacheUnexpectedCloseError("foo bar"),
  691. ])
  692. def _set():
  693. client.set(b'key', b'value', noreply=False)
  694. with pytest.raises(MemcacheUnexpectedCloseError):
  695. _set()
  696. assert client.sock is None
  697. def test_set_unknown_error(self):
  698. client = self.make_client([b'foobarbaz\r\n'])
  699. def _set():
  700. client.set(b'key', b'value', noreply=False)
  701. with pytest.raises(MemcacheUnknownError):
  702. _set()
  703. def test_set_key_with_space(self):
  704. client = self.make_client([b''])
  705. def _set():
  706. client.set(b'key has space', b'value', noreply=False)
  707. with pytest.raises(MemcacheIllegalInputError):
  708. _set()
  709. def test_set_key_with_newline(self):
  710. client = self.make_client([b''])
  711. def _set():
  712. client.set(b'key\n', b'value', noreply=False)
  713. with pytest.raises(MemcacheIllegalInputError):
  714. _set()
  715. def test_set_key_with_carriage_return(self):
  716. client = self.make_client([b''])
  717. def _set():
  718. client.set(b'key\r', b'value', noreply=False)
  719. with pytest.raises(MemcacheIllegalInputError):
  720. _set()
  721. def test_set_key_with_null_character(self):
  722. client = self.make_client([b''])
  723. def _set():
  724. client.set(b'key\00', b'value', noreply=False)
  725. with pytest.raises(MemcacheIllegalInputError):
  726. _set()
  727. def test_set_key_with_noninteger_expire(self):
  728. client = self.make_client([b''])
  729. class _OneLike(object):
  730. """object that looks similar to the int 1"""
  731. def __str__(self):
  732. return "1"
  733. for noreply in (True, False):
  734. for expire in (1.5, _OneLike(), "1"):
  735. def _set():
  736. client.set(b'finekey', b'finevalue',
  737. noreply=noreply, expire=expire)
  738. with pytest.raises(MemcacheIllegalInputError):
  739. _set()
  740. def test_set_many_socket_handling(self):
  741. client = self.make_client([b'STORED\r\n'])
  742. result = client.set_many({b'key': b'value'}, noreply=False)
  743. assert result == []
  744. assert client.sock.closed is False
  745. assert len(client.sock.send_bufs) == 1
  746. # unit test for encoding passed in __init__()
  747. client = self.make_client([b'STORED\r\n'], encoding='utf-8')
  748. result = client.set_many({b'key': b'value'}, noreply=False)
  749. assert result == []
  750. assert client.sock.closed is False
  751. assert len(client.sock.send_bufs) == 1
  752. def test_set_many_exception(self):
  753. client = self.make_client([b'STORED\r\n', Exception('fail')])
  754. def _set():
  755. client.set_many({b'key': b'value', b'other': b'value'},
  756. noreply=False)
  757. with pytest.raises(Exception):
  758. _set()
  759. assert client.sock is None
  760. # unit test for encoding passed in __init__()
  761. client = self.make_client([b'STORED\r\n', Exception('fail')],
  762. encoding='utf-8')
  763. def _set():
  764. client.set_many({b'key': b'value', b'other': b'value'},
  765. noreply=False)
  766. with pytest.raises(Exception):
  767. _set()
  768. assert client.sock is None
  769. def test_stats(self):
  770. client = self.make_client([b'STAT fake_stats 1\r\n', b'END\r\n'])
  771. result = client.stats()
  772. assert client.sock.send_bufs == [
  773. b'stats\r\n'
  774. ]
  775. assert result == {b'fake_stats': 1}
  776. def test_stats_with_args(self):
  777. client = self.make_client([b'STAT fake_stats 1\r\n', b'END\r\n'])
  778. result = client.stats('some_arg')
  779. assert client.sock.send_bufs == [
  780. b'stats some_arg\r\n'
  781. ]
  782. assert result == {b'fake_stats': 1}
  783. def test_stats_conversions(self):
  784. client = self.make_client([
  785. # Most stats are converted to int
  786. b'STAT cmd_get 2519\r\n',
  787. b'STAT cmd_set 3099\r\n',
  788. b'STAT evictions 939\r\n',
  789. # Unless they can't be, they remain str
  790. b'STAT libevent 2.0.19-stable\r\n',
  791. # Some named stats are explicitly converted
  792. b'STAT hash_is_expanding 0\r\n',
  793. b'STAT rusage_user 0.609165\r\n',
  794. b'STAT rusage_system 0.852791\r\n',
  795. b'STAT slab_reassign_running 1\r\n',
  796. b'STAT version 1.4.14\r\n',
  797. b'STAT umask 777\r\n',
  798. b'STAT auth_enabled_sasl yes\r\n',
  799. b'END\r\n',
  800. ])
  801. result = client.stats()
  802. assert client.sock.send_bufs == [
  803. b'stats\r\n'
  804. ]
  805. expected = {
  806. b'cmd_get': 2519,
  807. b'cmd_set': 3099,
  808. b'evictions': 939,
  809. b'libevent': b'2.0.19-stable',
  810. b'hash_is_expanding': False,
  811. b'rusage_user': 0.609165,
  812. b'rusage_system': 0.852791,
  813. b'slab_reassign_running': True,
  814. b'version': b'1.4.14',
  815. b'umask': 0o777,
  816. b'auth_enabled_sasl': True,
  817. }
  818. assert result == expected
  819. def test_stats_cachedump(self):
  820. client = self.make_client([b'ITEM bob [7 b; 0 s]\r\n', b'END\r\n'])
  821. result = client.stats('cachedump', '1', '1')
  822. assert client.sock.send_bufs == [
  823. b'stats cachedump 1 1\r\n'
  824. ]
  825. assert result == {b'bob': b'[7 b; 0 s]'}
  826. def test_cache_memlimit(self):
  827. client = self.make_client([b'OK\r\n'])
  828. result = client.cache_memlimit(8)
  829. assert client.sock.send_bufs == [
  830. b'cache_memlimit 8\r\n'
  831. ]
  832. assert result is True
  833. def test_python_dict_set_is_supported(self):
  834. client = self.make_client([b'STORED\r\n'])
  835. client[b'key'] = b'value'
  836. def test_python_dict_get_is_supported(self):
  837. client = self.make_client([b'VALUE key 0 5\r\nvalue\r\nEND\r\n'])
  838. assert client[b'key'] == b'value'
  839. def test_python_dict_get_not_found_is_supported(self):
  840. client = self.make_client([b'END\r\n'])
  841. def _get():
  842. client[b'key']
  843. with pytest.raises(KeyError):
  844. _get()
  845. def test_python_dict_del_is_supported(self):
  846. client = self.make_client([b'DELETED\r\n'])
  847. del client[b'key']
  848. def test_too_long_key(self):
  849. client = self.make_client([b'END\r\n'])
  850. with pytest.raises(MemcacheClientError):
  851. client.get(b'x' * 251)
  852. def test_too_long_unicode_key(self):
  853. client = self.make_client([b'STORED\r\n'], allow_unicode_keys=True)
  854. with pytest.raises(MemcacheClientError):
  855. client.get('my☃'*150)
  856. with pytest.raises(MemcacheClientError):
  857. client.get(u'\u0FFF'*150)
  858. def test_key_contains_space(self):
  859. client = self.make_client([b'END\r\n'])
  860. with pytest.raises(MemcacheClientError):
  861. client.get(b'abc xyz')
  862. def test_key_contains_nonascii(self):
  863. client = self.make_client([b'END\r\n'])
  864. with pytest.raises(MemcacheClientError):
  865. client.get(u'\u3053\u3093\u306b\u3061\u306f')
  866. def _default_noreply_false(self, cmd, args, response):
  867. client = self.make_client(response, default_noreply=False)
  868. result = getattr(client, cmd)(*args)
  869. assert result is False
  870. def _default_noreply_true(self, cmd, args, response):
  871. client = self.make_client(response, default_noreply=True)
  872. result = getattr(client, cmd)(*args)
  873. assert result is True
  874. def _default_noreply_true_and_empty_list(self, cmd, args, response):
  875. client = self.make_client(response, default_noreply=True)
  876. result = getattr(client, cmd)(*args)
  877. assert result == []
  878. def test_default_noreply_set(self):
  879. with pytest.raises(MemcacheUnknownError):
  880. self._default_noreply_false(
  881. 'set', (b'key', b'value'), [b'UNKNOWN\r\n'])
  882. self._default_noreply_false(
  883. 'set', (b'key', b'value'), [b'NOT_STORED\r\n'])
  884. self._default_noreply_true(
  885. 'set', (b'key', b'value'), [b'NOT_STORED\r\n'])
  886. def test_default_noreply_set_many(self):
  887. with pytest.raises(MemcacheUnknownError):
  888. client = self.make_client([b'UNKNOWN\r\n'], default_noreply=False)
  889. result = client.set_many({b'key': b'value'})
  890. assert result == [b'key']
  891. self._default_noreply_true_and_empty_list(
  892. 'set_many', ({b'key': b'value'},), [b'NOT_STORED\r\n'])
  893. def test_default_noreply_add(self):
  894. self._default_noreply_false(
  895. 'add', (b'key', b'value'), [b'NOT_STORED\r\n'])
  896. self._default_noreply_true(
  897. 'add', (b'key', b'value'), [b'NOT_STORED\r\n'])
  898. def test_default_noreply_replace(self):
  899. self._default_noreply_false(
  900. 'replace', (b'key', b'value'), [b'NOT_STORED\r\n'])
  901. self._default_noreply_true(
  902. 'replace', (b'key', b'value'), [b'NOT_STORED\r\n'])
  903. def test_default_noreply_append(self):
  904. self._default_noreply_false(
  905. 'append', (b'key', b'value'), [b'NOT_STORED\r\n'])
  906. self._default_noreply_true(
  907. 'append', (b'key', b'value'), [b'NOT_STORED\r\n'])
  908. def test_default_noreply_prepend(self):
  909. self._default_noreply_false(
  910. 'prepend', (b'key', b'value'), [b'NOT_STORED\r\n'])
  911. self._default_noreply_true(
  912. 'prepend', (b'key', b'value'), [b'NOT_STORED\r\n'])
  913. def test_default_noreply_touch(self):
  914. self._default_noreply_false('touch', (b'key',), [b'NOT_FOUND\r\n'])
  915. self._default_noreply_true('touch', (b'key',), [b'NOT_FOUND\r\n'])
  916. def test_default_noreply_flush_all(self):
  917. self._default_noreply_false('flush_all', (),
  918. [b'__FAKE_RESPONSE__\r\n'])
  919. self._default_noreply_true('flush_all', (), [b'__FAKE_RESPONSE__\r\n'])
  920. def test_version_success(self):
  921. client = self.make_client([b'VERSION 1.2.3\r\n'],
  922. default_noreply=False)
  923. result = client.version()
  924. assert result == b'1.2.3'
  925. def test_version_exception(self):
  926. client = self.make_client([b'INVALID DATA\r\n'], default_noreply=False)
  927. with pytest.raises(MemcacheUnknownError):
  928. client.version()
  929. @pytest.mark.unit()
  930. class TestClientSocketConnect(unittest.TestCase):
  931. def test_socket_connect_ipv4(self):
  932. server = ('127.0.0.1', 11211)
  933. client = Client(server, socket_module=MockSocketModule())
  934. client._connect()
  935. assert client.sock.connections == [server]
  936. assert client.sock.family == socket.AF_INET
  937. timeout = 2
  938. connect_timeout = 3
  939. client = Client(
  940. server, connect_timeout=connect_timeout, timeout=timeout,
  941. socket_module=MockSocketModule())
  942. client._connect()
  943. assert client.sock.timeouts == [connect_timeout, timeout]
  944. client = Client(server, socket_module=MockSocketModule())
  945. client._connect()
  946. assert client.sock.socket_options == []
  947. client = Client(
  948. server, socket_module=MockSocketModule(), no_delay=True)
  949. client._connect()
  950. assert client.sock.socket_options == [(socket.IPPROTO_TCP,
  951. socket.TCP_NODELAY, 1)]
  952. def test_socket_connect_ipv6(self):
  953. server = ('::1', 11211)
  954. client = Client(server, socket_module=MockSocketModule())
  955. client._connect()
  956. assert client.sock.connections == [server + (0, 0)]
  957. assert client.sock.family == socket.AF_INET6
  958. timeout = 2
  959. connect_timeout = 3
  960. client = Client(
  961. server, connect_timeout=connect_timeout, timeout=timeout,
  962. socket_module=MockSocketModule())
  963. client._connect()
  964. assert client.sock.timeouts == [connect_timeout, timeout]
  965. client = Client(server, socket_module=MockSocketModule())
  966. client._connect()
  967. assert client.sock.socket_options == []
  968. client = Client(
  969. server, socket_module=MockSocketModule(), no_delay=True)
  970. client._connect()
  971. assert client.sock.socket_options == [(socket.IPPROTO_TCP,
  972. socket.TCP_NODELAY, 1)]
  973. def test_socket_connect_unix(self):
  974. server = '/tmp/pymemcache.{pid}'.format(pid=os.getpid())
  975. with MockUnixSocketServer(server):
  976. client = Client(server)
  977. client._connect()
  978. assert client.sock.family == socket.AF_UNIX
  979. @unittest.skipIf('Linux' != platform.system(),
  980. 'Socket keepalive only support Linux platforms.')
  981. def test_linux_socket_keepalive(self):
  982. server = ('::1', 11211)
  983. try:
  984. client = Client(
  985. server,
  986. socket_module=MockSocketModule(),
  987. socket_keepalive=KeepaliveOpts())
  988. client._connect()
  989. except SystemError:
  990. self.fail("SystemError unexpectedly raised")
  991. with self.assertRaises(ValueError):
  992. # A KeepaliveOpts object is expected, a ValueError will be raised
  993. Client(
  994. server,
  995. socket_module=MockSocketModule(),
  996. socket_keepalive=True)
  997. @mock.patch('platform.system')
  998. def test_osx_socket_keepalive(self, platform_mock):
  999. platform_mock.return_value = 'Darwin'
  1000. server = ('::1', 11211)
  1001. # For the moment the socket keepalive is only implemented for Linux
  1002. with self.assertRaises(SystemError):
  1003. Client(
  1004. server,
  1005. socket_module=MockSocketModule(),
  1006. socket_keepalive=KeepaliveOpts())
  1007. @mock.patch('platform.system')
  1008. def test_windows_socket_keepalive(self, platform_mock):
  1009. platform_mock.return_value = 'Windows'
  1010. server = ('::1', 11211)
  1011. # For the moment the socket keepalive is only implemented for Linux
  1012. with self.assertRaises(SystemError):
  1013. Client(
  1014. server,
  1015. socket_module=MockSocketModule(),
  1016. socket_keepalive=KeepaliveOpts())
  1017. def test_socket_connect_closes_on_failure(self):
  1018. server = ("example.com", 11211)
  1019. socket_module = MockSocketModule(connect_failure=OSError())
  1020. client = Client(server, socket_module=socket_module)
  1021. with pytest.raises(OSError):
  1022. client._connect()
  1023. assert len(socket_module.sockets) == 1
  1024. assert socket_module.sockets[0].connections == []
  1025. assert socket_module.sockets[0].closed
  1026. def test_socket_close(self):
  1027. server = ("example.com", 11211)
  1028. client = Client(server, socket_module=MockSocketModule())
  1029. client._connect()
  1030. assert client.sock is not None
  1031. client.close()
  1032. assert client.sock is None
  1033. def test_socket_close_exception(self):
  1034. server = ("example.com", 11211)
  1035. socket_module = MockSocketModule(close_failure=OSError())
  1036. client = Client(server, socket_module=socket_module)
  1037. client._connect()
  1038. assert client.sock is not None
  1039. client.close()
  1040. assert client.sock is None
  1041. class TestPooledClient(ClientTestMixin, unittest.TestCase):
  1042. def make_client(self, mock_socket_values, **kwargs):
  1043. mock_client = Client(None, **kwargs)
  1044. mock_client.sock = MockSocket(list(mock_socket_values))
  1045. client = PooledClient(None, **kwargs)
  1046. client.client_pool = pool.ObjectPool(lambda: mock_client)
  1047. return client
  1048. def _default_noreply_false(self, cmd, args, response):
  1049. client = self.make_client(response, default_noreply=False)
  1050. result = getattr(client, cmd)(*args)
  1051. assert result is False
  1052. def _default_noreply_true(self, cmd, args, response):
  1053. client = self.make_client(response, default_noreply=True)
  1054. result = getattr(client, cmd)(*args)
  1055. assert result is True
  1056. def _default_noreply_true_and_empty_list(self, cmd, args, response):
  1057. client = self.make_client(response, default_noreply=True)
  1058. result = getattr(client, cmd)(*args)
  1059. assert result == []
  1060. def test_default_noreply_set(self):
  1061. with pytest.raises(MemcacheUnknownError):
  1062. self._default_noreply_false(
  1063. 'set', (b'key', b'value'), [b'UNKNOWN\r\n'])
  1064. self._default_noreply_false(
  1065. 'set', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1066. self._default_noreply_true(
  1067. 'set', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1068. def test_default_noreply_set_many(self):
  1069. with pytest.raises(MemcacheUnknownError):
  1070. client = self.make_client([b'UNKNOWN\r\n'], default_noreply=False)
  1071. client.set_many({b'key': b'value'})
  1072. self._default_noreply_true_and_empty_list(
  1073. 'set_many', ({b'key': b'value'},), [b'NOT_STORED\r\n'])
  1074. def test_default_noreply_add(self):
  1075. self._default_noreply_false(
  1076. 'add', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1077. self._default_noreply_true(
  1078. 'add', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1079. def test_default_noreply_replace(self):
  1080. self._default_noreply_false(
  1081. 'replace', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1082. self._default_noreply_true(
  1083. 'replace', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1084. def test_default_noreply_append(self):
  1085. self._default_noreply_false(
  1086. 'append', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1087. self._default_noreply_true(
  1088. 'append', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1089. def test_default_noreply_prepend(self):
  1090. self._default_noreply_false(
  1091. 'prepend', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1092. self._default_noreply_true(
  1093. 'prepend', (b'key', b'value'), [b'NOT_STORED\r\n'])
  1094. def test_default_noreply_touch(self):
  1095. self._default_noreply_false('touch', (b'key',), [b'NOT_FOUND\r\n'])
  1096. self._default_noreply_true('touch', (b'key',), [b'NOT_FOUND\r\n'])
  1097. def test_default_noreply_flush_all(self):
  1098. self._default_noreply_false('flush_all', (),
  1099. [b'__FAKE_RESPONSE__\r\n'])
  1100. self._default_noreply_true('flush_all', (), [b'__FAKE_RESPONSE__\r\n'])
  1101. def test_custom_client(self):
  1102. class MyClient(Client):
  1103. pass
  1104. client = PooledClient(('host', 11211))
  1105. client.client_class = MyClient
  1106. assert isinstance(client.client_pool.get(), MyClient)
  1107. class TestPooledClientIdleTimeout(ClientTestMixin, unittest.TestCase):
  1108. def make_client(self, mock_socket_values, **kwargs):
  1109. mock_client = Client(None, **kwargs)
  1110. mock_client.sock = MockSocket(list(mock_socket_values))
  1111. client = PooledClient(None, pool_idle_timeout=60, **kwargs)
  1112. client.client_pool = pool.ObjectPool(lambda: mock_client)
  1113. return client
  1114. def test_free_idle(self):
  1115. class Counter(object):
  1116. count = 0
  1117. def increment(self, obj):
  1118. self.count += 1
  1119. removed = Counter()
  1120. client = self.make_client([b'VALUE key 0 5\r\nvalue\r\nEND\r\n']*2)
  1121. client.client_pool._after_remove = removed.increment
  1122. client.client_pool._idle_clock = lambda: 0
  1123. client.set(b'key', b'value')
  1124. assert removed.count == 0
  1125. client.get(b'key')
  1126. assert removed.count == 0
  1127. # Advance clock to beyond the idle timeout.
  1128. client.client_pool._idle_clock = lambda: 61
  1129. client.get(b'key')
  1130. assert removed.count == 1
  1131. class TestMockClient(ClientTestMixin, unittest.TestCase):
  1132. def make_client(self, mock_socket_values, **kwargs):
  1133. client = MockMemcacheClient(None, **kwargs)
  1134. client.sock = MockSocket(list(mock_socket_values))
  1135. return client
  1136. def test_get_found(self):
  1137. client = self.make_client([
  1138. b'STORED\r\n',
  1139. b'VALUE key 0 5\r\nvalue\r\nEND\r\n',
  1140. ])
  1141. result = client.set(b'key', b'value', noreply=False)
  1142. result = client.get(b'key')
  1143. assert result == b'value'
  1144. def test_deserialization(self):
  1145. class JsonSerde(object):
  1146. def serialize(self, key, value):
  1147. if isinstance(value, dict):
  1148. return json.dumps(value).encode('UTF-8'), 1
  1149. return value, 0
  1150. def deserialize(self, key, value, flags):
  1151. if flags == 1:
  1152. return json.loads(value.decode('UTF-8'))
  1153. return value
  1154. client = self.make_client([
  1155. b'STORED\r\n',
  1156. b'VALUE key1 0 5\r\nhello\r\nEND\r\n',
  1157. b'STORED\r\n',
  1158. b'VALUE key2 0 18\r\n{"hello": "world"}\r\nEND\r\n',
  1159. ], serde=JsonSerde())
  1160. result = client.set(b'key1', b'hello', noreply=False)
  1161. result = client.get(b'key1')
  1162. assert result == b'hello'
  1163. result = client.set(b'key2', dict(hello='world'), noreply=False)
  1164. result = client.get(b'key2')
  1165. assert result == dict(hello='world')
  1166. class TestPrefixedClient(ClientTestMixin, unittest.TestCase):
  1167. def make_client(self, mock_socket_values, **kwargs):
  1168. client = Client(None, key_prefix=b'xyz:', **kwargs)
  1169. client.sock = MockSocket(list(mock_socket_values))
  1170. return client
  1171. def test_get_found(self):
  1172. client = self.make_client([
  1173. b'STORED\r\n',
  1174. b'VALUE xyz:key 0 5\r\nvalue\r\nEND\r\n',
  1175. ])
  1176. result = client.set(b'key', b'value', noreply=False)
  1177. result = client.get(b'key')
  1178. assert result == b'value'
  1179. def test_get_many_some_found(self):
  1180. client = self.make_client([
  1181. b'STORED\r\n',
  1182. b'VALUE xyz:key1 0 6\r\nvalue1\r\nEND\r\n',
  1183. ])
  1184. result = client.set(b'key1', b'value1', noreply=False)
  1185. result = client.get_many([b'key1', b'key2'])
  1186. assert result == {b'key1': b'value1'}
  1187. def test_get_many_all_found(self):
  1188. client = self.make_client([
  1189. b'STORED\r\n',
  1190. b'STORED\r\n',
  1191. b'VALUE xyz:key1 0 6\r\nvalue1\r\n',
  1192. b'VALUE xyz:key2 0 6\r\nvalue2\r\nEND\r\n',
  1193. ])
  1194. result = client.set(b'key1', b'value1', noreply=False)
  1195. result = client.set(b'key2', b'value2', noreply=False)
  1196. result = client.get_many([b'key1', b'key2'])
  1197. assert result == {b'key1': b'value1', b'key2': b'value2'}
  1198. def test_python_dict_get_is_supported(self):
  1199. client = self.make_client([b'VALUE xyz:key 0 5\r\nvalue\r\nEND\r\n'])
  1200. assert client[b'key'] == b'value'
  1201. class TestPrefixedPooledClient(TestPrefixedClient):
  1202. def make_client(self, mock_socket_values, **kwargs):
  1203. mock_client = Client(None, key_prefix=b'xyz:', **kwargs)
  1204. mock_client.sock = MockSocket(list(mock_socket_values))
  1205. client = PooledClient(None, key_prefix=b'xyz:', **kwargs)
  1206. client.client_pool = pool.ObjectPool(lambda: mock_client)
  1207. return client
  1208. @pytest.mark.unit()
  1209. class TestRetryOnEINTR(unittest.TestCase):
  1210. def make_client(self, values):
  1211. client = Client(None)
  1212. client.sock = MockSocket(list(values))
  1213. return client
  1214. def test_recv(self):
  1215. client = self.make_client([
  1216. b'VALUE ',
  1217. socket.error(errno.EINTR, "Interrupted system call"),
  1218. b'key1 0 6\r\nval',
  1219. socket.error(errno.EINTR, "Interrupted system call"),
  1220. b'ue1\r\nEND\r\n',
  1221. ])
  1222. assert client[b'key1'] == b'value1'
  1223. @pytest.mark.unit()
  1224. class TestNormalizeServerSpec(unittest.TestCase):
  1225. def test_normalize_server_spec(self):
  1226. f = normalize_server_spec
  1227. assert f(None) is None
  1228. assert f(('127.0.0.1', 12345)) == ('127.0.0.1', 12345)
  1229. assert f(['127.0.0.1', 12345]) == ('127.0.0.1', 12345)
  1230. assert f('unix:/run/memcached/socket') == '/run/memcached/socket'
  1231. assert f('/run/memcached/socket') == '/run/memcached/socket'
  1232. assert f('localhost') == ('localhost', 11211)
  1233. assert f('localhost:12345') == ('localhost', 12345)
  1234. assert f('[::1]') == ('::1', 11211)
  1235. assert f('[::1]:12345') == ('::1', 12345)
  1236. assert f('127.0.0.1') == ('127.0.0.1', 11211)
  1237. assert f('127.0.0.1:12345') == ('127.0.0.1', 12345)
  1238. with pytest.raises(ValueError) as excinfo:
  1239. f({'host': 12345})
  1240. assert str(excinfo.value) == "Unknown server provided: {'host': 12345}"
  1241. with pytest.raises(ValueError) as excinfo:
  1242. f(12345)
  1243. assert str(excinfo.value) == "Unknown server provided: 12345"
  1244. @pytest.mark.unit()
  1245. class TestKeepaliveopts(unittest.TestCase):
  1246. def test_keepalive_opts(self):
  1247. kao = KeepaliveOpts()
  1248. assert (kao.idle == 1 and kao.intvl == 1 and kao.cnt == 5)
  1249. kao = KeepaliveOpts(idle=1, intvl=4, cnt=2)
  1250. assert (kao.idle == 1 and kao.intvl == 4 and kao.cnt == 2)
  1251. kao = KeepaliveOpts(idle=8)
  1252. assert (kao.idle == 8 and kao.intvl == 1 and kao.cnt == 5)
  1253. kao = KeepaliveOpts(cnt=8)
  1254. assert (kao.idle == 1 and kao.intvl == 1 and kao.cnt == 8)
  1255. with self.assertRaises(ValueError):
  1256. KeepaliveOpts(cnt=0)
  1257. with self.assertRaises(ValueError):
  1258. KeepaliveOpts(idle=-1)