-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.py
333 lines (268 loc) · 12.5 KB
/
tests.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import unittest
from unittest import mock
import json
import tornado.testing
import tornado.gen
import tornado.httpclient
import tornado.concurrent
import agent
from paxos.learner import Learner
from paxos.models import (
Accept, agents, Learn, Phase, Prepare, Promise,
Promises, Propose, Success
)
class TestPhase(tornado.testing.AsyncTestCase):
def test_to_json(self):
prepare = Prepare(id=1,
key='foo', predicate='incr', argument=1)
phase = Phase(prepare=prepare)
self.assertEqual(phase.to_json(),
{'prepare': {
'id': 1,
'key': 'foo',
'predicate': 'incr',
'argument': 1}})
def test_from_request(self):
request = tornado.httpclient.HTTPRequest(
body=json.dumps({'prepare': {
'id': 2,
'key': 'bar',
'predicate': 'decr',
'argument': -1}}),
method='POST',
headers={'Content-Type': 'application/json'},
url='/testing')
phase = Phase.from_request(request)
self.assertIsInstance(phase.prepare, Prepare)
self.assertEqual(phase.prepare.id, 2)
self.assertEqual(phase.prepare.key, 'bar')
self.assertEqual(phase.prepare.predicate, 'decr')
self.assertEqual(phase.prepare.argument, -1)
@tornado.testing.gen_test
def test_fanout(self):
prepare = Prepare(id=1,
key='foo', predicate='incr', argument=1)
phase = Phase(prepare=prepare)
fut = tornado.concurrent.Future()
response = mock.Mock()
response.body = json.dumps(phase.to_json())
fut.set_result(response)
phase.endpoint = '/testing'
client = mock.Mock()
client.fetch = mock.Mock()
client.fetch.return_value = fut
with mock.patch('tornado.httpclient.AsyncHTTPClient',
return_value=client):
successes = yield phase.fanout(expected=Success)
self.assertEqual(len(successes), len(agents.all()))
@tornado.testing.gen_test
def test_fanout_raises_not_implemented(self):
prepare = Prepare(id=1,
key='foo', predicate='incr', argument=1)
phase = Phase(prepare=prepare)
with self.assertRaises(NotImplementedError):
_ = yield phase.fanout()
@tornado.testing.gen_test
def test_send(self):
prepare = Prepare(id=1,
key='foo', predicate='incr', argument=1)
phase = Phase(prepare=prepare)
fut = tornado.concurrent.Future()
response = mock.Mock()
response.body = json.dumps(phase.to_json())
fut.set_result(response)
phase.endpoint = '/testing'
client = mock.Mock()
client.fetch = mock.Mock()
client.fetch.return_value = fut
with mock.patch('tornado.httpclient.AsyncHTTPClient',
return_value=client):
responses, issued, conflicting = yield phase.send(agents.quorum())
self.assertEqual(len(responses), len(agents.quorum()))
class TestSubclasses(tornado.testing.AsyncTestCase):
@tornado.gen.coroutine
def assert_send_works(self, obj, _):
fut = tornado.concurrent.Future()
response = mock.Mock()
response.body = '{}'
fut.set_result(response)
client = mock.Mock()
client.fetch = mock.Mock()
client.fetch.return_value = fut
with mock.patch('tornado.httpclient.HTTPRequest') as req:
with mock.patch('tornado.httpclient.AsyncHTTPClient',
return_value=client):
agnt = agents.all()[0]
responses, _, _ = yield obj.send([agnt])
self.assertEqual(len(responses), 1)
req.assert_any_call(
url=agnt.url + ':' + str(agnt.port) + obj.endpoint,
method='POST',
headers={'Content-Type': 'application/json'},
body=json.dumps(obj.to_json())
)
@tornado.testing.gen_test
def test_prepare(self):
prepare = Prepare(id=3, key='biz', predicate='set', argument='a')
self.assertEqual(prepare.to_json(), {
'id': 3, 'key': 'biz', 'predicate': 'set', 'argument': 'a'})
target = Prepare._id
prepare = Prepare(key='buzz', predicate='a', argument='b')
self.assertEqual(prepare.to_json(), {
'id': target, 'key': 'buzz', 'predicate': 'a', 'argument': 'b'})
_ = yield self.assert_send_works(prepare, '/prepare')
request = tornado.httpclient.HTTPRequest(
body=json.dumps(prepare.to_json()),
method='POST',
headers={'Content-Type': 'application/json'},
url='/prepare')
target = Prepare.from_request(request)
self.assertEqual(target.to_json(), prepare.to_json())
def test_promise(self):
promise = Promise()
self.assertEqual(promise.prepare, None)
prepare = Prepare(id=3, key='biz', predicate='set', argument='a')
promise = Promise(prepare=prepare)
self.assertEqual(promise.prepare, prepare)
self.assertEqual(promise.to_json(), {
'prepare': prepare.to_json()
})
def test_propose(self):
prepare = Prepare(id=3, key='biz', predicate='set', argument='a')
propose = Propose(prepare=prepare)
self.assert_send_works(propose, '/propose')
request = tornado.httpclient.HTTPRequest(
body=json.dumps(propose.to_json()),
method='POST',
headers={'Content-Type': 'application/json'},
url='/propose')
target = Propose.from_request(request)
self.assertEqual(propose.to_json(), target.to_json())
def test_accept(self):
prepare = Prepare(id=3, key='biz', predicate='set', argument='a')
accept = Accept(prepare=prepare)
response = mock.Mock()
response.body = json.dumps(accept.to_json())
target = Accept.from_response(response)
self.assertEqual(accept.to_json(), target.to_json())
def test_learn(self):
prepare = Prepare(id=3, key='biz', predicate='set', argument='a')
learn = Learn(prepare=prepare)
request = tornado.httpclient.HTTPRequest(
body=json.dumps(learn.to_json()),
method='POST',
headers={'Content-Type': 'application/json'},
url='/learn')
self.assertEqual(learn.to_json(), Learn.from_request(request).to_json())
def test_success(self):
prepare = Prepare(id=3, key='biz', predicate='set', argument='a')
success = Success(prepare=prepare)
self.assertEqual(success.to_json(), {'status': 'SUCCESS',
'prepare': prepare.to_json()})
class TestContainers(tornado.testing.AsyncTestCase):
def test_promises(self):
prepare1 = Prepare(id=1, key='biz', predicate='pa', argument='a')
prepare2 = Prepare(id=2, key='biz', predicate='pb', argument='b')
prepare3 = Prepare(id=3, key='biz', predicate='pc', argument='c')
prepare4 = Prepare(id=4, key='baz', predicate='pd', argument='d')
promise1 = Promise(prepare=prepare1)
promise2 = Promise(prepare=prepare2)
promise3 = Promise(prepare=prepare3)
promise4 = Promise(prepare=prepare4)
promises = Promises([promise1, promise2, promise3, promise4])
self.assertEqual(promises.highest_numbered().to_json(),
promise4.to_json())
self.assertEqual(promises.highest_numbered(key='biz').to_json(),
promise3.to_json())
class Base(tornado.testing.AsyncHTTPTestCase):
def get_app(self):
return agent.get_app()
def post(self, url, body):
return self.fetch(url,
method='POST',
body=json.dumps(body),
headers={'Content-Type': 'application/json'})
@classmethod
def get_prepare(cls):
return Prepare(id=1, key='foo', predicate='set', argument='a')
def setUp(self):
Promises.current.clear()
Learner.completed_rounds.clear()
super(Base, self).setUp()
class TestProposer(Base):
def test_allows_non_conflicting_writes(self):
prepare = Prepare(id=0, key='foo', predicate='set', argument='a')
promise = Promise()
prepare_success = mock.Mock()
prepare_success.code = 200
prepare_success.body = json.dumps(promise.to_json())
fut = tornado.concurrent.Future()
fut.set_result(tuple([[prepare_success, prepare_success], [prepare_success, prepare_success], []]))
propose_success = mock.Mock()
propose_success.code = 200
propose_success.body = json.dumps(Promise(prepare=prepare).to_json())
propose_fut = tornado.concurrent.Future()
propose_fut.set_result(tuple([[propose_success, propose_success], [propose_success, propose_success], []]))
learn_success = mock.Mock()
learn_success.code = 200
learn_success.body = ''
learn_fut = tornado.concurrent.Future()
learn_fut.set_result(tuple([[learn_success, learn_success], [learn_success, learn_success], []]))
with mock.patch('paxos.models.Prepare.send', return_value=fut):
with mock.patch('paxos.models.Propose.send', return_value=propose_fut):
with mock.patch('paxos.models.Learn.fanout', return_value=learn_fut):
response = self.post('/write', body={
'key': 'foo',
'predicate': 'set',
'argument': 'a'})
self.assertEqual(response.code, 200)
class TestPrepareAcceptor(Base):
def test_rejects_when_there_is_a_higher_numbered_promise_in_progress(self):
lower_prepare = Prepare(id=0, key='foo', predicate='set', argument='a')
higher_prepare = Prepare(id=1, key='foo', predicate='set', argument='b')
success = self.post('/prepare', higher_prepare.to_json())
self.assertEqual(success.code, 200)
self.assertEqual(Promise.from_response(success).to_json(), {'prepare': None})
self.assertEqual(Promises.current.highest_numbered().to_json(),
{'prepare': higher_prepare.to_json()})
failure = self.post('/prepare', lower_prepare.to_json())
self.assertEqual(failure.code, 400)
target = Promise.from_response(failure)
self.assertEqual(target.to_json(), {'prepare': higher_prepare.to_json()})
def test_returns_lower_numbered_in_progress_promises(self):
lower_prepare = Prepare(id=0, key='foo', predicate='set', argument='a')
higher_prepare = Prepare(id=1, key='foo', predicate='set', argument='b')
success = self.post('/prepare', lower_prepare.to_json())
self.assertEqual(success.code, 200)
self.assertEqual(Promise.from_response(success).to_json(), {'prepare': None})
self.assertEqual(Promises.current.highest_numbered().to_json(),
{'prepare': lower_prepare.to_json()})
failure = self.post('/prepare', higher_prepare.to_json())
self.assertEqual(failure.code, 200)
target = Promise.from_response(failure)
self.assertEqual(target.to_json(), {'prepare': lower_prepare.to_json()})
class TestProposeAcceptor(Base):
def test_propose_acceptor_removes_promise(self):
promise = Promise(prepare=self.get_prepare())
Promises.current.add(promise)
self.assertEqual(Promises.current.highest_numbered().to_json(),
promise.to_json())
propose = Propose(prepare=self.get_prepare())
response = self.post('/propose', propose.to_json())
self.assertEqual(response.code, 200)
self.assertEqual(
Accept.from_response(response).to_json(),
{'prepare': self.get_prepare().to_json()})
self.assertIsNone(Promises.current.highest_numbered())
class TestLearner(Base):
def test_learner_learns(self):
learn = Learn(prepare=self.get_prepare())
response = self.post('/learn', learn.to_json())
self.assertEqual(response.code, 200)
self.assertEqual(
Success.from_response(response).to_json(),
{'status': 'SUCCESS', 'prepare': self.get_prepare().to_json()})
self.assertEqual(Learner.completed_rounds.highest_numbered().prepare.to_json(),
self.get_prepare().to_json())
if __name__ == '__main__':
unittest.main()