-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud_aux.lua
More file actions
99 lines (89 loc) · 2.21 KB
/
crud_aux.lua
File metadata and controls
99 lines (89 loc) · 2.21 KB
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
local fiber = require('fiber')
local crud = require('crud')
local crud_methods_to_patch = {
'insert',
'select',
'get',
'delete',
'replace',
'update',
'upsert',
'insert_many',
'insert_object_many',
'replace_many',
'replace_object_many',
'upsert_many',
'upsert_object_many',
'truncate',
'count',
'len',
'min',
'max'
}
local old_methods = {}
local function wrap_api(wrapper)
for _, name in ipairs(crud_methods_to_patch) do
local real_method
if old_methods[name] ~= nil then
real_method = old_methods[name]
else
real_method = crud[name]
old_methods[name] = real_method
end
crud[name] = wrapper(name, real_method)
end
end
local function unwrap_api()
for _, name in ipairs(crud_methods_to_patch) do
if old_methods[name] ~= nil then
crud[name] = old_methods[name]
end
end
end
local function break_api()
wrap_api(function(name, method)
return function(...)
local args = { ... }
local counter_name = ('crud_%s_calls'):format(name)
local counter = rawget(_G, counter_name)
if counter == nil then
counter = 0
end
counter = counter + 1
rawset(_G, counter_name, counter)
if counter % 3 ~= 0 then
error('some lua error ' .. counter)
end
return method(...)
end
end)
end
local function slow_api()
wrap_api(function(name, method)
return function(...)
fiber.sleep(1.5)
return method(...)
end
end)
end
local function init_module()
wrap_api(function(name, method)
return function(...)
local args = { ... }
rawset(_G, ('crud_%s_opts'):format(name), args[#args])
return method(...)
end
end)
end
local function reset_counters()
for _, name in ipairs(crud_methods_to_patch) do
rawset(_G, ('crud_%s_calls'):format(name), 0)
end
end
return {
init_module = init_module,
break_api = break_api,
unwrap_api = unwrap_api,
reset_counters = reset_counters,
slow_api = slow_api
}