From 89599f7beaa422459b8979d275610d4fa8cca543 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Wed, 26 Oct 2016 11:43:23 -0200 Subject: [PATCH] AP_HAL_Linux: add unit tests for Thread implementations --- libraries/AP_HAL_Linux/tests/test_thread.cpp | 120 +++++++++++++++++++ libraries/AP_HAL_Linux/tests/wscript | 7 ++ 2 files changed, 127 insertions(+) create mode 100644 libraries/AP_HAL_Linux/tests/test_thread.cpp create mode 100644 libraries/AP_HAL_Linux/tests/wscript diff --git a/libraries/AP_HAL_Linux/tests/test_thread.cpp b/libraries/AP_HAL_Linux/tests/test_thread.cpp new file mode 100644 index 0000000000..ee41536834 --- /dev/null +++ b/libraries/AP_HAL_Linux/tests/test_thread.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2016 Intel Corporation. All rights reserved. + * + * This file is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +#include + +#include +#include + +#include +#include +#include + +using namespace Linux; + +const AP_HAL::HAL &hal = AP_HAL::get_HAL(); + +class TestThread1 : public Thread { +public: + TestThread1() : Thread(nullptr) { } + + int n_loop = 0; + +protected: + bool _run() override { + n_loop = 1; + return true; + } +}; + +TEST(LinuxThread, override_run) +{ + TestThread1 thr; + EXPECT_TRUE(thr.start(nullptr, 0, 0)); + + while (thr.n_loop == 0) { + usleep(10000); + } + + EXPECT_EQ(thr.n_loop, 1); +} + +class TestThread2 : public Thread { +public: + TestThread2() : Thread{FUNCTOR_BIND_MEMBER(&TestThread2::_task, void)} { } + + int n_loop = 0; + +protected: + void _task() { + n_loop = 1; + } +}; + +TEST(LinuxThread, run_task) +{ + TestThread2 thr; + EXPECT_TRUE(thr.start(nullptr, 0, 0)); + + while (thr.n_loop == 0) { + usleep(10000); + } + + EXPECT_EQ(thr.n_loop, 1); +} + +TEST(LinuxThread, poller_thread) +{ + PollerThread thr; + EXPECT_TRUE(thr.start(nullptr, 0, 0)); + + while (!thr.is_started()) { + usleep(1000); + } + + usleep(10000); + + EXPECT_TRUE(thr.stop()); + EXPECT_TRUE(thr.join()); +} + +class TestPeriodicThread1 : public PeriodicThread { +public: + TestPeriodicThread1() : PeriodicThread{FUNCTOR_BIND_MEMBER(&TestPeriodicThread1::_task, void)} { } +protected: + void _task() { } +}; + +TEST(LinuxThread, periodic_thread) +{ + TestPeriodicThread1 thr; + EXPECT_TRUE(thr.set_rate(1000)); + EXPECT_TRUE(thr.start(nullptr, 0, 0)); + + while (!thr.is_started()) { + usleep(1000); + } + + // this must fail as the thread already started + EXPECT_FALSE(thr.set_rate(10)); + + usleep(10000); + + EXPECT_TRUE(thr.stop()); + EXPECT_TRUE(thr.join()); +} + +AP_GTEST_MAIN() diff --git a/libraries/AP_HAL_Linux/tests/wscript b/libraries/AP_HAL_Linux/tests/wscript new file mode 100644 index 0000000000..cd3e5e3ce7 --- /dev/null +++ b/libraries/AP_HAL_Linux/tests/wscript @@ -0,0 +1,7 @@ +#!/usr/bin/env python +# encoding: utf-8 + +def build(bld): + bld.ap_find_tests( + use='ap', + )