Cheetah Software  1.0
test_periodic_tasks.cpp
Go to the documentation of this file.
1 #include "gmock/gmock.h"
2 #include "gtest/gtest.h"
3 
5 
6 class TestPeriodicTask : public PeriodicTask {
7  public:
9  int _counter = 0;
10  bool _cleanedUp = false;
11  bool _init = false;
12  bool _slow = false;
13 
14  void run() override {
15  _counter++;
16  if (_slow) usleep(15000);
17  }
18 
19  void init() override { _init = true; }
20 
21  void cleanup() override { _cleanedUp = true; }
22 };
23 
24 static int scount = 0;
25 
26 void inc_scount() { scount++; }
27 
28 TEST(PeriodicTask, test1) {
29  PeriodicTaskManager taskManager;
30  TestPeriodicTask task1(&taskManager, 0.01f, "test-task-1");
31  TestPeriodicTask task2(&taskManager, 0.02f, "test-task-2");
32 
33  task1.start();
34  task2.start();
35 
36  for (int i = 0; i < 10; i++) {
37  // taskManager.printStatus();
38  usleep(50000);
39  }
40 
41  task1.start();
42  task1.start();
43 
44  EXPECT_TRUE(task2._counter > 0);
45  EXPECT_TRUE(task2._init);
46  EXPECT_FALSE(task2._cleanedUp);
47 
48  EXPECT_TRUE(task2._counter > 0);
49  EXPECT_TRUE(task2._init);
50  EXPECT_FALSE(task2._cleanedUp);
51 
52  taskManager.stopAll();
53 
54  TestPeriodicTask task3(&taskManager, .01f, "slow-task");
55  task3._slow = true;
56  task3.start();
57 
58  for (int i = 0; i < 10; i++) {
59  // taskManager.printStatus();
60  usleep(50000);
61  }
62 
63  taskManager.stopAll();
64  taskManager.stopAll();
65 
66  PeriodicFunction pf(&taskManager, 0.01f, "func-test", &inc_scount);
67  pf.start();
68 
69  usleep(100000);
70  EXPECT_TRUE(scount > 0);
71  printf("scount = %d\n", scount);
72 
73  // taskManager.stopAll(); test destructors cleaning things up instead
74 }
TEST(PeriodicTask, test1)
Implementation of a periodic function running in a separate thread. Periodic tasks have a task manage...
PeriodicTask(PeriodicTaskManager *taskManager, float period, std::string name)
static int scount
void inc_scount()
void cleanup() override
void init() override
MX f(const MX &x, const MX &u)