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

lunarmodules / copas / 10622180872

29 Aug 2024 08:28PM UTC coverage: 84.576% (+0.2%) from 84.343%
10622180872

push

github

web-flow
feat(exit): add exit signalling (#172)

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

5 existing lines in 2 files now uncovered.

1327 of 1569 relevant lines covered (84.58%)

33560.31 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")
12✔
2

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

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

11

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

15

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

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

25

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

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

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

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

55
      copas.pause(self.delay)
48✔
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)
12✔
64
    assert(initial_delay == nil or initial_delay >= 0, "delay must be greater than or equal to 0")
40✔
65
    if self.co then
40✔
66
      return nil, "already armed"
4✔
67
    end
68

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

75

76

77
--- Cancels a running timer.
78
-- @return timer object, or nil+error
79
function timer:cancel()
12✔
80
  if not self.co then
28✔
81
    return nil, "not armed"
8✔
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)
8✔
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)
12✔
109
    assert(opts.delay or -1 >= 0, "delay must be greater than or equal to 0")
32✔
110
    assert(type(opts.callback) == "function", "expected callback to be a function")
32✔
111

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

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

128

129

130
return timer
12✔
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