specialized_activities.cpp
Go to the documentation of this file.
1 #ifdef WIN32
2  #include <io.h>
3  #include <fcntl.h>
4  #define pipe(X) _pipe((X), 1024, _O_BINARY)
5  #define close _close
6  #define write _write
7 #endif
8 
9 #include "unit.hpp"
10 
13 #include <iostream>
14 #include <memory>
15 
16 #include <rtt-detail-fwd.hpp>
17 using namespace RTT::detail;
18 
19 using namespace std;
20 using namespace RTT;
21 
22 struct TestFDActivity : public FileDescriptorActivity
23 {
24  int step_count, count, other_count;
25  int fd, other_fd, result;
26 
27  bool do_read;
28 
30 
32  : FileDescriptorActivity(0), step_count(0), count(0), other_count(0), do_read(false) {}
33 
34  void step()
35  {
36  RTT::os::MutexLock lock(mutex);
37 
38  char buffer;
39  if (isUpdated(fd))
40  {
41  ++count;
42  if (do_read)
43  result = read(fd, &buffer, 1);
44  }
45  if (isUpdated(other_fd))
46  {
47  ++other_count;
48  if (do_read)
49  result = read(other_fd, &buffer, 1);
50  }
51  ++step_count;
52  }
53 };
54 
55 BOOST_FIXTURE_TEST_SUITE(SecializedActivitiesSuite,SpecializedActivities)
56 
57 BOOST_AUTO_TEST_CASE( testFileDescriptorActivity )
58 {
59 #if __cplusplus > 199711L
60  unique_ptr<TestFDActivity>
61 #else
62  auto_ptr<TestFDActivity>
63 #endif
64  activity(new TestFDActivity);
65  static const int USLEEP = 250000;
66 
67  int pipe_fds[2];
68  int piperet;
69  piperet = pipe(pipe_fds);
70  BOOST_REQUIRE(piperet == 0);
71  int reader = pipe_fds[0];
72  int writer = pipe_fds[1];
73 
74  int other_pipe[2];
75  piperet = pipe(other_pipe);
76  BOOST_REQUIRE(piperet == 0);
77  int other_reader = other_pipe[0];
78  int other_writer = other_pipe[1];
79 
80  activity->fd = reader;
81  activity->other_fd = other_reader;
82 
83  BOOST_CHECK( activity->start() );
84 
85  // Add something to watch and check that it does start
86  activity->watch(reader);
87  activity->watch(other_reader);
88  usleep(USLEEP);
89  BOOST_CHECK( !activity->isRunning() && activity->isActive() );
90  BOOST_CHECK_EQUAL(0, activity->step_count);
91 
92  // Check trigger(). Disable reading as there won't be any data on the FD
93  activity->do_read = false;
94  BOOST_CHECK( activity->trigger() );
95  usleep(USLEEP);
96  BOOST_CHECK_EQUAL(1, activity->step_count);
97  BOOST_CHECK_EQUAL(0, activity->count);
98  BOOST_CHECK_EQUAL(0, activity->other_count);
99  BOOST_CHECK( !activity->isRunning() && activity->isActive() );
100 
101  // Check normal operations. Re-enable reading.
102  activity->do_read = true;
103  int buffer, result;
104  result = write(writer, &buffer, 2);
105  BOOST_CHECK( result == 2 );
106  usleep(USLEEP);
107  BOOST_CHECK_EQUAL(3, activity->step_count);
108  BOOST_CHECK_EQUAL(2, activity->count);
109  BOOST_CHECK_EQUAL(0, activity->other_count);
110  BOOST_CHECK( !activity->isRunning() && activity->isActive() );
111 
112  result = write(other_writer, &buffer, 2);
113  BOOST_CHECK( result == 2 );
114  usleep(USLEEP);
115  BOOST_CHECK_EQUAL(5, activity->step_count);
116  BOOST_CHECK_EQUAL(2, activity->count);
117  BOOST_CHECK_EQUAL(2, activity->other_count);
118  BOOST_CHECK( !activity->isRunning() && activity->isActive() );
119 
120  // Check breakLoop()
121  BOOST_CHECK( activity->stop() );
122  usleep(USLEEP);
123  BOOST_CHECK( !activity->isRunning() && !activity->isActive() );
124 
125  // Now test timeout
126  activity->do_read = false;
127  activity->setTimeout(100);
128  BOOST_CHECK_EQUAL(100, activity->getTimeout());
129  BOOST_CHECK( activity->start() );
130  sleep(1);
131  BOOST_CHECK( activity->step_count >= 10 );
132  BOOST_CHECK_EQUAL(2, activity->count);
133  BOOST_CHECK_EQUAL(2, activity->other_count);
134  BOOST_CHECK( activity->stop() );
135  activity->setTimeout(0);
136 
137  // Test triggering while step is running
138  activity->step_count = 0;
139  activity->mutex.lock();
140  BOOST_CHECK( activity->start() );
141  activity->trigger();
142  sleep(1);
143  // step is blocking now
144  // trigger another 65537 times
145  for(std::size_t i = 0; i < 65537; ++i) activity->trigger();
146  activity->mutex.unlock();
147  sleep(1);
148  BOOST_CHECK_EQUAL(2, activity->step_count);
149  BOOST_CHECK( activity->stop() );
150 
151 }
152 
153 
155 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
BOOST_AUTO_TEST_CASE(testFileDescriptorActivity)
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
int usleep(unsigned int us)
Definition: fosi.cpp:58
unsigned int sleep(unsigned int s)
Definition: fosi.cpp:51
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:92
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:36