Skip to content

Commit

Permalink
Add tests for idle watcher (and priority)
Browse files Browse the repository at this point in the history
Add some simple test for the idle watcher support.  This also tests
priority, since idle watchers are the only thing one can reliably test
priority on.
  • Loading branch information
bdowning committed Aug 30, 2010
1 parent 4717d45 commit d0d3564
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Expand Up @@ -43,8 +43,9 @@ CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
ADD_TEST(ev_io ${LUA} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_ev_io.lua ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_CURRENT_BINARY_DIR}/)
ADD_TEST(ev_loop ${LUA} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_ev_loop.lua ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_CURRENT_BINARY_DIR}/)
ADD_TEST(ev_timer ${LUA} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_ev_timer.lua ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_CURRENT_BINARY_DIR}/)
ADD_TEST(ev_idle ${LUA} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_ev_idle.lua ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_CURRENT_BINARY_DIR}/)
ADD_TEST(ev_signal ${LUA} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_ev_signal.lua ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_CURRENT_BINARY_DIR}/)
SET_TESTS_PROPERTIES(ev_io ev_loop ev_timer ev_signal
SET_TESTS_PROPERTIES(ev_io ev_loop ev_timer ev_signal ev_idle
PROPERTIES
FAIL_REGULAR_EXPRESSION
"not ok")
Expand Down
68 changes: 68 additions & 0 deletions test/test_ev_idle.lua
@@ -0,0 +1,68 @@
print '1..17'

local src_dir, build_dir = ...
package.path = src_dir .. "?.lua;" .. package.path
package.cpath = build_dir .. "?.so;" .. package.cpath

local tap = require("tap")
local ev = require("ev")
local help = require("help")
local dump = require("dumper").dump
local ok = tap.ok

local noleaks = help.collect_and_assert_no_watchers
local loop = ev.Loop.default

-- Simply see if idle watchers work at all:
function test_basic()
local idle1 = ev.Idle.new(
function(loop, idle, revents)
ok(true, 'simple idle')
ok(ev.IDLE == revents, 'ev.IDLE(' .. ev.IDLE .. ') == revents (' .. revents .. ')')
idle:stop(loop)
end)
idle1:start(loop)
loop:loop()
end

-- See if idle prorities make sense
function test_priority()
local high_count, low_count = 0, 0
local idle_high = ev.Idle.new(
function(loop, idle, revents)
high_count = high_count + 1
ok(low_count == 0, 'high idle running first')
if high_count == 3 then
idle:stop(loop)
end
end)
ok(idle_high:priority(ev.MAXPRI) == 0, 'priority was default (0)')
ok(idle_high:priority() == ev.MAXPRI, 'priority correctly set')
idle_high:start(loop)
local idle_low = ev.Idle.new(
function(loop, idle, revents)
low_count = low_count + 1
ok(high_count == 3, 'low idle running last')
if low_count == 3 then
idle:stop(loop)
end
end)
idle_low:start(loop)
local daemon_count = 0
local idle_daemon = ev.Idle.new(
function(loop, idle, revents)
daemon_count = daemon_count + 1
ok(false, "daemon idle shouldn't run at all")
end)
ok(idle_daemon:priority(ev.MINPRI) == 0, 'priority was default (0)')
ok(idle_daemon:priority() == ev.MINPRI, 'priority correctly set')
idle_daemon:start(loop, true)
loop:loop()
ok(high_count == 3, 'high idle ran thrice')
ok(low_count == 3, 'low idle ran thrice')
ok(daemon_count == 0, 'daemon idle never ran')
idle_daemon:stop(loop)
end

noleaks(test_basic, "test_basic")
noleaks(test_priority, "test_priority")

0 comments on commit d0d3564

Please sign in to comment.