From patchwork Wed Jul 10 19:17:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1649 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DBF9C6156E for ; Wed, 10 Jul 2019 21:17:49 +0200 (CEST) Received: from pendragon.ideasonboard.com (softbank126163157105.bbtec.net [126.163.157.105]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 8A4BC54B for ; Wed, 10 Jul 2019 21:17:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1562786269; bh=CK8a7gmWvZY09dYumDGB1As4oCZj7stHZcbwYbdrBgA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=adpfBEYRKtP/Yf94fR7yVkTetKSnWV0MkitRO8dAyxK0e8kI3xxAbi3AMKNCl3g/o 7ySUkl37ybhd/EpjYFXx6iXBMP7VAW32Qmgn4zANx/KyDKUcl6W1TWfK4zKEL/vCE1 I6ZOimE7G+YEOF3n1ThfhOFnjxRAc/2mvMGFFHJk= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 10 Jul 2019 22:17:08 +0300 Message-Id: <20190710191708.13049-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190710191708.13049-1-laurent.pinchart@ideasonboard.com> References: <20190710191708.13049-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 6/6] test: Add test case for signal delivery across threads X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jul 2019 19:17:50 -0000 The test case creates a receiver inheriting from Object, connects a signal to one of its slot, moves the receiver to a different thread, emits the signal and verifies that it gets delivered in the correct thread with the expected value. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- test/meson.build | 1 + test/signal-threads.cpp | 125 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 test/signal-threads.cpp diff --git a/test/meson.build b/test/meson.build index 1f87319aeb65..60ce9601cc55 100644 --- a/test/meson.build +++ b/test/meson.build @@ -22,6 +22,7 @@ public_tests = [ internal_tests = [ ['camera-sensor', 'camera-sensor.cpp'], ['message', 'message.cpp'], + ['signal-threads', 'signal-threads.cpp'], ['threads', 'threads.cpp'], ] diff --git a/test/signal-threads.cpp b/test/signal-threads.cpp new file mode 100644 index 000000000000..c21f32ae0c20 --- /dev/null +++ b/test/signal-threads.cpp @@ -0,0 +1,125 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * signal-threads.cpp - Cross-thread signal delivery test + */ + +#include +#include +#include + +#include "message.h" +#include "thread.h" +#include "test.h" +#include "utils.h" + +using namespace std; +using namespace libcamera; + +class SignalReceiver : public Object +{ +public: + enum Status { + NoSignal, + InvalidThread, + SignalReceived, + }; + + SignalReceiver() + : status_(NoSignal) + { + } + + Status status() const { return status_; } + int value() const { return value_; } + void reset() + { + status_ = NoSignal; + value_ = 0; + } + + void slot(int value) + { + if (Thread::current() != thread()) + status_ = InvalidThread; + else + status_ = SignalReceived; + + value_ = value; + } + +private: + Status status_; + int value_; +}; + +class SignalThreadsTest : public Test +{ +protected: + int run() + { + SignalReceiver receiver; + signal_.connect(&receiver, &SignalReceiver::slot); + + /* Test that a signal is received in the main thread. */ + signal_.emit(42); + + switch (receiver.status()) { + case SignalReceiver::NoSignal: + cout << "No signal received for direct connection" << endl; + return TestFail; + case SignalReceiver::InvalidThread: + cout << "Signal received in incorrect thread " + "for direct connection" << endl; + return TestFail; + default: + break; + } + + /* + * Move the object to a thread and verify that the signal is + * correctly delivered, with the correct data. + */ + receiver.reset(); + receiver.moveToThread(&thread_); + + thread_.start(); + + signal_.emit(42); + + this_thread::sleep_for(chrono::milliseconds(100)); + + switch (receiver.status()) { + case SignalReceiver::NoSignal: + cout << "No signal received for message connection" << endl; + return TestFail; + case SignalReceiver::InvalidThread: + cout << "Signal received in incorrect thread " + "for message connection" << endl; + return TestFail; + default: + break; + } + + if (receiver.value() != 42) { + cout << "Signal received with incorrect value" << endl; + return TestFail; + } + + return TestPass; + } + + void cleanup() + { + thread_.exit(0); + thread_.wait(); + } + +private: + Thread thread_; + + Signal signal_; +}; + +TEST_REGISTER(SignalThreadsTest)