Skip to content

Commit eef05dd

Browse files
Oleg Chaplashkinylobankov
authored andcommitted
Raise error in wait_for_condition when server is terminated
Raise an error in the `Server:wait_for_condition()` function when the server process is terminated. This is useful to not wait for timeout, for example, when a server fails to start due to bad configuration. Resolves #224, #275
1 parent 0a8b39f commit eef05dd

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
via the corresponding upvalue.
4848
- Add new function `tarantool.skip_if_not_enterprise`.
4949
- Raise an error when non-array arguments passed to the `server:exec()`.
50+
- Raise an error in the `Server:wait_for_condition()` function when
51+
the server process is terminated. This is useful to not wait for timeout, for example,
52+
when a server fails to start due to bad configuration.
5053

5154
## 0.5.7
5255

luatest/server.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,14 @@ function Server:restart(params, opts)
315315
end
316316

317317
-- Wait until the given condition is `true` (anything except `false` and `nil`).
318-
-- Throws an error when timeout exceeds.
318+
-- Throws an error when the server process is terminated or timeout exceeds.
319319
local function wait_for_condition(cond_desc, server, func, ...)
320320
local deadline = clock.time() + WAIT_TIMEOUT
321321
while true do
322+
if not server.process:is_alive() then
323+
error(('Process is terminated when waiting for "%s" condition for server (alias: %s, workdir: %s, pid: %d)')
324+
:format(cond_desc, server.alias, fio.basename(server.workdir), server.process.pid))
325+
end
322326
if func(...) then
323327
return
324328
end
@@ -343,9 +347,12 @@ function Server:stop()
343347

344348
if self.process then
345349
self.process:kill()
346-
wait_for_condition('process is terminated', self, function()
350+
local ok, err = pcall(wait_for_condition, 'process is terminated', self, function()
347351
return not self.process:is_alive()
348352
end)
353+
if not ok and not err:find('Process is terminated when waiting for') then
354+
error(err)
355+
end
349356
log.debug('Killed server process PID ' .. self.process.pid)
350357
self.process = nil
351358
end

test/server_test.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,32 @@ g.test_server_start_with_coverage_enabled = function()
301301
server:exec(function() return box.info.status end), 'running'
302302
)
303303
end
304+
305+
g.test_wait_when_server_is_not_running_by_bad_option = function()
306+
local s1 = Server:new({
307+
box_cfg = {
308+
bad_option = 'bad'
309+
}
310+
})
311+
local s2 = Server:new({
312+
box_cfg = {
313+
replication = {
314+
'bad_uri'
315+
}
316+
}
317+
})
318+
319+
local expected_msg = 'Process is terminated when waiting for "server is ready"'
320+
321+
local status, msg = pcall(Server.start, s1)
322+
t.assert_equals(status, false)
323+
t.assert_str_contains(msg, expected_msg)
324+
t.assert_equals(s1.process:is_alive(), false)
325+
s1:clean()
326+
327+
status, msg = pcall(Server.start, s2)
328+
t.assert_equals(status, false)
329+
t.assert_str_contains(msg, expected_msg)
330+
t.assert_equals(s2.process:is_alive(), false)
331+
s2:clean()
332+
end

0 commit comments

Comments
 (0)