-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaky.lua
215 lines (153 loc) · 5.43 KB
/
flaky.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
local tx = require 'pl.tablex'
local RETRY_DEFAULT = 5
local TAG = 'flaky'
-- like [#flaky (3/5) Some desc maybe]
local FMT = '[#{tag} ({attempt}/{attempts}) {name}]'
local WAIT = 0
local NOOP = function() end
local function relay(busted)
local enabled = false
-- noop
local track = function() end
local function handler(cb)
return function(...)
-- noop
if not enabled then return nil, true end
return cb(track, ...)
end
end
return {
off = function() enabled = false end,
on = function(cb)
enabled = true
track = cb
end,
attach = function(topic, cb)
busted.subscribe(topic, handler(cb), { priority = 1 })
end,
}
end
local function fmt(template, words)
local w = template:gsub('(%b{})', function(m)
return words[m:sub(2, -2)] or m
end)
return w:gsub('%s-%b{}%s-', '')
end
return function(busted, helper, options)
local block = require 'busted.block'(busted)
local cli = require 'cliargs'
cli:set_name('flaky')
cli:option('--attempts=NUM', 'number of attempts for flaky blocks', RETRY_DEFAULT)
cli:option('--tag=TAG', 'tag for marking flaky blocks', TAG)
cli:option('--format=FORMAT', 'format string for flaky blocks', FMT)
cli:option('--wait=TIME', 'seconds to wait between retries', WAIT)
local cli_args = cli:parse(options.arguments)
local tag = cli_args.tag
local tag_fmt = cli_args.format
local max_attempts = tonumber(cli_args.attempts)
local wait = tonumber(cli_args.wait)
local hammer = relay(busted)
-- Now everything looks like a nail
hammer.attach({'error'}, function(track, ...)
track(busted.status('error'))
-- do not propagate errors
return nil, false
end)
hammer.attach({'failure'}, function(track, ...)
track(busted.status('failure'))
-- do not propagate errors
return nil, false
end)
hammer.attach({'test', 'end'}, function(track, element, parent, status)
track(busted.status(status))
-- do not propagate errors
return nil, not(status == 'failure' or status == 'error')
end)
local function flaky(element)
local ctx = busted.context.get()
local parent = busted.context.parent(element)
busted.safe_publish(tag, { tag, 'start' }, element, parent)
local name = element.name
-- accept element level customization
local attributes = element.attributes
local wait = attributes and attributes.wait or wait
local tag_fmt = attributes and attributes.fmt or tag_fmt
local max_attempts = attributes and attributes.attempts or max_attempts
local retry_callback = attributes and attributes.callback or NOOP
local status
hammer.on(function(s) status:update(s) end)
local attempts = 1
while true do
-- last attempt is chatty
if attempts == max_attempts then hammer.off() end
status = busted.status('success')
element.name = fmt(tag_fmt, {
tag = element.descriptor,
name = name,
attempt = attempts,
attempts = max_attempts
})
block.execute("flaky", element)
-- bye
if status:success() or attempts == max_attempts then break end
attempts = attempts + 1
-- nuke childrens for next run
tx.clear(busted.context.children(element))
retry_callback(element, status, attempts, ctx)
busted.sleep(wait)
end
hammer.off()
busted.safe_publish(tag, { tag, 'end' }, element, parent, status)
end
local function patch_publisher(descriptor)
local _publisher = busted.executors[descriptor]
-- special publisher that accepts per-block attributes
local publisher = function(name, fn, attributes)
if not attributes and type(fn) == 'table' then
attributes = fn
fn = name
name = descriptor
elseif not attributes then
-- publisher call without attributes, bailout
return _publisher(name, fn)
end
local ctx = busted.context.get()
local trace = attributes.trace or
( busted.context.parent(ctx) and
busted.getTrace(ctx, 3, name) )
local publish = function(f)
busted.publish({ 'register', descriptor }, name, f, trace, attributes)
end
if fn then publish(fn) else return publish end
end
busted.executors[descriptor] = publisher
busted.export(descriptor, publisher)
end
-- register `flaky` block
busted.register('flaky', flaky)
-- patch publishers to accept block level attributes
patch_publisher('it')
patch_publisher('flaky')
patch_publisher('describe')
local function wrap(descriptor, tag, block)
return function(name, fn, trace, attributes)
-- could use options.predicate on subscribe
if not name or not name:find('#' .. tag) then return nil, true end
-- remove tag from name
local name = name:gsub('%s-%#' .. tag ..'%s-', '')
local attributes = tx.update({ trace = trace }, attributes or {})
-- wrap `descriptor` around a block
busted.executors[block](function()
busted.executors[descriptor](name, function()
return fn()
end, attributes)
end, { trace = trace })
-- Do not allow calls from any other subscriber
return nil, false
end
end
-- shortcut: wrap #tag on 'it' and 'describe' blocks with a `flaky` block
busted.subscribe({'register', 'it'}, wrap('it', tag, 'flaky'), { priority = 1 })
busted.subscribe({'register', 'describe'}, wrap('describe', tag, 'flaky'), { priority = 1 })
return true
end