forked from tarantool/expirationd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
executable file
·793 lines (701 loc) · 21.8 KB
/
test.lua
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#!/usr/bin/env tarantool
local clock = require('clock')
local fun = require('fun')
local log = require('log')
local tap = require('tap')
local yaml = require('yaml')
local fiber = require('fiber')
local strict = require('strict')
local expirationd = require('expirationd')
local test = tap.test("expirationd")
strict.on()
-- ========================================================================= --
-- local support functions --
-- ========================================================================= --
-- Strip pcall()'s true or re-raise catched error.
local function wait_cond_finish(status, ...)
if not status then
error((...), 2)
end
return ...
end
-- Block until the condition function returns a positive value
-- (anything except `nil` and `false`) or until the timeout
-- exceeds. Return the result of the last invocation of the
-- condition function (it is `false` or `nil` in case of exiting
-- by the timeout).
--
-- If the condition function raises a Lua error, wait_cond()
-- continues retrying. If the latest attempt raises an error (and
-- we hit a timeout), the error will be re-raised.
local function wait_cond(cond, timeout, delay)
assert(type(cond) == 'function')
local timeout = timeout or 60
local delay = delay or 0.001
local start_time = clock.monotonic()
local res = {pcall(cond)}
while not res[1] or not res[2] do
local work_time = clock.monotonic() - start_time
if work_time > timeout then
return wait_cond_finish(res[1], res[2])
end
fiber.sleep(delay)
res = {pcall(cond)}
end
return wait_cond_finish(res[1], res[2])
end
-- get field
local function get_field(tuple, field_no)
if tuple == nil then
return nil
end
if #tuple < field_no then
return nil
end
return tuple[field_no]
end
local function construct_key(space_id, tuple)
return fun.map(
function(x) return tuple[x.fieldno] end,
box.space[space_id].index[0].parts
):totable()
end
local function truncate(space_id)
local sp = box.space[space_id]
if sp.engine == 'memtx' then
return sp:truncate()
else
fun.iter(box.space[space_id]:select()):each(function(tuple)
box.space[space_id]:delete(construct_key(space_id, tuple))
end)
end
end
-- ========================================================================= --
-- Expiration handlers examples
-- ========================================================================= --
local function len(arg)
local len = 0
for k, v in pairs(arg) do
len = len + 1
end
return len
end
-- check tuple's expiration by timestamp (stored in last field)
local function check_tuple_expire_by_timestamp(args, tuple)
local tuple_expire_time = get_field(tuple, args.field_no)
if type(tuple_expire_time) ~= 'number' then
return true
end
local current_time = fiber.time()
return current_time >= tuple_expire_time
end
-- put expired tuple in archive
local function put_tuple_to_archive(space_id, args, tuple)
-- delete expired tuple
box.space[space_id]:delete{tuple[1]}
local email = get_field(tuple, 2)
if args.archive_space_id ~= nil and email ~= nil then
box.space[args.archive_space_id]:replace{email, fiber.time()}
end
end
local nonexistentfunction = nil
local function check_tuple_expire_by_timestamp_error(args, tuple)
nonexistentfunction()
end
local function put_tuple_to_archive_error(space_id, args, tuple)
nonexistentfunction()
end
-- ========================================================================= --
-- Expiration module test functions
-- ========================================================================= --
-- Warning: for these test functions to work, you need
-- a space with a numeric primary key defined on field[0]
-- generate email string
local function get_email(uid)
local email = "test_" .. uid .. "@tarantool.org"
return email
end
-- insert entry to space
local function add_entry(space_id, uid, email, expiration_time)
box.space[space_id]:replace{uid, email, expiration_time}
end
-- put test tuples
local function put_test_tuples(space_id, total)
local time = math.floor(fiber.time())
for i = 0, total do
add_entry(space_id, i, get_email(i), time + i)
end
-- tuple w/o expiration date
local uid = total + 1
add_entry(space_id, uid, get_email(uid), "")
-- tuple w/ invalid expiration date
uid = total + 2
add_entry(space_id, uid, get_email(uid), "some string in exp field")
end
-- print test tuples
local function print_test_tuples(space_id)
for state, tuple in box.space[space_id].index[0]:pairs(nil, {iterator = box.index.ALL}) do
log.info(tostring(tuple))
end
end
local function prefix_space_id(space_id)
if type(space_id) == 'number' then
return '#'..space_id
end
return string.format('%q', space_id)
end
-- Configure box, create spaces and indexes
local function init_box()
box.cfg{
log = 'tarantool.log'
}
local index_type = arg[1] or os.getenv('INDEX_TYPE') or 'TREE'
local space_type = arg[2] or os.getenv('SPACE_TYPE') or 'memtx'
log.info('Running tests for %s index and engine %s', index_type, space_type)
local a = box.schema.create_space('origin', {
engine = space_type,
if_not_exists = true
})
a:create_index('first', {
type = index_type,
parts = {1, 'number'},
if_not_exists = true
})
truncate(a.id)
local b = box.schema.create_space('cemetery', {
engine = space_type,
if_not_exists = true
})
b:create_index('first', {
type = index_type,
parts = {1, 'string'},
if_not_exists = true
})
truncate(b.id)
local c = box.schema.create_space('exp_test', {
engine = space_type,
if_not_exists = true
})
c:create_index('first', {
type = index_type,
parts = {1, 'number'},
if_not_exists = true
})
truncate(c.id)
local d = box.schema.create_space('drop_test', {
engine = space_type,
if_not_exists = true
})
d:create_index('first', {
type = index_type,
parts = {1, 'number'},
if_not_exists = true
})
truncate(d.id)
local e = box.schema.create_space('restart_test', {
engine = space_type,
if_not_exists = true
})
e:create_index('first', {
type = index_type,
parts = {1, 'number'},
if_not_exists = true
})
truncate(e.id)
local f = box.schema.create_space('complex_test', {
engine = space_type,
if_not_exists = true
})
f:create_index('first', {
type = index_type,
parts = {2, 'number', 1, 'number'},
if_not_exists = true
})
truncate(f.id)
local g = box.schema.create_space('delays_test', {
engine = space_type,
if_not_exists = true
})
g:create_index('first', {
type = index_type,
parts = {1, 'number'},
if_not_exists = true
})
truncate(g.id)
local h = box.schema.create_space('error_callback_test', {
engine = space_type,
if_not_exists = true
})
h:create_index('first', {
type = index_type,
parts = {1, 'number'},
if_not_exists = true
})
truncate(h.id)
end
local space_id = 'origin'
local archive_space_id = 'cemetery'
init_box()
-- ========================================================================= --
-- TAP TESTS:
-- 1. simple archive test.
-- 2. errors test,
-- 3. not expire test,
-- 4. kill zombie test
-- 5. multiple expires test
-- 6. default drop function test
-- 7. restart test
-- 8. complex key test
-- 9. delays and scan callbacks test
-- 10. error callback test
-- ========================================================================= --
test:plan(10)
test:test('simple expires test', function(test)
test:plan(4)
-- put test tuples
log.info("-------------- put ------------")
log.info("put to space " .. prefix_space_id(space_id))
put_test_tuples(space_id, 10)
-- print before
log.info("------------- print -----------")
log.info("before print space " .. prefix_space_id(space_id), "\n")
print_test_tuples(space_id)
log.info("before print archive space " .. prefix_space_id(archive_space_id), "\n")
print_test_tuples(archive_space_id)
log.info("-------------- run ------------")
expirationd.start(
"test",
space_id,
check_tuple_expire_by_timestamp,
{
process_expired_tuple = put_tuple_to_archive,
args = {
field_no = 3,
archive_space_id = archive_space_id
},
}
)
-- wait expiration
local start_time = fiber.time()
log.info("------------- wait ------------")
log.info("before time = " .. os.date('%X', start_time))
fiber.sleep(5)
local end_time = fiber.time()
log.info("after time = " .. os.date('%X', end_time))
-- print after
log.info("------------- print -----------")
log.info("After print space " .. prefix_space_id(space_id), "\n")
print_test_tuples(space_id)
log.info("after print archive space " .. prefix_space_id(archive_space_id), "\n")
print_test_tuples(archive_space_id)
expirationd.tasks()
local task = expirationd.task("test")
test:is(task.start_time, start_time, 'checking start time')
test:is(task.name, "test", 'checking task name')
local restarts = 1
test:is(task.restarts, restarts, 'checking restart count')
local res = wait_cond(
function()
local task = expirationd.task("test")
local cnt = task.expired_tuples_count
return cnt == 7
end
)
test:is(res, true, 'Test task executed and moved to archive')
expirationd.kill("test")
end)
test:test("execution error test", function (test)
test:plan(2)
expirationd.start(
"test",
space_id,
check_tuple_expire_by_timestamp_error,
{
process_expired_tuple = put_tuple_to_archive,
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
}
)
test:is(expirationd.task("test").restarts, 1, 'checking restart count')
expirationd.start("test",
space_id,
check_tuple_expire_by_timestamp,
{
process_expired_tuple = put_tuple_to_archive_error,
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
}
)
local task = expirationd.task("test")
test:is(task.restarts, 1, 'Error task executed')
expirationd.kill("test")
end)
test:test("not expired task", function(test)
test:plan(2)
truncate(space_id)
local tuples_count = 5
local time = fiber.time()
for i = 1, tuples_count do
add_entry(space_id, i, get_email(i), time + 2)
end
expirationd.start(
"test",
space_id,
check_tuple_expire_by_timestamp,
{
process_expired_tuple = put_tuple_to_archive,
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
}
)
local task = expirationd.task("test")
-- after run tuples is not expired
test:is(task.expired_tuples_count, 0, 'checking expired tuples empty')
-- wait 3 seconds and check: all tuples must be expired
fiber.sleep(3)
test:is(task.expired_tuples_count, tuples_count, 'checking expired tuples count')
expirationd.kill("test")
end)
test:test("zombie task kill", function(test)
test:plan(4)
local tuples_count = 10
local time = math.floor(fiber.time())
for i = 0, tuples_count do
add_entry(space_id, i, get_email(i), time + i * 5)
end
-- first run
expirationd.start(
"test",
space_id,
check_tuple_expire_by_timestamp,
{
process_expired_tuple = put_tuple_to_archive,
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
}
)
local fiber_obj = expirationd.task("test").guardian_fiber
test:is(fiber_obj:status(), 'suspended', 'checking status of fiber')
-- run again and check - it must kill first task
expirationd.start(
"test",
space_id,
check_tuple_expire_by_timestamp,
{
process_expired_tuple = put_tuple_to_archive,
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
}
)
local task = expirationd.task("test")
test:is(task.restarts, 1, 'checking restart count')
-- check is first fiber killed
test:is(task.guardian_fiber:status(), "suspended", 'checking status of fiber')
test:is(fiber_obj:status(), 'dead', "Zombie task was killed and restarted")
expirationd.kill("test")
end)
test:test("multiple expires test", function(test)
test:plan(2)
local tuples_count = 10
local time = fiber.time()
local space_name = 'exp_test'
local expire_delta = 0.5
for i = 1, tuples_count do
box.space[space_name]:delete{i}
if i <= tuples_count / 2 then
time = time + expire_delta
end
box.space[space_name]:insert{i, get_email(i), time}
end
expirationd.start(
"test",
space_name,
check_tuple_expire_by_timestamp,
{
process_expired_tuple = put_tuple_to_archive,
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
tuples_per_iteration = 5,
full_scan_time = 1,
}
)
-- test first expire part
local res = wait_cond(
function()
local task = expirationd.task("test")
local cnt = task.expired_tuples_count
return cnt < tuples_count and cnt > 0
end,
2 + expire_delta
)
test:ok(res, true, 'First part expires done')
-- test second expire part
res = wait_cond(
function()
local task = expirationd.task("test")
local cnt = task.expired_tuples_count
return cnt == tuples_count
end,
4
)
test:ok(res, true, 'Multiple expires done')
expirationd.kill("test")
end)
test:test("default drop function test", function(test)
test:plan(2)
local tuples_count = 10
local space_name = 'drop_test'
local space = box.space[space_name]
for i = 1, tuples_count do
space:insert{i, 'test_data', fiber.time()}
end
test:is(space:count{}, tuples_count, 'tuples are in space')
expirationd.start(
"test",
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
tuples_per_iteration = 10,
full_scan_time = 1,
}
)
local task = expirationd.task("test")
local res = wait_cond(
function()
return space:count{} == 0
end,
2
)
test:is(res, true, 'all tuples are expired with default function')
expirationd.kill("test")
end)
test:test("restart test", function(test)
test:plan(5)
local tuples_count = 10
local space_name = 'restart_test'
local space = box.space[space_name]
local task1 = expirationd.start(
"test1",
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 3,
archive_space_id = archive_space_id
},
tuples_per_iteration = 10,
full_scan_time = 1,
}
)
local task2 = expirationd.start(
"test2",
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 3,
archive_space_id = archive_space_id
},
tuples_per_iteration = 10,
full_scan_time = 1,
}
)
local task3 = expirationd.start(
"test3",
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
tuples_per_iteration = 10,
full_scan_time = 1,
}
)
local task4 = expirationd.start(
"test4",
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 3,
archive_space_id = archive_space_id,
},
tuples_per_iteration = 10,
full_scan_time = 1,
}
)
local fiber_cnt = len(fiber.info())
local old_expd = expirationd
local chan = fiber.channel(1)
local fiber_update = fiber.create(function()
expirationd.update()
chan:put(1)
end)
local ok, err = pcall(function() expirationd.start() end)
test:like(err, ".*Wait until update is done.*", "error while reloading")
chan:get()
for i = 1, tuples_count do
space:insert{i, 'test_data', fiber.time() + 1}
end
expirationd = require('expirationd')
test:isnt(tostring(old_expd):match('0x.*'),
tostring(expirationd):match('0x.*'),
'new expirationd table')
test:is(space:count{}, tuples_count, 'tuples are in space')
fiber.sleep(4)
test:is(space:count{}, 0, 'all tuples are expired')
task1:statistics()
test:is(fiber_cnt, len(fiber.info()), "check for absence of ghost fibers")
expirationd.kill("test1")
expirationd.kill("test2")
expirationd.kill("test3")
expirationd.kill("test4")
end)
test:test("complex key test", function(test)
test:plan(2)
local tuples_count = 10
local space_name = 'complex_test'
local space = box.space[space_name]
for i = 1, tuples_count do
space:insert{i, i*i + 100, fiber.time() + 1}
end
expirationd.start(
"test",
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 3,
archive_space_id = archive_space_id
},
tuples_per_iteration = 10,
full_scan_time = 1,
}
)
test:is(space:count{}, tuples_count, 'tuples are in space')
fiber.sleep(3.1)
test:is(space:count{}, 0, 'all tuples are expired with default function')
expirationd.kill("test")
end)
test:test('delays and scan callbacks test', function(test)
test:plan(4)
-- Prepare the space.
local tuples_count = 10
local time = fiber.time()
local space_name = 'delays_test'
local expire_delta = 10
for i = 1, tuples_count do
box.space[space_name]:insert{i, time + expire_delta}
end
-- To check all delays (iteration and full scan), two full scan
-- iterations will be performed.
local first_iteration_done = false
local task_name = 'delays_task'
local cond = fiber.cond()
local start_time = 0
local complete_time = 0
local iteration_delay = 1
local full_scan_delay = 2
expirationd.start(
task_name,
space_name,
check_tuple_expire_by_timestamp,
{
args = {
field_no = 2
},
tuples_per_iteration = 10,
iteration_delay = iteration_delay,
full_scan_delay = full_scan_delay,
on_full_scan_start = function(task)
start_time = fiber.time()
if first_iteration_done then
-- Check the full scan delay with an accuracy
-- of 0.1 seconds.
test:ok(math.abs(start_time - complete_time -
full_scan_delay) < 0.1, 'test full scan delay')
end
end,
on_full_scan_success = function(task)
-- Must be called twice.
test:ok(true, 'test success callback invoke')
end,
on_full_scan_complete = function(task)
complete_time = fiber.time()
if first_iteration_done then
cond:signal()
else
-- Check the accuracy of iteration delay,
-- it should be not beyond 1 second.
test:ok(math.abs(complete_time - start_time -
iteration_delay) < 1, 'test iteration delay')
first_iteration_done = true
end
end
}
)
cond:wait()
expirationd.kill(task_name)
end)
test:test('error callback test', function(test)
test:plan(2)
-- Prepare the space.
local tuples_count = 1
local time = fiber.time()
local space_name = 'error_callback_test'
local expire_delta = 10
for i = 1, tuples_count do
box.space[space_name]:insert{i, time + expire_delta}
end
local task_name = 'error_callback_task'
local cond = fiber.cond()
local error_cb_called = false
local complete_cb_called = false
local err_msg = 'The error is occured'
expirationd.start(
task_name,
space_name,
function(args, tuple)
error(err_msg)
end,
{
args = {
field_no = 2
},
-- The callbacks can be called multiple times because guardian_loop
-- will restart the task.
on_full_scan_error = function(task, err)
if err:find(err_msg) then
error_cb_called = true
end
end,
on_full_scan_complete = function(task)
complete_cb_called = true
cond:signal()
end
}
)
cond:wait()
expirationd.kill(task_name)
test:ok(error_cb_called, 'the "error" callback has been invoked')
test:ok(complete_cb_called, 'the "complete" callback has been invoked')
end)
os.exit(test:check() and 0 or 1)