[Tarantool-patches] [PATCH v2 09/11] luatest: add new helpers for 'server' object

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Nov 12 02:54:30 MSK 2021


-- Wait until the instance becomes an elected leader.
server:wait_election_leader()

-- Wait until an election leader is found.
server:wait_election_leader_found()

-- Get numeric ID of the instance like in box.info.id.
server:instance_id()

-- Get UUID of the instance like in box.info.uuid.
server:instance_uuid()

These are going to be used in a new test in a next commit.
---
 test/luatest_helpers/server.lua | 66 +++++++++++++++++++++++++++------
 1 file changed, 54 insertions(+), 12 deletions(-)

diff --git a/test/luatest_helpers/server.lua b/test/luatest_helpers/server.lua
index b6d6f1400..900db2243 100644
--- a/test/luatest_helpers/server.lua
+++ b/test/luatest_helpers/server.lua
@@ -75,28 +75,45 @@ function Server:build_env()
     return res
 end
 
-function Server:wait_for_readiness()
-    local alias = self.alias
-    local id = self.id
-    local pid = self.process.pid
+local function wait_cond(cond_name, server, func, ...)
+    local alias = server.alias
+    local id = server.id
+    local pid = server.process.pid
 
     local deadline = clock.time() + WAIT_TIMEOUT
     while true do
-        local ok, is_ready = pcall(function()
-            self:connect_net_box()
-            return self.net_box:eval('return _G.ready') == true
-        end)
-        if ok and is_ready then
-            break
+        if func(...) then
+            return
         end
         if clock.time() > deadline then
-            error(('Starting of server %s-%s (PID %d) was timed out'):format(
-                alias, id, pid))
+            error(('Waiting for "%s" on server %s-%s (PID %d) timed out')
+                  :format(cond_name, alias, id, pid))
         end
         fiber.sleep(WAIT_DELAY)
     end
 end
 
+function Server:wait_for_readiness()
+    return wait_cond('readiness', self, function()
+        local ok, is_ready = pcall(function()
+            self:connect_net_box()
+            return self.net_box:eval('return _G.ready') == true
+        end)
+        return ok and is_ready
+    end)
+end
+
+function Server:wait_election_leader()
+    return wait_cond('election leader', self, self.exec, self, function()
+        return box.info.election.state == 'leader'
+    end)
+end
+
+function Server:wait_election_leader_found()
+    return wait_cond('election leader is found', self, self.exec, self,
+                     function() return box.info.election.leader ~= 0 end)
+end
+
 -- Unlike the original luatest.Server function it waits for
 -- starting the server.
 function Server:start(opts)
@@ -116,6 +133,29 @@ function Server:start(opts)
     end
 end
 
+function Server:instance_id()
+    -- Cache the value when found it first time.
+    if self.instance_id_value then
+        return self.instance_id_value
+    end
+    local id = self:exec(function() return box.info.id end)
+    -- But do not cache 0 - it is an anon instance, its ID might change.
+    if id ~= 0 then
+        self.instance_id_value = id
+    end
+    return id
+end
+
+function Server:instance_uuid()
+    -- Cache the value when found it first time.
+    if self.instance_uuid_value then
+        return self.instance_uuid_value
+    end
+    local uuid = self:exec(function() return box.info.uuid end)
+    self.instance_uuid_value = uuid
+    return uuid
+end
+
 -- TODO: Add the 'wait_for_readiness' parameter for the restart()
 -- method.
 
@@ -146,6 +186,8 @@ function Server:cleanup()
     for _, pattern in ipairs(DEFAULT_CHECKPOINT_PATTERNS) do
         fio.rmtree(('%s/%s'):format(self.workdir, pattern))
     end
+    self.instance_id_value = nil
+    self.instance_uuid_value = nil
 end
 
 function Server:drop()
-- 
2.24.3 (Apple Git-128)



More information about the Tarantool-patches mailing list