git » libjio » master » tree

[master] / tests / behaviour / t_normal.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env python

# Normal tests.

import libjio
from tf import *


def test_n01():
	"open + close"
	def f1(f, jf):
		pass

	n = run_with_tmp(f1)
	assert content(n) == ''
	fsck_verify(n)
	assert content(n) == ''
	cleanup(n)

def test_n02():
	"write + seek + read"
	c = gencontent()

	def f1(f, jf):
		jf.write(c[:len(c) / 2])
		jf.write(c[len(c) / 2:])
		jf.lseek(0, 0)
		assert jf.read(len(c) * 2) == c

	n = run_with_tmp(f1)
	assert content(n) == c
	fsck_verify(n)
	cleanup(n)

def test_n03():
	"pwrite"
	c = gencontent()

	def f1(f, jf):
		jf.pwrite(c, 80)

	n = run_with_tmp(f1)
	assert content(n) == '\0' * 80 + c
	fsck_verify(n)
	cleanup(n)

def test_n04():
	"truncate"
	def f1(f, jf):
		jf.truncate(826)

	n = run_with_tmp(f1)
	assert content(n) == '\0' * 826
	fsck_verify(n)
	cleanup(n)

def test_n05():
	"commit"
	c = gencontent()

	def f1(f, jf):
		t = jf.new_trans()
		t.add_w(c, 80)
		t.commit()

	n = run_with_tmp(f1)
	assert content(n) == '\0' * 80 + c
	fsck_verify(n)
	cleanup(n)

def test_n06():
	"empty, then rollback"
	c = gencontent()

	def f1(f, jf):
		t = jf.new_trans()
		t.add_w(c, 80)
		t.commit()
		t.rollback()

	n = run_with_tmp(f1)

	# XXX: This is weird, because the file was empty at the beginning.
	# However, making it go back to 0 is delicate and the current code
	# doesn't implement it. It probably should.
	assert content(n) == '\0' * 80
	fsck_verify(n)
	cleanup(n)

def test_n07():
	"extending, then rollback"
	c1 = gencontent()
	c2 = gencontent()

	def f1(f, jf):
		jf.write(c1)
		t = jf.new_trans()
		t.add_w(c2, len(c1) - 973)
		t.commit()
		t.rollback()

	n = run_with_tmp(f1)

	assert content(n) == c1
	fsck_verify(n)
	cleanup(n)

def test_n08():
	"multiple overlapping ops"
	c1 = gencontent(9345)
	c2 = gencontent(len(c1))
	c3 = gencontent(len(c1))
	c4 = gencontent(len(c1))
	c5 = gencontent(len(c1))

	def f1(f, jf):
		jf.write(c1)
		t = jf.new_trans()
		t.add_w(c2, len(c1) - 973)
		t.add_w(c3, len(c1) - 1041)
		t.add_w(c4, len(c1) - 666)
		t.add_w(c5, len(c1) - 3000)
		t.commit()

	n = run_with_tmp(f1)
	assert content(n) == c1[:-3000] + c5 + c4[- (- 666 + 3000):]
	fsck_verify(n)
	cleanup(n)

def test_n09():
	"rollback multiple overlapping ops"
	c1 = gencontent(9345)
	c2 = gencontent(len(c1))
	c3 = gencontent(len(c1))
	c4 = gencontent(len(c1))
	c5 = gencontent(len(c1))

	def f1(f, jf):
		jf.write(c1)
		t = jf.new_trans()
		t.add_w(c2, len(c1) - 973)
		t.add_w(c3, len(c1) - 1041)
		t.add_w(c4, len(c1) - 666)
		t.add_w(c5, len(c1) - 3000)
		t.commit()
		t.rollback()

	n = run_with_tmp(f1)

	assert content(n) == c1
	fsck_verify(n)
	cleanup(n)

def test_n10():
	"lingering transactions"
	c = gencontent()

	def f1(f, jf):
		t = jf.new_trans()
		t.add_w(c, 0)
		t.commit()
		del t
		assert content(f.name) == c
		assert os.path.exists(transpath(f.name, 1))
		jf.jsync()
		assert not os.path.exists(transpath(f.name, 1))

	n = run_with_tmp(f1, libjio.J_LINGER)

	assert content(n) == c
	fsck_verify(n)
	cleanup(n)

def test_n11():
	"jfsck a nonexisting file"
	try:
		libjio.jfsck('this file does not exist')
	except IOError:
		return
	raise

def test_n12():
	"jfsck with a nonexisting dir"
	f, jf = bitmp()
	try:
		libjio.jfsck(f.name, 'this directory does not exist')
	except IOError:
		cleanup(f.name)
		return
	raise

def test_n13():
	"move journal to a nonexisting dir"
	import os

	f, jf = bitmp()
	n = f.name
	p = tmppath()

	jf.write('x')
	jf.jmove_journal(p)
	jf.write('y')
	del jf

	assert libjio.jfsck(n, p)['total'] == 0
	os.unlink(n)

def test_n14():
	"autosync"
	f, jf = bitmp(jflags = libjio.J_LINGER)
	n = f.name

	jf.autosync_start(1, 10)
	jf.write('x' * 200)
	jf.write('x' * 200)
	jf.autosync_stop()
	del jf

	fsck_verify(n)
	cleanup(n)

def test_n15():
	"jpread/jpwrite"
	c = gencontent()

	f, jf = bitmp(jflags = libjio.J_LINGER)
	n = f.name

	jf.pwrite(c, 2000)
	assert content(n) == '\0' * 2000 + c
	assert jf.pread(len(c), 2000) == c
	del jf

	fsck_verify(n)
	cleanup(n)

def test_n16():
	"jopen r/o + jtrans_add_w + jtrans_commit"
	c = gencontent()

	# create the file before opening, read-only mode does not create it
	n = tmppath()
	open(n, 'w+')
	f, jf = biopen(n, mode = 'r')

	t = jf.new_trans()

	try:
		t.add_w(c, 80)
	except IOError:
		pass
	else:
		raise AssertionError

	try:
		# note this fails because there are no ops to commit
		t.commit()
	except IOError:
		pass
	else:
		raise AssertionError

	cleanup(n)

def test_n17():
	"move journal to an existing dir"
	import os

	f, jf = bitmp()
	n = f.name
	p = tmppath()
	os.mkdir(p)
	open(p + '/x', 'w')

	jf.write('x')
	jf.jmove_journal(p)
	jf.write('y')
	del jf
	os.unlink(p + '/x')

	assert libjio.jfsck(n, p)['total'] == 0
	os.unlink(n)

def test_n18():
	"jtrans_rollback with norollback"
	c = gencontent()
	f, jf = bitmp(jflags = libjio.J_NOROLLBACK)
	n = f.name

	t = jf.new_trans()
	t.add_w(c, 80)
	t.commit()
	try:
		t.rollback()
	except IOError:
		pass
	else:
		raise AssertionError

	assert content(n) == '\0' * 80 + c
	fsck_verify(n)
	cleanup(n)

def test_n19():
	"jwrite in files opened with O_APPEND"
	c1 = gencontent()
	c2 = gencontent()
	f, jf = bitmp(mode = 'a')
	n = f.name

	jf.write(c1)
	jf.write(c2)

	assert content(n) == c1 + c2
	fsck_verify(n)
	cleanup(n)

def test_n20():
	"jtrans_add_w of 0 length"
	f, jf = bitmp()
	n = f.name

	t = jf.new_trans()

	try:
		t.add_w('', 80)
	except IOError:
		pass
	else:
		raise AssertionError

	del t
	del jf
	fsck_verify(n)
	cleanup(n)

def test_n21():
	"jwritev and jreadv"
	f, jf = bitmp()
	n = f.name

	jf.writev(["hello ", "world"])
	l = [bytearray("......"), bytearray(".....")]
	jf.lseek(0, 0)
	jf.readv(l)

	assert content(n) == "hello world"
	assert l[0] == "hello " and l[1] == "world"
	fsck_verify(n)
	cleanup(n)

def test_n22():
	"jpread/jpwrite ~2mb"
	c = gencontent(2 * 1024 * 1024 + 1465)

	f, jf = bitmp(jflags = libjio.J_LINGER)
	n = f.name

	jf.pwrite(c, 2000)
	assert content(n) == '\0' * 2000 + c
	assert jf.pread(len(c), 2000) == c
	del jf

	fsck_verify(n)
	cleanup(n)

def test_n23():
	"jtrans_add_w + jtrans_add_r"
	f, jf = bitmp()
	n = f.name

	c1 = gencontent(1000)
	c2 = gencontent(2000)
	c3 = gencontent(3000)

	buf1 = bytearray(0 for i in range(30))
	buf2 = bytearray(0 for i in range(100))

	t = jf.new_trans()
	t.add_w(c1, 0)
	t.add_r(buf1, 0)
	t.add_w(c2, len(c1))
	t.add_r(buf2, len(c1) - len(buf2) / 2)
	t.add_w(c3, len(c1) + len(c2))
	t.commit()

	assert content(n) == c1 + c2 + c3
	assert buf1 == c1[:len(buf1)]
	assert buf2 == c1[-(len(buf2) / 2):] + c2[:len(buf2) / 2]

	del t
	del jf
	fsck_verify(n)
	cleanup(n)


def test_n24():
	"many jtrans_add_w + many jtrans_add_r"
	f, jf = bitmp()
	n = f.name

	# just randomly chosen numbers
	len_c1 = 1293
	len_c2 = 529
	len_c3 = 1621
	block_len = len_c1 + len_c2 + len_c3

	t = jf.new_trans()
	bufs = []
	for i in range(50):
		offset = i * block_len
		buf1 = bytearray(0 for _ in range(30))
		buf2 = bytearray(0 for _ in range(100))
		bufs.append((buf1, buf2))

		# We can't use the bytearray directly because add_w requires
		# an immutable object, so we convert it to a string
		c1 = str(bytearray(i for _ in range(len_c1)))
		c2 = str(bytearray(i for _ in range(len_c2)))
		c3 = str(bytearray(i for _ in range(len_c3)))

		t.add_w(c1, offset)
		t.add_r(buf1, offset)
		t.add_w(c2, offset + len(c1))
		t.add_r(buf2, offset + len(c1) - len(buf2) / 2)
		t.add_w(c3, offset + len(c1) + len(c2))

	t.commit()

	cont = content(n)
	for i in range(50):
		offset = i * block_len
		buf1, buf2 = bufs[i]
		c1 = str(bytearray(i for _ in range(len_c1)))
		c2 = str(bytearray(i for _ in range(len_c2)))
		c3 = str(bytearray(i for _ in range(len_c3)))

		assert cont[offset : offset + block_len] == c1 + c2 + c3
		assert buf1 == c1[:len(buf1)]
		assert buf2 == c1[-(len(buf2) / 2):] + c2[:len(buf2) / 2]

	del t
	del jf
	fsck_verify(n)
	cleanup(n)