From 63c965572d12af1e631b7ed8d95d333338dce45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Thu, 1 Aug 2019 07:13:25 +0200 Subject: [PATCH] Improved test wait functionality --- test/cowait.lua | 43 +++++++++++++++---------------------------- test/cowait.script | 5 ----- test/test.collection | 15 --------------- test/test_monarch.lua | 14 ++++---------- 4 files changed, 19 insertions(+), 58 deletions(-) delete mode 100644 test/cowait.script diff --git a/test/cowait.lua b/test/cowait.lua index bb707db..656b0bd 100644 --- a/test/cowait.lua +++ b/test/cowait.lua @@ -1,38 +1,25 @@ local M = {} - -local instances = {} - -local function create(fn) - local instance = { - co = coroutine.running(), - fn = fn, - } - table.insert(instances, instance) - coroutine.yield(instance.co) -end - - - function M.seconds(amount) - local time = socket.gettime() + amount - create(function() - return socket.gettime() >= time + local co = coroutine.running() + assert(co, "You must run this from within a coroutine") + timer.delay(amount, false, function() + coroutine.resume(co) end) + coroutine.yield() end - -function M.eval(fn) - create(fn) -end - -function M.update() - for k,instance in pairs(instances) do - if instance.fn() then - instances[k] = nil - coroutine.resume(instance.co) +function M.eval(fn, timeout) + local co = coroutine.running() + assert(co, "You must run this from within a coroutine") + local start = socket.gettime() + timer.delay(0.01, true, function(self, handle, time_elapsed) + if fn() or (timeout and socket.gettime() > (start + timeout)) then + timer.cancel(handle) + coroutine.resume(co) end - end + end) + coroutine.yield() end return setmetatable(M, { diff --git a/test/cowait.script b/test/cowait.script deleted file mode 100644 index 0cc5580..0000000 --- a/test/cowait.script +++ /dev/null @@ -1,5 +0,0 @@ -local cowait = require "test.cowait" - -function update(self, dt) - cowait.update() -end diff --git a/test/test.collection b/test/test.collection index bbad341..3fd99a1 100644 --- a/test/test.collection +++ b/test/test.collection @@ -17,21 +17,6 @@ embedded_instances { " w: 1.0\n" " }\n" "}\n" - "components {\n" - " id: \"cowait\"\n" - " component: \"/test/cowait.script\"\n" - " position {\n" - " x: 0.0\n" - " y: 0.0\n" - " z: 0.0\n" - " }\n" - " rotation {\n" - " x: 0.0\n" - " y: 0.0\n" - " z: 0.0\n" - " w: 1.0\n" - " }\n" - "}\n" "embedded_components {\n" " id: \"screensfactory\"\n" " type: \"collectionfactory\"\n" diff --git a/test/test_monarch.lua b/test/test_monarch.lua index 228446a..59cff0a 100644 --- a/test/test_monarch.lua +++ b/test/test_monarch.lua @@ -28,20 +28,14 @@ return function() local function wait_timeout(fn, ...) local args = { ... } - local time = socket.gettime() - cowait(function() - return fn(unpack(args)) or socket.gettime() > time + 5 - end) + cowait(function() return fn(unpack(args)) end, 5) return fn(...) end local function wait_until_done(fn) - local is_done = false - local function done() - is_done = true - end - fn(done) - wait_timeout(function() return is_done end) + local done = false + fn(function() done = true end) + wait_timeout(function() return done end) end local function wait_until_visible(screen_id) return wait_timeout(is_visible, screen_id)