hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ReducingTaskTTLManagerTest.cpp
Go to the documentation of this file.
1 /*
2  * ReducingTaskTTLManagerTest.cpp
3  *
4  * Created on: Nov 24, 2013
5  * Author: Steven
6  */
7 
8 #include <gtest/gtest.h>
10 #include <Poco/Thread.h>
11 
12 using namespace std;
14 
15 class ReducingTaskTTLManagerTest: public ::testing::Test {
16 protected:
17  virtual void SetUp(){
18  taskId = 1;
19  }
20 
21  virtual void TearDown(){
22  }
23 
24  void sleep_milliseconds( unsigned int milliseconds ) {
25  Poco::Thread::sleep( milliseconds );
26  }
27 
28  unsigned int milliseconds( int millis ) {
29  return millis;
30  }
31 
33  unsigned long long taskId;
34 };
35 
37  taskTTLManager.addTaskTTL( taskId, 10 );
38 
39  EXPECT_EQ( taskTTLManager.getTaskTTL( taskId ), 10U);
40 }
41 
42 TEST_F( ReducingTaskTTLManagerTest, NotExistTaskTTL ) {
43  EXPECT_EQ( taskTTLManager.getTaskTTL( taskId ), 0U);
44 }
45 
46 TEST_F( ReducingTaskTTLManagerTest, AddTaskMultiTimes ) {
47  taskTTLManager.addTaskTTL( taskId, 10 );
48  taskTTLManager.addTaskTTL( taskId, 20 );
49 
50  EXPECT_EQ( taskTTLManager.getTaskTTL( taskId ), 20U);
51 }
52 
53 TEST_F( ReducingTaskTTLManagerTest, RemoveNotExpiredTask ) {
54  taskTTLManager.addTaskTTL( taskId, 20 );
55  EXPECT_EQ( taskTTLManager.getTaskTTL( taskId ), 20U);
56  taskTTLManager.removeTaskTTL( taskId );
57  EXPECT_FALSE( taskTTLManager.isTaskTerminatedByTTL( taskId ) );
58 }
59 
60 TEST_F( ReducingTaskTTLManagerTest, RemoveExpiredTask ) {
61 
62  taskTTLManager.addTaskTTL( taskId, 10 );
63  sleep_milliseconds(20);
64  EXPECT_TRUE( taskTTLManager.isTaskExpired( taskId ) );
65  taskTTLManager.removeTaskTTL( taskId );
66  EXPECT_TRUE( taskTTLManager.isTaskTerminatedByTTL( taskId ) );
67 }
68 
69 TEST_F( ReducingTaskTTLManagerTest, NotExpiredWithoutParam ) {
70  taskTTLManager.addTaskTTL( taskId, 100 );
71 
72  EXPECT_FALSE( taskTTLManager.isTaskExpired( taskId ) );
73  sleep_milliseconds( 10 );
74  EXPECT_FALSE( taskTTLManager.isTaskExpired( taskId ) );
75 }
76 
77 TEST_F( ReducingTaskTTLManagerTest, NotExpiredWithParam ) {
78  taskTTLManager.addTaskTTL( taskId, 100 );
79 
80  sleep_milliseconds( 10 );
81  EXPECT_FALSE( taskTTLManager.isTaskExpired( taskId, 20 ) );
82 }
83 
84 TEST_F( ReducingTaskTTLManagerTest, ExpiredWithoutParam ) {
85  taskTTLManager.addTaskTTL( taskId, 10 );
86 
87  EXPECT_FALSE( taskTTLManager.isTaskExpired( taskId ) );
88  sleep_milliseconds( 10 );
89  EXPECT_TRUE( taskTTLManager.isTaskExpired( taskId ) );
90 }
91 
92 TEST_F( ReducingTaskTTLManagerTest, ExpiredWithParam ) {
93  taskTTLManager.addTaskTTL( taskId, 100 );
94 
95  sleep_milliseconds( 10 );
96  EXPECT_TRUE( taskTTLManager.isTaskExpired( taskId, 10 ) );
97 }
98 
99 
100 TEST_F( ReducingTaskTTLManagerTest, ExpiredTasksCleanup ) {
101  taskTTLManager.addTaskTTL( taskId, 10 );
102  taskTTLManager.addTaskTTL( taskId + 1, 20 );
103  taskTTLManager.addTaskTTL( taskId + 2, 30 );
104  taskTTLManager.addTaskTTL( taskId + 4, 40 );
105  taskTTLManager.addTaskTTL( taskId + 5, 50 );
106 
107  sleep_milliseconds( 11 );
108  taskTTLManager.removeTaskTTL( taskId );
109  taskTTLManager.removeTaskTTL( taskId + 1);
110  EXPECT_TRUE( taskTTLManager.isTaskTerminatedByTTL( taskId ) );
111  EXPECT_FALSE( taskTTLManager.isTaskTerminatedByTTL( taskId + 1 ) );
112  sleep_milliseconds( 11 );
113  EXPECT_EQ( taskTTLManager.cleanupExpiredTasksByTTLQueue( 10, 10 ), 1U);
114  EXPECT_FALSE( taskTTLManager.isTaskTerminatedByTTL( taskId ) );
115  EXPECT_FALSE( taskTTLManager.isTaskTerminatedByTTL( taskId + 1 ) );
116 }
117 
118 TEST_F( ReducingTaskTTLManagerTest, ExceededTTLTasksWithDefault ) {
119  std::vector<unsigned long long> exceededTTLTasks;
120  taskTTLManager.addTaskTTL( taskId, 10 );
121  taskTTLManager.addTaskTTL( taskId + 1, 20 );
122  taskTTLManager.addTaskTTL( taskId + 2, 30 );
123  taskTTLManager.addTaskTTL( taskId + 4, 40 );
124  taskTTLManager.addTaskTTL( taskId + 5, 50 );
125  sleep_milliseconds( 21 );
126  taskTTLManager.getExceededTTLTasks( 10, exceededTTLTasks );
127 
128  EXPECT_EQ( exceededTTLTasks.size(), 2U);
129 }
130 
131 TEST_F( ReducingTaskTTLManagerTest, ExceededTTLTasksWithParam ) {
132  std::vector<unsigned long long> exceededTTLTasks;
133  taskTTLManager.addTaskTTL( taskId, 10 );
134  taskTTLManager.addTaskTTL( taskId + 1, 20 );
135  taskTTLManager.addTaskTTL( taskId + 2, 30 );
136  taskTTLManager.addTaskTTL( taskId + 4, 40 );
137  taskTTLManager.addTaskTTL( taskId + 5, 50 );
138  sleep_milliseconds( 20 );
139  taskTTLManager.getExceededTTLTasks( 10, exceededTTLTasks, 10 );
140 
141  EXPECT_EQ( exceededTTLTasks.size(), 5U);
142 }
143 
144 TEST_F( ReducingTaskTTLManagerTest, NoTerminatedTasks ) {
145  taskTTLManager.addTaskTTL( taskId, 10 );
146  sleep_milliseconds( 20 );
147  EXPECT_FALSE( taskTTLManager.isTaskTerminatedByTTL( taskId ) );
148 }
149 
150 TEST_F( ReducingTaskTTLManagerTest, TaskIsTerminated ) {
151  taskTTLManager.addTaskTTL( taskId, 10 );
152  sleep_milliseconds( 11 );
153  taskTTLManager.removeTaskTTL( taskId );
154  EXPECT_TRUE( taskTTLManager.isTaskTerminatedByTTL( taskId ) );
155 }
156