[{"id":2256,"web_url":"https://patchwork.libcamera.org/comment/2256/","msgid":"<20190714103327.GD6043@pendragon.ideasonboard.com>","date":"2019-07-14T10:33:27","subject":"Re: [libcamera-devel] [PATCH 3/4] test: logging: add logging\n\tprocess test","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Sat, Jul 13, 2019 at 05:16:19AM +0900, Paul Elder wrote:\n> Add a test to test that logging works in child processes. Only\n> logSetFile is tested.\n\nCan you explain why ?\n\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> ---\n>  test/log_process.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++\n>  test/meson.build     |   1 +\n>  2 files changed, 148 insertions(+)\n>  create mode 100644 test/log_process.cpp\n> \n> diff --git a/test/log_process.cpp b/test/log_process.cpp\n> new file mode 100644\n> index 0000000..feda687\n> --- /dev/null\n> +++ b/test/log_process.cpp\n> @@ -0,0 +1,147 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * process_test.cpp - Process test\n\nForgot to rename ?\n\n> + */\n> +\n> +#include <ctime>\n> +#include <cstdlib>\n> +#include <fcntl.h>\n> +#include <iostream>\n> +#include <string.h>\n> +#include <sys/stat.h>\n> +#include <sys/types.h>\n> +#include <unistd.h>\n> +#include <vector>\n> +\n> +#include <libcamera/camera_manager.h>\n> +#include <libcamera/event_dispatcher.h>\n> +#include <libcamera/logging.h>\n> +#include <libcamera/timer.h>\n> +\n> +#include \"log.h\"\n> +#include \"process.h\"\n> +#include \"test.h\"\n> +#include \"utils.h\"\n> +\n> +using namespace std;\n> +using namespace libcamera;\n> +\n> +static string message(\"hello from the child\");\n> +\n> +LOG_DEFINE_CATEGORY(LogProcessTest)\n> +\n> +class LogProcessTestChild\n> +{\n> +public:\n> +\tint run(int status, int num)\n> +\t{\n> +\t\tusleep(50000);\n> +\n> +\t\tstring logPath = \"/tmp/libcamera.worker.test.\" +\n> +\t\t\t\t to_string(num) + \".log\";\n> +\t\tif (logSetFile(logPath.c_str()) < 0)\n> +\t\t\treturn TestSkip;\n> +\t\tLOG(LogProcessTest, Warning) << message;\n> +\n> +\t\treturn status;\n> +\t}\n> +};\n> +\n> +class LogProcessTest : public Test\n> +{\n> +public:\n> +\tLogProcessTest()\n> +\t{\n> +\t}\n\nYou can drop the empty default constructor.\n\n> +\n> +protected:\n> +\tint run()\n> +\t{\n> +\t\tEventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();\n> +\t\tTimer timeout;\n> +\n> +\t\tsrand(time(0));\n> +\t\tint num = rand();\n\nIf you want to do it the C++ way,\n\n\t\tstd::random_device random;\n\t\tint num = random();\n\n> +\n> +\t\tint exitCode = 42;\n> +\t\tvector<std::string> args;\n> +\t\targs.push_back(to_string(exitCode));\n> +\t\targs.push_back(to_string(num));\n> +\t\tint ret = proc_.start(\"/proc/self/exe\", args);\n> +\t\tif (ret) {\n> +\t\t\tcerr << \"failed to start process\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\t\tproc_.finished.connect(this, &LogProcessTest::procFinished);\n\nYou should connect signals before calling the method that will result in\nthe signal being emitted, event if it is safe in this case. I would move\nthe connect and random number generation to the init() method of the\ntest.\n\n> +\n> +\t\ttimeout.start(200);\n> +\t\twhile (timeout.isRunning())\n> +\t\t\tdispatcher->processEvents();\n> +\n> +\t\tif (exitStatus_ != Process::NormalExit) {\n> +\t\t\tcerr << \"process did not exit normally\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tif (exitCode_ == TestSkip)\n> +\t\t\treturn TestSkip;\n\nThis shouldn't happen, that should thus be an error.\n\n> +\n> +\t\tif (exitCode != exitCode_) {\n> +\t\t\tcerr << \"exit code should be \" << exitCode\n> +\t\t\t     << \", actual is \" << exitCode_ << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tstring logPath = \"/tmp/libcamera.worker.test.\" +\n> +\t\t\t\t to_string(num) + \".log\";\n\nCan you make sure the file gets unlinked ? The best way is likely to\ncall unlink in the cleanup() method.\n\n> +\t\tint fd = open(logPath.c_str(), O_RDONLY, S_IRUSR);\n> +\t\tif (fd < 0) {\n> +\t\t\tcerr << \"failed to open tmp log file\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tchar buf[200];\n> +\t\tmemset(buf, 0, sizeof(buf));\n> +\t\tlseek(fd, 0, SEEK_SET);\n\nThis shouldn't be necessary as you've just opened the file.\n\n> +\t\tif (read(fd, buf, sizeof(buf)) < 0) {\n> +\t\t\tcerr << \"Failed to read tmp log file\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\t\tclose(fd);\n> +\n> +\t\tstring str(buf);\n> +\t\tif (str.find(message) == string::npos)\n> +\t\t\treturn TestFail;\n> +\n> +\t\treturn TestPass;\n> +\t}\n> +\n> +private:\n> +\tvoid procFinished(Process *proc, enum Process::ExitStatus exitStatus, int exitCode)\n> +\t{\n> +\t\texitStatus_ = exitStatus;\n> +\t\texitCode_ = exitCode;\n> +\t}\n> +\n> +\tProcess proc_;\n> +\tenum Process::ExitStatus exitStatus_;\n> +\tint exitCode_;\n> +};\n> +\n> +/*\n> + * Can't use TEST_REGISTER() as single binary needs to act as both\n> + * parent and child processes.\n> + */\n> +int main(int argc, char **argv)\n> +{\n> +\tif (argc == 3) {\n> +\t\tint status = std::stoi(argv[1]);\n> +\t\tint num = std::stoi(argv[2]);\n> +\t\tLogProcessTestChild child;\n> +\t\treturn child.run(status, num);\n> +\t}\n> +\n> +\treturn LogProcessTest().execute();\n> +}\n> diff --git a/test/meson.build b/test/meson.build\n> index ad1a2f2..658f283 100644\n> --- a/test/meson.build\n> +++ b/test/meson.build\n> @@ -23,6 +23,7 @@ public_tests = [\n>  internal_tests = [\n>      ['camera-sensor',                   'camera-sensor.cpp'],\n>      ['log',                             'log.cpp'],\n> +    ['log_process',                     'log_process.cpp'],\n>      ['message',                         'message.cpp'],\n>      ['signal-threads',                  'signal-threads.cpp'],\n>      ['threads',                         'threads.cpp'],","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E656161572\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 14 Jul 2019 12:33:56 +0200 (CEST)","from pendragon.ideasonboard.com (softbank126159224182.bbtec.net\n\t[126.159.224.182])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A216C2B2;\n\tSun, 14 Jul 2019 12:33:55 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1563100436;\n\tbh=HtkvFjyjvB8UO3vOYem9z6xnh/4YvJEcdsiK2yXCl4s=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=WCzCVuDSs4LpsqxjgYBnWB0XE8J9cTCimxCy4XlnBF5Gqa84eK9BYIi3bA4lt6GOE\n\thD4Ao1mZZ/ciK1eXYKl6owh/e7E0yihCGBjn3MLxFbk1G3og7FmIS4eOtoxtgixl9e\n\t7pfkn7Y+vNTjD52HJg/f7W9naNQO6lcVXAL73Vow=","Date":"Sun, 14 Jul 2019 13:33:27 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190714103327.GD6043@pendragon.ideasonboard.com>","References":"<20190712201620.30457-1-paul.elder@ideasonboard.com>\n\t<20190712201620.30457-3-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190712201620.30457-3-paul.elder@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 3/4] test: logging: add logging\n\tprocess test","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Sun, 14 Jul 2019 10:33:57 -0000"}}]