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     TestFDActivity()
00027         : FileDescriptorActivity(0), step_count(0), count(0), other_count(0), do_read(false) {}
00028 
00029     void step()
00030     {
00031         char buffer;
00032         if (isUpdated(fd))
00033         {
00034             ++count;
00035             if (do_read)
00036                 result = read(fd, &buffer, 1);
00037         }
00038         if (isUpdated(other_fd))
00039         {
00040             ++other_count;
00041             if (do_read)
00042                 result = read(other_fd, &buffer, 1);
00043         }
00044         ++step_count;
00045     };
00046 };
00047 
00048 BOOST_FIXTURE_TEST_SUITE(SecializedActivitiesSuite,SpecializedActivities)
00049 
00050 BOOST_AUTO_TEST_CASE( testFileDescriptorActivity )
00051 {
00052     auto_ptr<TestFDActivity> activity(new TestFDActivity);
00053     static const int USLEEP = 250000;
00054 
00055     int pipe_fds[2];
00056     int piperet;
00057     piperet = pipe(pipe_fds);
00058     BOOST_REQUIRE(piperet == 0);
00059     int reader = pipe_fds[0];
00060     int writer = pipe_fds[1];
00061 
00062     int other_pipe[2];
00063     piperet = pipe(other_pipe);
00064     BOOST_REQUIRE(piperet == 0);
00065     int other_reader = other_pipe[0];
00066     int other_writer = other_pipe[1];
00067 
00068     activity->fd = reader;
00069     activity->other_fd = other_reader;
00070 
00071     BOOST_CHECK( activity->start() );
00072 
00073     // Add something to watch and check that it does start
00074     activity->watch(reader);
00075     activity->watch(other_reader);
00076     usleep(USLEEP);
00077     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00078     BOOST_CHECK_EQUAL(0, activity->step_count);
00079 
00080     // Check trigger(). Disable reading as there won't be any data on the FD
00081     activity->do_read = false;
00082     BOOST_CHECK( activity->trigger() );
00083     usleep(USLEEP);
00084     BOOST_CHECK_EQUAL(1, activity->step_count);
00085     BOOST_CHECK_EQUAL(0, activity->count);
00086     BOOST_CHECK_EQUAL(0, activity->other_count);
00087     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00088 
00089     // Check normal operations. Re-enable reading.
00090     activity->do_read = true;
00091     int buffer, result;
00092     result = write(writer, &buffer, 2);
00093     BOOST_CHECK( result == 2 );
00094     usleep(USLEEP);
00095     BOOST_CHECK_EQUAL(3, activity->step_count);
00096     BOOST_CHECK_EQUAL(2, activity->count);
00097     BOOST_CHECK_EQUAL(0, activity->other_count);
00098     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00099 
00100     result = write(other_writer, &buffer, 2);
00101     BOOST_CHECK( result == 2 );
00102     usleep(USLEEP);
00103     BOOST_CHECK_EQUAL(5, activity->step_count);
00104     BOOST_CHECK_EQUAL(2, activity->count);
00105     BOOST_CHECK_EQUAL(2, activity->other_count);
00106     BOOST_CHECK( !activity->isRunning() && activity->isActive() );
00107 
00108     // Check breakLoop()
00109     BOOST_CHECK( activity->stop() );
00110     usleep(USLEEP);
00111     BOOST_CHECK( !activity->isRunning() && !activity->isActive() );
00112 
00113     // Now test timeout
00114     activity->do_read = false;
00115     activity->setTimeout(100);
00116     BOOST_CHECK_EQUAL(100, activity->getTimeout());
00117     BOOST_CHECK( activity->start() );
00118     sleep(1);
00119     BOOST_CHECK( activity->step_count >= 10 );
00120     BOOST_CHECK_EQUAL(2, activity->count);
00121     BOOST_CHECK_EQUAL(2, activity->other_count);
00122 }
00123 
00124 
00125 BOOST_AUTO_TEST_SUITE_END()
00126 


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:16:18