Skip to content

Commit

Permalink
🔀 Merge branch 'yann/tests/eventqueue/fix-call' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Mar 25, 2022
2 parents e5a258d + 0b7e378 commit 8fcb23f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 10 deletions.
5 changes: 2 additions & 3 deletions libs/BatteryKit/source/BatteryKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ using namespace std::chrono_literals;

void BatteryKit::startEventHandler()
{
_event_queue.dispatch_forever();

auto on_tick = [this] {
if (_on_low_battery && level() <= 5) {
_on_low_battery();
Expand All @@ -19,10 +21,7 @@ void BatteryKit::startEventHandler()
}
};

on_tick(); // TODO (@john_doe): only for unit tests. Due to event_queue that does not make the call
_event_queue.call_every(1s, on_tick);

_event_queue.dispatch_forever();
}

auto BatteryKit::level() -> uint8_t
Expand Down
3 changes: 3 additions & 0 deletions libs/BatteryKit/tests/BatteryKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mocks/leka/Battery.h"
#include "mocks/leka/EventQueue.h"

using namespace leka;

Expand All @@ -21,6 +22,8 @@ class BatteryKitTest : public ::testing::Test
// void SetUp() override {}
// void TearDown() override {}

mock::EventQueue event_queue {};

mock::Battery mock_battery;
BatteryKit batterykit;
};
Expand Down
3 changes: 3 additions & 0 deletions libs/RobotKit/tests/RobotController_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mocks/leka/Battery.h"
#include "mocks/leka/EventQueue.h"
#include "mocks/leka/FirmwareUpdate.h"
#include "mocks/leka/LEDAnimation.h"
#include "mocks/leka/MCU.h"
Expand Down Expand Up @@ -75,6 +76,8 @@ class RobotControllerTest : public testing::Test
}
void TearDown() override { ble::delete_mocks(); }

mock::EventQueue event_queue {};

mock::Timeout sleep_timeout {};
mock::Battery battery {};

Expand Down
4 changes: 0 additions & 4 deletions tests/unit/headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ target_include_directories(mbed-os

${EXTERN_MBED_OS_DIR}

${EXTERN_MBED_OS_DIR}/events/tests/UNITTESTS/doubles
${EXTERN_MBED_OS_DIR}/events/tests/UNITTESTS/doubles/equeue

${EXTERN_MBED_OS_DIR}/cmsis/tests/UNITTESTS/doubles

${EXTERN_MBED_OS_DIR}/connectivity/tests/UNITTESTS/doubles
Expand Down Expand Up @@ -60,7 +57,6 @@ target_include_directories(mbed-os

${EXTERN_MBED_OS_DIR}/events
${EXTERN_MBED_OS_DIR}/events/include
${EXTERN_MBED_OS_DIR}/events/include/events

${EXTERN_MBED_OS_DIR}/hal
${EXTERN_MBED_OS_DIR}/hal/include
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/mocks/mocks/leka/EventQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "equeue_stub.h"
#include "gmock/gmock.h"
#include "stubs/leka/CoreEventQueue.h"
#include "stubs/mbed/EventQueue_extension.h"

namespace leka::mock {

class EventQueue
{
public:
EventQueue()
{
equeue_stub.void_ptr = &ptr;

spy_CoreEventQueue_on_dispatch_forever_call = [] { equeue_stub.call_cb_immediately = true; };
spy_EventQueue_on_dispatch_forever_call = [] { equeue_stub.call_cb_immediately = true; };
}

~EventQueue()
{
equeue_stub.void_ptr = nullptr;
equeue_stub.call_cb_immediately = false;
}

private:
struct equeue_event ptr;
};

} // namespace leka::mock
2 changes: 1 addition & 1 deletion tests/unit/stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ target_sources(stubs

${UNIT_TESTS_STUBS_LEKA_DIR}/source/CoreEventQueue.cpp

${EXTERN_MBED_OS_DIR}/events/tests/UNITTESTS/doubles/equeue_stub.c
${EXTERN_MBED_OS_DIR}/rtos/tests/UNITTESTS/doubles/mbed_rtos_rtx_stub.c
${EXTERN_MBED_OS_DIR}/rtos/tests/UNITTESTS/doubles/Mutex_stub.cpp
${EXTERN_MBED_OS_DIR}/rtos/tests/UNITTESTS/doubles/rtx_mutex_stub.c
${EXTERN_MBED_OS_DIR}/rtos/tests/UNITTESTS/doubles/ThisThread_stub.cpp
${EXTERN_MBED_OS_DIR}/rtos/tests/UNITTESTS/doubles/Thread_stub.cpp
${EXTERN_MBED_OS_DIR}/platform/tests/UNITTESTS/doubles/mbed_assert_stub.cpp
${EXTERN_MBED_OS_DIR}/events/tests/UNITTESTS/doubles/equeue_stub.c
${EXTERN_MBED_OS_DIR}/events/tests/UNITTESTS/doubles/EventQueue_stub.cpp
${EXTERN_MBED_OS_DIR}/drivers/tests/UNITTESTS/doubles/SerialBase_stub.cpp
${EXTERN_MBED_OS_DIR}/platform/tests/UNITTESTS/doubles/FileHandle_stub.cpp
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/stubs/stubs/leka/CoreEventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ namespace leka {

extern bool spy_CoreEventQueue_did_call_function;

extern std::function<void()> spy_CoreEventQueue_on_dispatch_forever_call;

} // namespace leka
5 changes: 4 additions & 1 deletion tests/unit/stubs/stubs/leka/source/CoreEventQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace leka {

bool spy_CoreEventQueue_did_call_function = false;
std::function<void()> spy_CoreEventQueue_on_dispatch_forever_call;

void CoreEventQueue::dispatch_forever()
{
// do nothing
if (spy_CoreEventQueue_on_dispatch_forever_call) {
spy_CoreEventQueue_on_dispatch_forever_call();
}
}

void CoreEventQueue::callMbedCallback(mbed::Callback<void()> const &f)
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/stubs/stubs/mbed/EventQueue_extension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Leka - LekaOS
// Copyright 2021 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include <functional>

namespace leka {

extern std::function<void()> spy_EventQueue_on_dispatch_forever_call;

} // namespace leka
8 changes: 7 additions & 1 deletion tests/unit/stubs/stubs/mbed/source/EventQueue_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
// Copyright 2021 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "../EventQueue_extension.h"

#include "events/EventQueue.h"

std::function<void()> leka::spy_EventQueue_on_dispatch_forever_call;

void events::EventQueue::dispatch_forever()
{
// nothing to do
if (leka::spy_EventQueue_on_dispatch_forever_call) {
leka::spy_EventQueue_on_dispatch_forever_call();
}
}

0 comments on commit 8fcb23f

Please sign in to comment.