This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
/
connection_test.py
281 lines (226 loc) · 10.7 KB
/
connection_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import unittest
import asyncio
import os
from ._testutil import BaseTest, run_until_complete
from aioredis import ReplyError, ProtocolError, RedisError
class ConnectionTest(BaseTest):
@run_until_complete
def test_connect_tcp(self):
conn = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
self.assertEqual(conn.db, 0)
self.assertEqual(str(conn), '<RedisConnection [db:0]>')
conn = yield from self.create_connection(
['localhost', self.redis_port], loop=self.loop)
self.assertEqual(conn.db, 0)
self.assertEqual(str(conn), '<RedisConnection [db:0]>')
@unittest.skipIf(not os.environ.get('REDIS_SOCKET'), "no redis socket")
@run_until_complete
def test_connect_unixsocket(self):
conn = yield from self.create_connection(
self.redis_socket, db=0, loop=self.loop)
self.assertEqual(conn.db, 0)
self.assertEqual(str(conn), '<RedisConnection [db:0]>')
def test_global_loop(self):
asyncio.set_event_loop(self.loop)
conn = self.loop.run_until_complete(self.create_connection(
('localhost', self.redis_port), db=0))
self.assertEqual(conn.db, 0)
self.assertIs(conn._loop, self.loop)
@run_until_complete
def test_select_db(self):
address = ('localhost', self.redis_port)
conn = yield from self.create_connection(address, loop=self.loop)
self.assertEqual(conn.db, 0)
with self.assertRaises(ValueError):
yield from self.create_connection(address, db=-1, loop=self.loop)
with self.assertRaises(TypeError):
yield from self.create_connection(address, db=1.0, loop=self.loop)
with self.assertRaises(TypeError):
yield from self.create_connection(
address, db='bad value', loop=self.loop)
with self.assertRaises(TypeError):
conn = yield from self.create_connection(
address, db=None, loop=self.loop)
yield from conn.select(None)
with self.assertRaises(ReplyError):
yield from self.create_connection(
address, db=100000, loop=self.loop)
yield from conn.select(1)
self.assertEqual(conn.db, 1)
yield from conn.select(2)
self.assertEqual(conn.db, 2)
yield from conn.execute('select', 0)
self.assertEqual(conn.db, 0)
yield from conn.execute(b'select', 1)
self.assertEqual(conn.db, 1)
@run_until_complete
def test_protocol_error(self):
loop = self.loop
conn = yield from self.create_connection(
('localhost', self.redis_port), loop=loop)
reader = conn._reader
with self.assertRaises(ProtocolError):
reader.feed_data(b'not good redis protocol response')
yield from conn.select(1)
self.assertEqual(len(conn._waiters), 0)
def test_close_connection__tcp(self):
loop = self.loop
conn = loop.run_until_complete(self.create_connection(
('localhost', self.redis_port), loop=loop))
conn.close()
with self.assertRaises(AssertionError):
loop.run_until_complete(conn.select(1))
conn = loop.run_until_complete(self.create_connection(
('localhost', self.redis_port), loop=loop))
with self.assertRaises(AssertionError):
conn.close()
fut = conn.select(1)
loop.run_until_complete(fut)
@unittest.skipIf(not os.environ.get('REDIS_SOCKET'), "no redis socket")
@run_until_complete
def test_close_connection__socket(self):
conn = yield from self.create_connection(
self.redis_socket, loop=self.loop)
conn.close()
with self.assertRaises(AssertionError):
yield from conn.select(1)
@run_until_complete
def test_auth(self):
conn = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
res = yield from conn.execute('CONFIG', 'SET', 'requirepass', 'pass')
self.assertEqual(res, b'OK')
conn2 = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
with self.assertRaises(ReplyError):
yield from conn2.select(1)
res = yield from conn2.auth('pass')
self.assertEqual(res, True)
res = yield from conn2.select(1)
self.assertTrue(res)
conn3 = yield from self.create_connection(
('localhost', self.redis_port), password='pass', loop=self.loop)
res = yield from conn3.select(1)
self.assertTrue(res)
res = yield from conn2.execute('CONFIG', 'SET', 'requirepass', '')
self.assertEqual(res, b'OK')
@run_until_complete
def test_decoding(self):
conn = yield from self.create_connection(
('localhost', self.redis_port), encoding='utf-8', loop=self.loop)
self.assertEqual(conn.encoding, 'utf-8',)
res = yield from conn.execute('set', 'key1', 'value')
self.assertEqual(res, 'OK')
res = yield from conn.execute('get', 'key1')
self.assertEqual(res, 'value')
res = yield from conn.execute('set', 'key1', b'bin-value')
self.assertEqual(res, 'OK')
res = yield from conn.execute('get', 'key1')
self.assertEqual(res, 'bin-value')
res = yield from conn.execute('get', 'key1', encoding='ascii')
self.assertEqual(res, 'bin-value')
res = yield from conn.execute('get', 'key1', encoding=None)
self.assertEqual(res, b'bin-value')
with self.assertRaises(UnicodeDecodeError):
yield from conn.execute('set', 'key1', 'значение')
yield from conn.execute('get', 'key1', encoding='ascii')
conn2 = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
res = yield from conn2.execute('get', 'key1', encoding='utf-8')
self.assertEqual(res, 'значение')
@run_until_complete
def test_execute_exceptions(self):
conn = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
with self.assertRaises(TypeError):
yield from conn.execute(None)
with self.assertRaises(TypeError):
yield from conn.execute("ECHO", None)
@run_until_complete
def test_subscribe_unsubscribe(self):
conn = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
self.assertEqual(conn.in_pubsub, 0)
res = yield from conn.execute('subscribe', 'chan:1')
self.assertEqual(res, [b'subscribe', b'chan:1', 1])
self.assertTrue(conn.in_pubsub, 1)
res = yield from conn.execute('unsubscribe', 'chan:1')
self.assertEqual(res, [b'unsubscribe', b'chan:1', 0])
self.assertEqual(conn.in_pubsub, 0)
res = yield from conn.execute('subscribe', 'chan:1', 'chan:2')
self.assertEqual(res, [b'subscribe', b'chan:1', 1])
self.assertEqual(conn.in_pubsub, 2)
res = yield from conn.execute('unsubscribe', 'non-existent')
self.assertEqual(res, [b'unsubscribe', b'non-existent', 2])
self.assertEqual(conn.in_pubsub, 2)
res = yield from conn.execute('unsubscribe', 'chan:1')
self.assertEqual(res, [b'unsubscribe', b'chan:1', 1])
self.assertEqual(conn.in_pubsub, 1)
@run_until_complete
def test_psubscribe_punsubscribe(self):
conn = yield from self.create_connection(
('localhost', 6379), loop=self.loop)
res = yield from conn.execute('psubscribe', 'chan:*')
self.assertEqual(res, [b'psubscribe', b'chan:*', 1])
self.assertEqual(conn.in_pubsub, 1)
@run_until_complete
def test_bad_command_in_pubsub(self):
conn = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
res = yield from conn.execute('subscribe', 'chan:1')
self.assertEqual(res, [b'subscribe', b'chan:1', 1])
msg = "Connection in SUBSCRIBE mode"
with self.assertRaisesRegex(RedisError, msg):
yield from conn.execute('select', 1)
with self.assertRaisesRegex(RedisError, msg):
conn.execute('get')
@run_until_complete
def test_pubsub_messages(self):
sub = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
pub = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
res = yield from sub.execute('subscribe', 'chan:1')
self.assertEqual(res, [b'subscribe', b'chan:1', 1])
self.assertIn(b'chan:1', sub.pubsub_channels)
chan = sub.pubsub_channels[b'chan:1']
self.assertEqual(chan.name, b'chan:1')
self.assertTrue(chan.is_active())
res = yield from pub.execute('publish', 'chan:1', 'Hello!')
self.assertEqual(res, 1)
msg = yield from chan.get()
self.assertEqual(msg, b'Hello!')
res = yield from sub.execute('psubscribe', 'chan:*')
self.assertEqual(res, [b'psubscribe', b'chan:*', 2])
self.assertIn(b'chan:*', sub.pubsub_patterns)
chan2 = sub.pubsub_patterns[b'chan:*']
self.assertEqual(chan2.name, b'chan:*')
self.assertTrue(chan2.is_active())
res = yield from pub.execute('publish', 'chan:1', 'Hello!')
self.assertEqual(res, 2)
msg = yield from chan.get()
self.assertEqual(msg, b'Hello!')
dest_chan, msg = yield from chan2.get()
self.assertEqual(dest_chan, b'chan:1')
self.assertEqual(msg, b'Hello!')
@run_until_complete
def test_multiple_subscribe_unsubscribe(self):
sub = yield from self.create_connection(
('localhost', self.redis_port), loop=self.loop)
res = yield from sub.execute('subscribe', 'chan:1')
self.assertEqual(res, [b'subscribe', b'chan:1', 1])
res = yield from sub.execute('subscribe', b'chan:1')
self.assertEqual(res, [b'subscribe', b'chan:1', 1])
res = yield from sub.execute('unsubscribe', 'chan:1')
self.assertEqual(res, [b'unsubscribe', b'chan:1', 0])
res = yield from sub.execute('unsubscribe', 'chan:1')
self.assertEqual(res, [b'unsubscribe', b'chan:1', 0])
res = yield from sub.execute('psubscribe', 'chan:*')
self.assertEqual(res, [b'psubscribe', b'chan:*', 1])
res = yield from sub.execute('psubscribe', 'chan:*')
self.assertEqual(res, [b'psubscribe', b'chan:*', 1])
res = yield from sub.execute('punsubscribe', 'chan:*')
self.assertEqual(res, [b'punsubscribe', b'chan:*', 0])
res = yield from sub.execute('punsubscribe', 'chan:*')
self.assertEqual(res, [b'punsubscribe', b'chan:*', 0])