Skip to content

Commit e03b037

Browse files
committed
server: fix grep_log not finding string logged immediately before
Logs written by a test server go through a pipe and are processed by a luatest fiber before they make it to the log file so if grep_log() is called immediately after exec() that writes something to the log it may fail to find the logged string. Let's fix this issue by writing a unique marker string to the server log via exec() and retrying grep_log() until we find the marker. Closes #421
1 parent 8306126 commit e03b037

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fixed a bug when `server:grep_log()` failed to find a string logged in
6+
`server:exec()` called immediately before it (gh-421).
7+
38
## 1.1.0
49

510
- Added logging to unified file (gh-324).

luatest/server.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,19 @@ function Server:grep_log(pattern, bytes_num, opts)
896896
fail('Failed to open log file')
897897
end
898898

899-
io.flush() -- attempt to flush stdout == log fd
899+
-- Logs written by a test server go through a pipe and are processed
900+
-- by a luatest fiber before they make it to the log file. Let's write
901+
-- a unique marker string to the server log via server:exec() and retry
902+
-- until we find the marker.
903+
local marker
904+
if self ~= nil and self.net_box ~= nil and self.net_box:ping() then
905+
marker = ('LUATEST_GREP_LOG_MARKER:%d'):format(math.random(1e9))
906+
self:exec(function(s) require('log').info(s) end, {marker})
907+
end
908+
909+
local retries = 0
900910

911+
::retry::
901912
local filesize = file:seek(0, 'SEEK_END')
902913
if filesize == nil then
903914
fail('Failed to get log file size')
@@ -938,11 +949,21 @@ function Server:grep_log(pattern, bytes_num, opts)
938949
else
939950
found = string.match(line, pattern) or found
940951
end
952+
if marker ~= nil and string.match(line, marker) then
953+
marker = nil
954+
end
941955
end
942956
pos = endpos and endpos + 2 -- jump to char after \n
943957
until pos == nil
944958
until s == ''
945959

960+
if found == nil and marker ~= nil and retries < 10 then
961+
log.info('Retrying grep because marker was not found')
962+
retries = retries + 1
963+
fiber.sleep(0.1)
964+
goto retry
965+
end
966+
946967
file:close()
947968

948969
return found

0 commit comments

Comments
 (0)