2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/spec/unit/background_runner_spec.lua
NiLuJe 6d53f83286
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
  This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
  * Port delay computations to TimeVal
  * Fixed delay computations to handle time warps (large and negative deltas).
  * Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
  * Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
  * Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
  * The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
    The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
    a clock source that is *NOT* MONOTONIC.
    AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
  * Use the <linux/input.h> FFI module instead of re-declaring every constant
  * Fixed (verbose) debug logging of input events to actually translate said constants properly.
  * Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
  * Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
    Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
    as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
  * Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
  * Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
  * Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
  * New methods:
    * Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
    * Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
  * Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
    This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
  * Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
    It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
    because very few time has passed.
    The only code left that does live syscalls does it because it's actually necessary for accuracy,
    and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
  * Fix it so that settings are actually honored.
  * Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.

* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 02:57:59 +02:00

374 lines
10 KiB
Lua

describe("BackgroundRunner widget tests", function()
local Device, PluginShare, MockTime, UIManager
setup(function()
require("commonrequire")
package.unloadAll()
require("document/canvascontext"):init(require("device"))
-- Device needs to be loaded before UIManager.
Device = require("device")
Device.input.waitEvent = function() end
PluginShare = require("pluginshare")
MockTime = require("mock_time")
MockTime:install()
UIManager = require("ui/uimanager")
UIManager._run_forever = true
requireBackgroundRunner()
end)
teardown(function()
MockTime:uninstall()
package.unloadAll()
require("document/canvascontext"):init(require("device"))
stopBackgroundRunner()
end)
before_each(function()
require("util").clearTable(PluginShare.backgroundJobs)
end)
it("should start job", function()
local executed = false
table.insert(PluginShare.backgroundJobs, {
when = 10,
executable = function()
executed = true
end,
})
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(9)
UIManager:handleInput()
assert.is_false(executed)
MockTime:increase(2)
UIManager:handleInput()
assert.is_true(executed)
end)
it("should repeat job", function()
local executed = 0
table.insert(PluginShare.backgroundJobs, {
when = 1,
repeated = function() return executed < 10 end,
executable = function()
executed = executed + 1
end,
})
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
for i = 1, 10 do
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(i, executed)
end
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(10, executed)
end)
it("should repeat job for predefined times", function()
local executed = 0
table.insert(PluginShare.backgroundJobs, {
when = 1,
repeated = 10,
executable = function()
executed = executed + 1
end,
})
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
for i = 1, 10 do
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(i, executed)
end
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(10, executed)
end)
it("should block long job", function()
local executed = 0
local job = {
when = 1,
repeated = true,
executable = function()
executed = executed + 1
MockTime:increase(2)
end,
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, executed)
assert.is_true(job.timeout)
assert.is_true(job.blocked)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, executed)
end)
it("should execute binary", function()
local executed = false
local job = {
when = 1,
executable = "ls | grep this-should-not-be-a-file",
callback = function()
executed = true
end,
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_tv == nil do
MockTime:increase(2)
UIManager:handleInput()
end
-- grep should return 1 when there is no match.
assert.are.equal(1, job.result)
assert.is_false(job.timeout)
assert.is_false(job.bad_command)
assert.is_true(executed)
end)
it("should forward string environment to the executable", function()
local job = {
when = 1,
repeated = false,
executable = "echo $ENV1 | grep $ENV2",
environment = {
ENV1 = "yes",
ENV2 = "yes",
}
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_tv == nil do
MockTime:increase(2)
UIManager:handleInput()
end
-- grep should return 0 when there is a match.
assert.are.equal(0, job.result)
assert.is_false(job.timeout)
assert.is_false(job.bad_command)
job.environment = {
ENV1 = "yes",
ENV2 = "no",
}
job.end_tv = nil
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_tv == nil do
MockTime:increase(2)
UIManager:handleInput()
end
-- grep should return 1 when there is no match.
assert.are.equal(1, job.result)
assert.is_false(job.timeout)
assert.is_false(job.bad_command)
assert.are.not_equal(os.getenv("ENV1"), "yes")
assert.are.not_equal(os.getenv("ENV2"), "yes")
assert.are.not_equal(os.getenv("ENV2"), "no")
end)
it("should forward function environment to the executable", function()
local env2 = "yes"
local job = {
when = 1,
repeated = false,
executable = "echo $ENV1 | grep $ENV2",
environment = function()
return {
ENV1 = "yes",
ENV2 = env2,
}
end,
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_tv == nil do
MockTime:increase(2)
UIManager:handleInput()
end
-- grep should return 0 when there is a match.
assert.are.equal(0, job.result)
assert.is_false(job.timeout)
assert.is_false(job.bad_command)
job.end_tv = nil
env2 = "no"
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_tv == nil do
MockTime:increase(2)
UIManager:handleInput()
end
-- grep should return 1 when there is no match.
assert.are.equal(1, job.result)
assert.is_false(job.timeout)
assert.is_false(job.bad_command)
end)
it("should block long binary job", function()
local job = {
when = 1,
repeated = true,
executable = "sleep 1h",
environment = {
TIMEOUT = 1
}
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_tv == nil do
MockTime:increase(2)
UIManager:handleInput()
end
assert.are.equal(255, job.result)
assert.is_true(job.timeout)
assert.is_true(job.blocked)
end)
it("should execute callback", function()
local executed = 0
table.insert(PluginShare.backgroundJobs, {
when = 1,
repeated = 10,
executable = function() end,
callback = function()
executed = executed + 1
end,
})
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
for i = 1, 10 do
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(i, executed)
end
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(10, executed)
end)
it("should not execute two jobs sequentially", function()
local executed = 0
table.insert(PluginShare.backgroundJobs, {
when = 1,
executable = function()
executed = executed + 1
end,
})
table.insert(PluginShare.backgroundJobs, {
when = 1,
executable = function()
executed = executed + 1
end,
})
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, executed)
end)
it("should stop executing when suspending", function()
local executed = 0
local job = {
when = 1,
repeated = true,
executable = function()
executed = executed + 1
end,
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, executed)
-- Simulate a suspend event.
requireBackgroundRunner():onSuspend()
for i = 1, 10 do
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, executed)
end
-- Simulate a resume event.
requireBackgroundRunner():onResume()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(4, executed)
end)
it("should not start multiple times after multiple onResume", function()
local executed = 0
local job = {
when = 1,
repeated = true,
executable = function()
executed = executed + 1
end,
}
table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
for i = 1, 10 do
requireBackgroundRunner():onResume()
end
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, executed)
end)
end)