specialized_activities.cpp
Go to the documentation of this file.
00001 #ifdef WIN32
00002   #include <io.h>
00003   #include <fcntl.h>
00004   #define pipe(X) _pipe((X), 1024, _O_BINARY)
00005   #define close _close
00006   #define write _write
00007 #endif
00008 
00009 #include "unit.hpp"
00010 
00011 #include "specialized_activities.hpp"
00012 #include <extras/FileDescriptorActivity.hpp>
00013 #include <iostream>
00014 #include <rtt-detail-fwd.hpp>
00015 using namespace RTT::detail;
00016 
00017 using namespace std;
00018 using namespace RTT;
00019 
00020 struct TestFDActivity : public FileDescriptorActivity
00021 {
00022     int step_count, count, other_count;
00023     int fd, other_fd, result;
00024 
00025     bool do_read;
00026 
00027     RTT::os::Mutex mutex;
00028 
00029     TestFDActivity()
00030         : FileDescriptorActivity(0), step_count(0), count(0), other_count(0), do_read(false) {}
00031 
00032     void step()
00033     {
00034         RTT::os::MutexLock lock(mutex);
00035 
00036         char buffer;
00037         if (isUpdated(fd))
00038         {
00039             ++count;
00040             if (do_read)
00041                 result = read(fd, &buffer, 1);
00042         }
00043         if (isUpdated(other_fd))
00044         {
00045             ++other_count;
00046             if (do_read)
00047                 result = read(other_fd, &buffer, 1);
00048         }
00049         ++step_count;
00050     }
00051 };
00052 
00053 BOOST_FIXTURE_TEST_SUITE(SecializedActivitiesSuite,SpecializedActivities)
00054 
00055 BOOST_AUTO_TEST_CASE( testFileDescriptorActivity )
00056 {
00057     auto_ptr<TestFDActivity> activity(new TestFDActivity);
00058     static const int USLEEP = 250000;
00059 
00060     int pipe_fds[2];
00061     int piperet;
00062     piperet = pipe(pipe_fds);
00063     BOOST_REQUIRE(piperet == 0);
00064     int reader = pipe_fds[0];
00065     int writer = pipe_fds[1];
00066 
00067     int other_pipe[2];
00068     piperet = pipe(other_pipe);
00069     BOOST_REQUIRE(piperet == 0);
00070     int other_reader = other_pipe[0];
00071     int other_writer = other_pipe[1];
00072 
00073     activity->fd = reader;
00074     activity->other_fd = other_reader;
00075 
00076     BOOST_CHECK( activity->start() );
00077 
00078     // Add something to watch and check that it does start
00079     activity->watch(reader);
00080     activity->watch(other_reader);
00081     usleep(USLEEP);
00082     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00083     BOOST_CHECK_EQUAL(0, activity->step_count);
00084 
00085     // Check trigger(). Disable reading as there won't be any data on the FD
00086     activity->do_read = false;
00087     BOOST_CHECK( activity->trigger() );
00088     usleep(USLEEP);
00089     BOOST_CHECK_EQUAL(1, activity->step_count);
00090     BOOST_CHECK_EQUAL(0, activity->count);
00091     BOOST_CHECK_EQUAL(0, activity->other_count);
00092     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00093 
00094     // Check normal operations. Re-enable reading.
00095     activity->do_read = true;
00096     int buffer, result;
00097     result = write(writer, &buffer, 2);
00098     BOOST_CHECK( result == 2 );
00099     usleep(USLEEP);
00100     BOOST_CHECK_EQUAL(3, activity->step_count);
00101     BOOST_CHECK_EQUAL(2, activity->count);
00102     BOOST_CHECK_EQUAL(0, activity->other_count);
00103     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00104 
00105     result = write(other_writer, &buffer, 2);
00106     BOOST_CHECK( result == 2 );
00107     usleep(USLEEP);
00108     BOOST_CHECK_EQUAL(5, activity->step_count);
00109     BOOST_CHECK_EQUAL(2, activity->count);
00110     BOOST_CHECK_EQUAL(2, activity->other_count);
00111     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00112 
00113     // Check breakLoop()
00114     BOOST_CHECK( activity->stop() );
00115     usleep(USLEEP);
00116     BOOST_CHECK( !activity->isRunning() && !activity->isActive() );
00117 
00118     // Now test timeout
00119     activity->do_read = false;
00120     activity->setTimeout(100);
00121     BOOST_CHECK_EQUAL(100, activity->getTimeout());
00122     BOOST_CHECK( activity->start() );
00123     sleep(1);
00124     BOOST_CHECK( activity->step_count >= 10 );
00125     BOOST_CHECK_EQUAL(2, activity->count);
00126     BOOST_CHECK_EQUAL(2, activity->other_count);
00127     BOOST_CHECK( activity->stop() );
00128     activity->setTimeout(0);
00129 
00130     // Test triggering while step is running
00131     activity->step_count = 0;
00132     activity->mutex.lock();
00133     BOOST_CHECK( activity->start() );
00134     activity->trigger();
00135     sleep(1);
00136     // step is blocking now
00137     // trigger another 65537 times
00138     for(std::size_t i = 0; i < 65537; ++i) activity->trigger();
00139     activity->mutex.unlock();
00140     sleep(1);
00141     BOOST_CHECK_EQUAL(2, activity->step_count);
00142     BOOST_CHECK( activity->stop() );
00143 
00144 }
00145 
00146 
00147 BOOST_AUTO_TEST_SUITE_END()
00148 


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