forked from Kinetic/kinetic-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_batch.py
214 lines (175 loc) · 7.27 KB
/
test_batch.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
# Copyright 2013-2015 Seagate Technology LLC.
#
# This Source Code Form is subject to the terms of the Mozilla
# Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at
# https://mozilla.org/MP:/2.0/.
#
# This program is distributed in the hope that it will be useful,
# but is provided AS-IS, WITHOUT ANY WARRANTY; including without
# the implied warranty of MERCHANTABILITY, NON-INFRINGEMENT or
# FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public
# License for more details.
#
# See www.openkinetic.org for more project information
#
#@author: Paul Dardeau
import unittest
from kinetic import Client
from kinetic import batch
from kinetic import common
from base import BaseTestCase
class BatchTestCase(BaseTestCase):
def setUp(self):
super(BatchTestCase, self).setUp()
self.client = Client(self.host, self.port)
self.client.connect()
self._create_new_batch()
def _create_new_batch(self):
self.batch = self.client.begin_batch()
if self.batch is None:
raise common.KineticException("unable to create batch")
def test_batch_initial_state(self):
is_completed = self.batch.is_completed()
op_count = len(self.batch)
self.batch.abort()
self.assertFalse(is_completed)
self.assertEquals(op_count, 0)
def test_batch_operation_count(self):
key1 = self.buildKey('test_batch_operation_count_1')
key2 = self.buildKey('test_batch_operation_count_2')
key3 = self.buildKey('test_batch_operation_count_3')
self.batch.put(key1, '')
self.assertEquals(len(self.batch), 1)
self.batch.put(key2, '')
self.assertEquals(len(self.batch), 2)
self.batch.delete(key3)
self.assertEquals(len(self.batch), 3)
self.batch.abort()
def test_batch_commit_is_completed(self):
key1 = self.buildKey('test_batch_commit_is_completed_1')
key2 = self.buildKey('test_batch_commit_is_completed_2')
self.assertFalse(self.batch.is_completed())
self.batch.put(key1, '')
self.batch.delete(key2)
self.assertFalse(self.batch.is_completed())
self.batch.commit()
self.assertTrue(self.batch.is_completed())
def test_batch_abort_is_completed(self):
key1 = self.buildKey('test_batch_abort_is_completed_1')
key2 = self.buildKey('test_batch_abort_is_completed_2')
self.assertFalse(self.batch.is_completed())
self.batch.put(key1, '')
self.batch.delete(key2)
self.assertFalse(self.batch.is_completed())
self.batch.abort()
self.assertTrue(self.batch.is_completed())
def test_empty_batch_abort(self):
# abort with no operations in batch
self.assertRaises(common.BatchAbortedException, self.batch.abort())
def test_batch_abort(self):
key = self.buildKey('key_should_not_exist')
self.batch.put(key, '')
self.batch.abort()
self.assertEqual(self.client.get(key), None)
def test_empty_batch_commit(self):
# commit with no operations in batch
self.assertRaises(common.BatchAbortedException, self.batch.commit())
def test_batch_commit(self):
key = self.buildKey('key_should_exist')
self.batch.put(key, '')
self.batch.commit()
self.assertIsNotNone(self.client.get(key))
def test_batch_delete_commit(self):
# put an entry
key = self.buildKey('test_batch_delete_commit')
self.client.put(key, '')
self.batch.delete(key)
self.batch.commit()
self.assertEqual(self.client.get(key), None)
def test_batch_delete_abort(self):
# put an entry
key = self.buildKey('test_batch_delete_abort')
self.client.put(key, '')
self.batch.delete(key)
self.batch.abort()
self.assertIsNotNone(self.client.get(key))
def test_batch_put_commit(self):
key = self.buildKey('test_batch_put_commit')
self.batch.put(key, '')
self.batch.commit()
self.assertIsNotNone(self.client.get(key))
def test_batch_put_abort(self):
key = self.buildKey('test_batch_put_abort')
self.batch.put(key, '')
self.batch.abort()
self.assertEqual(self.client.get(key), None)
def test_batch_multiple_put_commit(self):
key1 = self.buildKey('test_batch_multiple_put_commit_1')
key2 = self.buildKey('test_batch_multiple_put_commit_2')
self.batch.put(key1, '')
self.batch.put(key2, '')
self.batch.commit()
self.assertIsNotNone(self.client.get(key1))
self.assertIsNotNone(self.client.get(key2))
def test_batch_multiple_put_abort(self):
key1 = self.buildKey('test_batch_multiple_put_abort_1')
key2 = self.buildKey('test_batch_multiple_put_abort_2')
self.batch.put(key1, '')
self.batch.put(key2, '')
self.batch.abort()
self.assertEqual(self.client.get(key1), None)
self.assertEqual(self.client.get(key2), None)
def test_batch_multiple_delete_commit(self):
key1 = self.buildKey('test_batch_multiple_delete_commit_1')
key2 = self.buildKey('test_batch_multiple_delete_commit_2')
self.client.put(key1, '')
self.client.put(key2, '')
self.batch.delete(key1)
self.batch.delete(key2)
self.batch.commit()
self.assertEqual(self.client.get(key1), None)
self.assertEqual(self.client.get(key2), None)
def test_batch_multiple_delete_abort(self):
key1 = self.buildKey('test_batch_multiple_delete_abort_1')
key2 = self.buildKey('test_batch_multiple_delete_abort_2')
self.client.put(key1, '')
self.client.put(key2, '')
self.batch.delete(key1)
self.batch.delete(key2)
self.batch.abort()
self.assertIsNotNone(self.client.get(key1))
self.assertIsNotNone(self.client.get(key2))
def test_batch_mixed_commit(self):
key1 = self.buildKey('test_batch_mixed_commit_1')
key2 = self.buildKey('test_batch_mixed_commit_2')
self.client.put(key1, '')
self.batch.delete(key1)
self.batch.put(key2, '')
self.batch.commit()
self.assertEqual(self.client.get(key1), None)
self.assertIsNotNone(self.client.get(key2))
def test_batch_mixed_abort(self):
key1 = self.buildKey('test_batch_mixed_abort_1')
key2 = self.buildKey('test_batch_mixed_abort_2')
self.client.put(key1, '')
self.batch.delete(key1)
self.batch.put(key2, '')
self.batch.abort()
self.assertIsNotNone(self.client.get(key1))
self.assertEqual(self.client.get(key2), None)
def test_batch_reuse_after_commit(self):
key1 = self.buildKey('test_batch_reuse_after_commit_1')
key2 = self.buildKey('test_batch_reuse_after_commit_2')
self.batch.put(key1, '')
self.batch.commit()
args = (key2, '')
self.assertRaises(common.BatchCompletedException, self.batch.put, *args)
def test_batch_reuse_after_abort(self):
key = self.buildKey('test_batch_reuse_after_abort')
self.batch.put(key, '')
self.batch.abort()
args = (key)
self.assertRaises(common.BatchCompletedException, self.batch.delete, *args)
if __name__ == '__main__':
unittest.main()