slave_test.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:54:57 CEST 2010  slave_test.cpp
00003 
00004                         slave_test.cpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
00009 
00010  ***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 
00020 #include "unit.hpp"
00021 
00022 #include <rtt-fwd.hpp>
00023 
00024 #include <rtt/TaskContext.hpp>
00025 #include <rtt/Operation.hpp>
00026 #include <rtt/OperationCaller.hpp>
00027 #include <rtt/extras/SlaveActivity.hpp>
00028 
00029 #include <rtt/os/Mutex.hpp>
00030 #include <rtt/os/Condition.hpp>
00031 #include <rtt/os/fosi.h>
00032 
00033 using namespace std;
00034 using namespace RTT::detail;
00035 using namespace RTT;
00036 
00037 class MasterComponent : public TaskContext
00038 {
00039 public:
00040     MasterComponent() : TaskContext("master")
00041       , activity(0)
00042     {
00043     }
00044 
00045     void updateHook()
00046     {
00047         BOOST_TEST_MESSAGE( "[ENTER] MasterComponent::updateHook()" );
00048         if (activity) activity->execute();
00049         BOOST_TEST_MESSAGE( "[EXIT]  MasterComponent::updateHook()" );
00050     }
00051 
00052     void execute(RTT::base::ActivityInterface *activity)
00053     {
00054         this->activity = activity;
00055     }
00056 
00057 public:
00058     RTT::base::ActivityInterface *activity;
00059 };
00060 
00061 class SlaveComponent : public TaskContext
00062 {
00063 public:
00064     SlaveComponent() : TaskContext("slave")
00065       , own_thread_operation_called_counter(0)
00066     {
00067         this->addOperation("ownThreadOperation", &SlaveComponent::ownThreadOperation, this, RTT::OwnThread);
00068         slave_operation_caller.setCaller(this->engine());
00069     }
00070 
00071     void ownThreadOperation()
00072     {
00073         BOOST_TEST_MESSAGE( "[ENTER] SlaveComponent::ownThreadOperation()" );
00074         own_thread_operation_called_counter++;
00075 
00076         if (slave_operation_caller.ready()) {
00077             slave_operation_caller();
00078         }
00079         BOOST_TEST_MESSAGE( "[EXIT]  SlaveComponent::ownThreadOperation()" );
00080     }
00081 
00082 public:
00083     unsigned int own_thread_operation_called_counter;
00084     RTT::OperationCaller<void()> slave_operation_caller;
00085 };
00086 
00087 class ClientComponent : public TaskContext
00088 {
00089 public:
00090     ClientComponent() : TaskContext("client")
00091       , callback_operation_called_counter(0)
00092     {
00093         this->addOperation("callSlaveOperation", &ClientComponent::callSlaveOperation, this, RTT::OwnThread);
00094         this->addOperation("callbackOperation", &ClientComponent::callbackOperation, this, RTT::OwnThread);
00095         slave_operation_caller.setCaller(this->engine());
00096     }
00097 
00098     void callSlaveOperation()
00099     {
00100         BOOST_TEST_MESSAGE( "[ENTER] ClientComponent::callSlaveOperation()" );
00101         BOOST_ASSERT(slave_operation_caller.ready());
00102         slave_operation_caller();
00103         BOOST_TEST_MESSAGE( "[EXIT]  ClientComponent::callSlaveOperation()" );
00104     }
00105 
00106     void callbackOperation()
00107     {
00108         BOOST_TEST_MESSAGE( "[ENTER] ClientComponent::callbackOperation()" );
00109         callback_operation_called_counter++;
00110         BOOST_TEST_MESSAGE( "[EXIT]  ClientComponent::callbackOperation()" );
00111     }
00112 
00113 public:
00114     unsigned int callback_operation_called_counter;
00115     RTT::OperationCaller<void()> slave_operation_caller;
00116 };
00117 
00121 class SlaveActivityTest
00122 {
00123 public:
00124     SlaveActivityTest()
00125     {
00126         //RTT::Logger::Instance()->setLogLevel(RTT::Logger::Debug);
00127 
00128         slave.setActivity(new RTT::extras::SlaveActivity(master.getActivity()));
00129         master.execute(slave.getActivity());
00130         client.slave_operation_caller = slave.getOperation("ownThreadOperation");
00131     }
00132 
00133     ~SlaveActivityTest()
00134     {}
00135 
00136 public:
00137     MasterComponent master;
00138     SlaveComponent slave;
00139     ClientComponent client;
00140 };
00141 
00142 // Registers the fixture into the 'registry'
00143 BOOST_FIXTURE_TEST_SUITE(  SlaveTestSuite,  SlaveActivityTest )
00144 
00145 // Test operation call on the SlaveComponent
00146 BOOST_AUTO_TEST_CASE( testSlaveOperationCall )
00147 {
00148     RTT::OperationCaller<void()> callSlaveOperation = client.getOperation("callSlaveOperation");
00149     BOOST_ASSERT( callSlaveOperation.ready() );
00150 
00151     // Note: master is not running!
00152     RTT::SendHandle<void()> handle = callSlaveOperation.send();
00153     int wait = 5;
00154     while( !handle.collectIfDone() && wait-- ) {
00155         sleep(1);
00156     }
00157     BOOST_CHECK( handle.collectIfDone() );
00158     BOOST_CHECK_EQUAL( slave.own_thread_operation_called_counter, 1 );
00159     BOOST_CHECK_EQUAL( client.callback_operation_called_counter, 0 );
00160 }
00161 
00162 // Test operation call with a callback to the ClientComponent
00163 BOOST_AUTO_TEST_CASE( testSlaveOperationCallWithCallback )
00164 {
00165     RTT::OperationCaller<void()> callSlaveOperation = client.getOperation("callSlaveOperation");
00166     BOOST_ASSERT( callSlaveOperation.ready() );
00167 
00168     // connect SlaveComponent::slave_operation_caller to ClientComponent::callbackOperation()
00169     slave.slave_operation_caller = client.getOperation("callbackOperation");
00170     BOOST_ASSERT( slave.slave_operation_caller.ready() );
00171 
00172     // Note: master is not running!
00173     RTT::SendHandle<void()> handle = callSlaveOperation.send();
00174     int wait = 5;
00175     while( !handle.collectIfDone() && wait-- ) {
00176         sleep(1);
00177     }
00178     BOOST_CHECK( handle.collectIfDone() );
00179     BOOST_CHECK_EQUAL( slave.own_thread_operation_called_counter, 1 );
00180     BOOST_CHECK_EQUAL( client.callback_operation_called_counter, 1 );
00181 }
00182 
00183 BOOST_AUTO_TEST_SUITE_END()


rtt
Author(s): RTT Developers
autogenerated on Fri Sep 9 2016 04:02:15