• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / copas / 8213284419

09 Mar 2024 08:13AM UTC coverage: 84.221%. Remained the same
8213284419

push

github

Tieske
release 4.7.1

2 of 2 new or added lines in 1 file covered. (100.0%)

97 existing lines in 2 files now uncovered.

1297 of 1540 relevant lines covered (84.22%)

52509.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.33
/src/copas/timer.lua
1
local copas = require("copas")
10✔
2

3
local xpcall = xpcall
10✔
4
local coroutine_running = coroutine.running
10✔
5

6
if _VERSION=="Lua 5.1" and not jit then     -- obsolete: only for Lua 5.1 compatibility
10✔
UNCOV
7
  xpcall = require("coxpcall").xpcall
2✔
UNCOV
8
  coroutine_running = require("coxpcall").running
2✔
9
end
10

11

12
local timer = {}
10✔
13
timer.__index = timer
10✔
14

15

16
local new_name do
10✔
17
  local count = 0
10✔
18

19
  function new_name()
8✔
20
    count = count + 1
30✔
21
    return "copas_timer_" .. count
30✔
22
  end
23
end
24

25

26
do
27
  local function expire_func(self, initial_delay)
28
    if self.errorhandler then
40✔
29
      copas.seterrorhandler(self.errorhandler)
5✔
30
    end
31
    copas.pause(initial_delay)
40✔
32
    while true do
33
      if not self.cancelled then
65✔
34
        if not self.recurring then
65✔
35
          -- non-recurring timer
36
          self.cancelled = true
20✔
37
          self.co = nil
20✔
38

39
          self:callback(self.params)
20✔
40
          return
20✔
41

42
        else
43
          -- recurring timer
44
          self:callback(self.params)
45✔
45
        end
46
      end
47

48
      if self.cancelled then
45✔
49
        -- clean up and exit the thread
50
        self.co = nil
10✔
51
        self.cancelled = true
10✔
52
        return
10✔
53
      end
54

55
      copas.pause(self.delay)
41✔
56
    end
57
  end
58

59

60
  --- Arms the timer object.
61
  -- @param initial_delay (optional) the first delay to use, if not provided uses the timer delay
62
  -- @return timer object, nil+error, or throws an error on bad input
63
  function timer:arm(initial_delay)
10✔
64
    assert(initial_delay == nil or initial_delay >= 0, "delay must be greater than or equal to 0")
45✔
65
    if self.co then
45✔
66
      return nil, "already armed"
5✔
67
    end
68

69
    self.cancelled = false
40✔
70
    self.co = copas.addnamedthread(self.name, expire_func, self, initial_delay or self.delay)
48✔
71
    return self
40✔
72
  end
73
end
74

75

76

77
--- Cancels a running timer.
78
-- @return timer object, or nil+error
79
function timer:cancel()
10✔
80
  if not self.co then
30✔
81
    return nil, "not armed"
10✔
82
  end
83

84
  if self.cancelled then
20✔
85
    return nil, "already cancelled"
×
86
  end
87

88
  self.cancelled = true
20✔
89
  copas.wakeup(self.co)       -- resume asap
20✔
90
  copas.removethread(self.co) -- will immediately drop the thread upon resuming
20✔
91
  self.co = nil
20✔
92
  return self
20✔
93
end
94

95

96
do
97
  -- xpcall error handler that forwards to the copas errorhandler
98
  local ehandler = function(err_obj)
99
    return copas.geterrorhandler()(err_obj, coroutine_running(), nil)
12✔
100
  end
101

102

103
  --- Creates a new timer object.
104
  -- Note: the callback signature is: `function(timer_obj, params)`.
105
  -- @param opts (table) `opts.delay` timer delay in seconds, `opts.callback` function to execute, `opts.recurring` boolean
106
  -- `opts.params` (optional) this value will be passed to the timer callback, `opts.initial_delay` (optional) the first delay to use, defaults to `delay`.
107
  -- @return timer object, or throws an error on bad input
108
  function timer.new(opts)
10✔
109
    assert(opts.delay or -1 >= 0, "delay must be greater than or equal to 0")
35✔
110
    assert(type(opts.callback) == "function", "expected callback to be a function")
35✔
111

112
    local callback = function(timer_obj, params)
113
      xpcall(opts.callback, ehandler, timer_obj, params)
65✔
114
    end
115

116
    return setmetatable({
91✔
117
      name = opts.name or new_name(),
41✔
118
      delay = opts.delay,
35✔
119
      callback = callback,
35✔
120
      recurring = not not opts.recurring,
35✔
121
      params = opts.params,
35✔
122
      cancelled = false,
28✔
123
      errorhandler = opts.errorhandler,
35✔
124
    }, timer):arm(opts.initial_delay)
70✔
125
  end
126
end
127

128

129

130
return timer
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc