From 47380bb7626544800239ca179a6b2c490ebb6d1c Mon Sep 17 00:00:00 2001
From: Sergey Bronnikov <sergeyb@tarantool.org>
Date: Thu, 29 Jun 2023 15:16:43 +0300
Subject: [PATCH] test: fix flakiness in gh_6128_background_mode_test

Test runs an external process with tarantool that writes to a log file.
Then test reads that log file and searches a string with required
message in it (see function check_err_msg). Test was flaky on macOS and
I suspect it was happening due to a high log level - timeout was not
enough to wait message in the log file.

Patch decreases a log level to a default value and replaces io
functions with the similar alternatives in a fio module. Using
fio functions allows to not block fibers.

NO_CHANGELOG=test fix
NO_DOC=test fix
---
 test/app-luatest/gh_6128_background_mode_test.lua | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/test/app-luatest/gh_6128_background_mode_test.lua b/test/app-luatest/gh_6128_background_mode_test.lua
index 6e61eded87..5c60f8dab1 100644
--- a/test/app-luatest/gh_6128_background_mode_test.lua
+++ b/test/app-luatest/gh_6128_background_mode_test.lua
@@ -12,10 +12,16 @@ local function tarantool_path(arg)
     return arg[index + 1]
 end
 
+-- Check presence of string 'msg' in file 'file'.
+-- Returns true when string is found and false otherwise.
+-- Function is not as smart as grep_log in a luatest (luatest/server.lua)
+-- and reads the whole log file every time, but this log file has a small
+-- size so it is ok.
+-- https://github.com/tarantool/luatest/blob/89da427f8bb3bb66e01d2a7b5a9370d0428d8c52/luatest/server.lua#L660-L727
 local function check_err_msg(file, msg)
-    local f = io.open(file, "rb")
+    local f = fio.open(file, {'O_RDONLY', 'O_NONBLOCK'})
     t.assert_not_equals(f, nil)
-    local content = f:read("*all")
+    local content = f:read(2048)
     f:close()
     return (string.match(content, msg) and true) or false
 end
@@ -31,7 +37,7 @@ g.before_test("test_background_mode_box_cfg", function()
     t.assert_equals(fio.path.exists(g.pid_path), false)
 
     local box_cfg = string.format([[-e box.cfg{
-pid_file='%s', background=true, work_dir='%s', log='%s', log_level=7,
+pid_file='%s', background=true, work_dir='%s', log='%s',
 }]], g.pid_path, g.work_dir, g.log_path)
     local cmd = {
         TARANTOOL_PATH, box_cfg,
-- 
GitLab