Skip to content

Unit_Tests

Olaf Maibaum edited this page Oct 9, 2020 · 1 revision

Unit Tests

The Tasking Framework has a concurrent execution model. Unit tests of application code are nearly impossible, when the functionality between channels and tasks should be tested. For this purpose, the Tasking Framework provides a special scheduler for unit tests. With this scheduler the test thread can run as an executor until no further event and task is pending. The application in unit tests is very simple and straight forward. The following example will use google test for unit testing.

#include <gtest/gtest.h>
#include <schedulePolicyLifo.h>
#include <schedulerUnitTest.h>

#include "SingleDataChannel.hpp"
#include "SingleDataPrinterTask.hpp"

class MyTest: public ::testing::Test
{
public:
    MyTest(void): scheduler(policy), task(scheduler)
    {
        task.configureInput(0u, channel);
        scheduler.start();
    }

    Tasking::SchedulePolicyLifo policy;
    Tasking::SchedulerUnitTest scheduler;
    SingleDataChannel<std::string> channel;
    SingleDataPrinterTask task(scheduler);
}

TEST_F(MyTest, callTask)
{
    channel.push("This is a test");
    scheduler.schedule();
    EXPECT_TRUE(true);
}

The example shows that a test fixture MyTest is set-up with a scheduler, a channel, and a task. Channel and task are connected and the scheduler will start. In the test itself a message is pushed to the channel, which will queue the task in the run queue of the scheduler. By calling scheduler.schedule() the test case becomes the executor and will process the pending task. When the run queue becomes empty the result can be checked. Normally this will be checks on outgoing channels, which don't exist in the example.

The method schedule also accepts a parameter with a time. The simulated clock will be forwarded then and as the first action all pending events will be handled by the scheduler. An example of such an application can be found in the test folder in the test testTaskEvent.cpp. Further examples for unit testing with the Tasking Framework can be found in the test folder, which test the Tasking Framework API.

Clone this wiki locally