libcamera/test/timer-fail.cpp
Laurent Pinchart 626172a16b libcamera: Drop file name from header comment blocks
Source files in libcamera start by a comment block header, which
includes the file name and a one-line description of the file contents.
While the latter is useful to get a quick overview of the file contents
at a glance, the former is mostly a source of inconvenience. The name in
the comments can easily get out of sync with the file name when files
are renamed, and copy & paste during development have often lead to
incorrect names being used to start with.

Readers of the source code are expected to know which file they're
looking it. Drop the file name from the header comment block.

The change was generated with the following script:

----------------------------------------

dirs="include/libcamera src test utils"

declare -rA patterns=(
	['c']=' \* '
	['cpp']=' \* '
	['h']=' \* '
	['py']='# '
	['sh']='# '
)

for ext in ${!patterns[@]} ; do
	files=$(for dir in $dirs ; do find $dir -name "*.${ext}" ; done)
	pattern=${patterns[${ext}]}

	for file in $files ; do
		name=$(basename ${file})
		sed -i "s/^\(${pattern}\)${name} - /\1/" "$file"
	done
done
----------------------------------------

This misses several files that are out of sync with the comment block
header. Those will be addressed separately and manually.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
2024-05-08 22:39:50 +03:00

109 lines
2 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2024, Ideas on Board Oy
*
* Threaded timer failure test
*/
#include <chrono>
#include <iostream>
#include <libcamera/base/event_dispatcher.h>
#include <libcamera/base/object.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/timer.h>
#include "test.h"
using namespace libcamera;
using namespace std;
using namespace std::chrono_literals;
class TimeoutHandler : public Object
{
public:
TimeoutHandler()
: timer_(this), timeout_(false)
{
timer_.timeout.connect(this, &TimeoutHandler::timeoutHandler);
}
void start()
{
timer_.start(100ms);
}
bool timeout() const
{
return timeout_;
}
private:
void timeoutHandler()
{
timeout_ = true;
}
Timer timer_;
bool timeout_;
};
class TimerFailTest : public Test
{
protected:
int init()
{
thread_.start();
timeout_ = new TimeoutHandler();
timeout_->moveToThread(&thread_);
return TestPass;
}
int run()
{
/*
* Test that the forbidden operation of starting the timer from
* another thread results in a failure. We need to interrupt the
* event dispatcher to make sure we don't succeed simply because
* the event dispatcher hasn't noticed the timer restart.
*/
timeout_->start();
thread_.eventDispatcher()->interrupt();
this_thread::sleep_for(chrono::milliseconds(200));
/*
* The wrong start() call should result in an assertion in debug
* builds, and a timeout in release builds. The test is
* therefore marked in meson.build as expected to fail. We need
* to return TestPass in the unexpected (usually known as
* "fail") case, and TestFail otherwise.
*/
if (timeout_->timeout()) {
cout << "Timer start from wrong thread succeeded unexpectedly"
<< endl;
return TestPass;
}
return TestFail;
}
void cleanup()
{
/*
* Object class instances must be destroyed from the thread
* they live in.
*/
timeout_->deleteLater();
thread_.exit(0);
thread_.wait();
}
private:
TimeoutHandler *timeout_;
Thread thread_;
};
TEST_REGISTER(TimerFailTest)