You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_coverage.lua 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. luacov.init()
  13. local pool = mempool.create()
  14. -- we don't need the pool, we need userdata to put __gc() on it
  15. -- __gc() is not called for tables, that't why there is such trick
  16. -- so, we are free to clean memory, let's do this :)
  17. pool:destroy()
  18. local woker_name
  19. rspamd_config:add_on_load(function(cfg, ev_base, worker)
  20. woker_name = worker:get_name()
  21. local stats_path = rspamd_paths["DBDIR"] .. '/' .. woker_name .. '.luacov.stats.out'
  22. local config = luacov.load_config()
  23. config.statsfile = stats_path
  24. end)
  25. -- use global variable to prevent the object from being GC'ed too early
  26. __GLOBAL_COVERAGE_WATCHDOG = {pool = pool}
  27. local mt = {
  28. __gc = function()
  29. --[[
  30. -- We could've used finish_script but in that case some coverage would be missed:
  31. -- pool destructors are executed after finish_scripts (when Lua state is terminated and that's
  32. -- how we can collect coverage of cove executed there
  33. --]]
  34. if woker_name then
  35. luacov.shutdown()
  36. end
  37. end
  38. }
  39. debug.setmetatable(__GLOBAL_COVERAGE_WATCHDOG.pool, mt)