[Tarantool-patches] [PATCH 8/9] luatest: add new helpers for 'server' object

Serge Petrenko sergepetrenko at tarantool.org
Mon Nov 8 18:16:44 MSK 2021



06.11.2021 02:56, Vladislav Shpilevoy пишет:
> -- 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 | 64 ++++++++++++++++++++++++++-------
>   1 file changed, 52 insertions(+), 12 deletions(-)

Hi! Thanks for working on this!
Please, find a couple of small comments below.

>
> diff --git a/test/luatest_helpers/server.lua b/test/luatest_helpers/server.lua
> index b6d6f1400..9959bca6c 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(alias, id, pid))

1. Looks like you forgot to use "cond_name" in the error message.

>           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

2. Let's reset cached instance id in Server:cleanup().
    One might want to call Server:cleanup() and then restart the server
    making it receive a new instance id.

> +
> +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.
>   

-- 
Serge Petrenko



More information about the Tarantool-patches mailing list