Debian Codegraph @

test/functional/lua/test_coverage.lua

raw
1 --[[
2 -- This should be the very first file executed during a test
3 -- otherwise coverage will be partly missed
4 --]]
5 local logger = require "rspamd_logger"
6 local mempool = require "rspamd_mempool"
7 local loaded, luacov = pcall(require, 'luacov.runner')
8 if not loaded then
9 logger.errx('luacov is not loaded, will not collect coverage')
10 return
11 end
12
13 luacov.init()
14
15 local pool = mempool.create()
16 -- we don't need the pool, we need userdata to put __gc() on it
17 -- __gc() is not called for tables, that't why there is such trick
18 -- so, we are free to clean memory, let's do this :)
19 pool:destroy()
20
21 local woker_name
22
23 rspamd_config:add_on_load(function(cfg, ev_base, worker)
24 woker_name = worker:get_name()
25 local stats_path = rspamd_paths["DBDIR"] .. '/' .. woker_name .. '.luacov.stats.out'
26 local config = luacov.load_config()
27 config.statsfile = stats_path
28 end)
29
30 -- use global variable to prevent the object from being GC'ed too early
31 __GLOBAL_COVERAGE_WATCHDOG = {pool = pool}
32
33 local mt = {
34 __gc = function()
35 --[[
36 -- We could've used finish_script but in that case some coverage would be missed:
37 -- pool destructors are executed after finish_scripts (when Lua state is terminated and that's
38 -- how we can collect coverage of cove executed there
39 --]]
40 if woker_name then
41 luacov.shutdown()
42 end
43 end
44 }
45
46 debug.setmetatable(__GLOBAL_COVERAGE_WATCHDOG.pool, mt)