semaphores_timed.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <cstdlib>
15 #include <gtest/gtest.h>
16 #include <ecl/time/sleep.hpp>
17 #include <ecl/time/timestamp.hpp>
18 #include "../../include/ecl/ipc/semaphore.hpp"
19 #include "../../include/ecl/ipc/shared_memory.hpp"
20 
21 #ifdef ECL_HAS_SEMAPHORES
22 
23 /*****************************************************************************
24 ** Using
25 *****************************************************************************/
26 
27 using std::string;
28 using ecl::SharedMemory;
29 using ecl::Semaphore;
30 using ecl::Duration;
31 using ecl::Sleep;
32 
33 /*****************************************************************************
34 ** Doxygen
35 *****************************************************************************/
36 
41 /*****************************************************************************
42 ** Namespaces
43 *****************************************************************************/
44 
45 namespace ecl {
46 namespace ipc {
47 namespace tests {
48 
49 /*****************************************************************************
50 ** Data Storage Class
51 *****************************************************************************/
52 
53 class SemaphoreTimedData {
54  public:
55  double value[2];
56  void initialise() { value[0] = 1.0; value[1] = 2.0; };
57 };
58 
59 } // namespace tests
60 } // namespace ipc
61 } // namespace ecl
62 
63 /*****************************************************************************
64 ** Using
65 *****************************************************************************/
66 
67 using ecl::ipc::tests::SemaphoreTimedData;
68 
69 /*****************************************************************************
70 ** Doxygen
71 *****************************************************************************/
72 
77 /*****************************************************************************
78 ** Tests
79 *****************************************************************************/
80 
81 TEST(SemaphoreTimedTests,access) {
82 
83  string sm_name("shared_memory");
84  string sem_name("shared_semaphore");
85  Sleep sleep;
86  Duration timeout(0,750000000);
87 
88  pid_t pID = fork();
89  if (pID == 0)
90  {
91  /*********************************************************************
92  ** Child
93  *********************************************************************/
94 // std::cout << "Child waiting" << std::endl;
95  sleep(1);
96  try {
97  SharedMemory<SemaphoreTimedData> sm(sm_name);
98  Semaphore semaphore(sem_name);
99 // std::cout << "Child trying the lock for 750ms" << std::endl;
100  bool result = semaphore.trylock(timeout);
101  EXPECT_FALSE(result);
102  if ( result ) {
103 // std::cout << "Child locked semaphore." << std::endl;
104  } else {
105 // std::cout << "Child lock attempt timed out." << std::endl;
106 // std::cout << "Child trying the lock again for 750ms" << std::endl;
107  if ( (result = semaphore.trylock(timeout)) ) {
108  EXPECT_TRUE(result);
109 // std::cout << "Child locked semaphore." << std::endl;
110  } else {
111 // std::cout << "Child lock attempt timed out." << std::endl;
112  }
113  }
114  semaphore.unlock();
115  } catch ( ecl::StandardException &e ) {
116  // Don't fail the test, hudson doesn't let us have permissions for instance.
117  std::cout << e.what() << std::endl;
118  }
119  }
120  else if (pID < 0) // failed to fork
121  {
122  std::cerr << "Failed to fork" << std::endl;
123  exit(1);
124  }
125  else
126  {
127  /*********************************************************************
128  ** Parent
129  *********************************************************************/
130  try {
131  SharedMemory<SemaphoreTimedData> sm(sm_name);
132  Semaphore semaphore(sem_name);
133  SemaphoreTimedData *data = sm.data();
134  semaphore.lock();
135  data->value[0] = 1.3;
136  data->value[1] = 2.3;
137  EXPECT_EQ(1.3,data->value[0]);
138  EXPECT_EQ(2.3,data->value[1]);
139 // std::cout << "Parent in the shared memory" << std::endl;
140 // std::cout << "Parent locking the semaphore." << std::endl;
141  sleep(2);
142 // std::cout << "Parent unlocking the semaphore." << std::endl;
143  semaphore.unlock();
144  sleep(2);
145  } catch ( ecl::StandardException &e ) {
146  // Don't fail the test, hudson doesn't let us have permissions for instance.
147  std::cout << e.what() << std::endl;
148  }
149  }
150 }
151 
152 /*****************************************************************************
153 ** Main program
154 *****************************************************************************/
155 
156 int main(int argc, char **argv) {
157 
158  std::cout << std::endl;
159  std::cout << "What you should see:" << std::endl;
160  std::cout << " - Process forks" << std::endl;
161  std::cout << " - Parent creates shared memory." << std::endl;
162  std::cout << " - Parent locks a semaphore and enters shared memory for 2 seconds." << std::endl;
163  std::cout << " - Child waits one second and tries lock for 750ms." << std::endl;
164  std::cout << " - Child fails and again tries lock for 750ms." << std::endl;
165  std::cout << " - Child succeeds in locking.." << std::endl;
166  std::cout << std::endl;
167 
168  testing::InitGoogleTest(&argc,argv);
169  return RUN_ALL_TESTS();
170 }
171 
172 #else
173 
174 /*****************************************************************************
175 ** Alternative Main
176 *****************************************************************************/
177 
178 int main(int argc, char **argv) {
179  std::cout << std::endl;
180  std::cout << "Semaphores are not supported on this platform (or ecl is just lacking)." << std::endl;
181  std::cout << std::endl;
182  return 0;
183 }
184 
185 #endif /* ECL_HAS_SEMAPHORES */
186 
187 
188 
189 
190 
191 
192 
int main(int argc, char **argv)
Embedded control libraries.
const char * what() const
TEST(TypeTests, fundamentals)
TimeStamp Duration


ecl_ipc
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:17