File: TimeoutManagerTest.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (243 lines) | stat: -rw-r--r-- 7,925 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * TimeoutManagerTest.cpp
 * Test fixture for the TimeoutManager class.
 * Copyright (C) 2013 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>

#include <map>

#include "common/io/TimeoutManager.h"
#include "ola/Callback.h"
#include "ola/Clock.h"
#include "ola/ExportMap.h"
#include "ola/Logging.h"
#include "ola/testing/TestUtils.h"

using ola::ExportMap;
using ola::MockClock;
using ola::NewSingleCallback;
using ola::NewCallback;
using ola::TimeInterval;
using ola::TimeStamp;
using ola::io::TimeoutManager;
using ola::thread::timeout_id;

class TimeoutManagerTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(TimeoutManagerTest);
  CPPUNIT_TEST(testSingleTimeouts);
  CPPUNIT_TEST(testRepeatingTimeouts);
  CPPUNIT_TEST(testAbortedRepeatingTimeouts);
  CPPUNIT_TEST(testPendingEventShutdown);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testSingleTimeouts();
    void testRepeatingTimeouts();
    void testAbortedRepeatingTimeouts();
    void testPendingEventShutdown();

    void HandleEvent(unsigned int event_id) {
      m_event_counters[event_id]++;
    }

    bool HandleRepeatingEvent(unsigned int event_id) {
      m_event_counters[event_id]++;
      return true;
    }

    // returns false after the second event.
    bool HandleAbortedEvent(unsigned int event_id) {
      m_event_counters[event_id]++;
      return m_event_counters[event_id] < 2;
    }

    unsigned int GetEventCounter(unsigned int event_id) {
      return m_event_counters[event_id];
    }

 private:
    ExportMap m_map;
    std::map<unsigned int, unsigned int> m_event_counters;
};


CPPUNIT_TEST_SUITE_REGISTRATION(TimeoutManagerTest);

/*
 * Check RegisterSingleTimeout works.
 */
void TimeoutManagerTest::testSingleTimeouts() {
  MockClock clock;
  TimeoutManager timeout_manager(&m_map, &clock);

  OLA_ASSERT_FALSE(timeout_manager.EventsPending());

  TimeInterval timeout_interval(1, 0);
  timeout_id id1 = timeout_manager.RegisterSingleTimeout(
      timeout_interval,
      NewSingleCallback(this, &TimeoutManagerTest::HandleEvent, 1u));
  OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT);

  TimeStamp last_checked_time;

  clock.AdvanceTime(0, 1);  // Small offset to work around timer precision
  clock.CurrentMonotonicTime(&last_checked_time);
  TimeInterval next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_EQ(0u, GetEventCounter(1));
  OLA_ASSERT_LT(next, timeout_interval);

  clock.AdvanceTime(0, 500000);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_EQ(0u, GetEventCounter(1));
  OLA_ASSERT_LT(next, TimeInterval(0, 500000));

  clock.AdvanceTime(0, 500000);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_TRUE(next.IsZero());
  OLA_ASSERT_EQ(1u, GetEventCounter(1));

  OLA_ASSERT_FALSE(timeout_manager.EventsPending());

  // now add another timeout and then remove it
  timeout_id id2 = timeout_manager.RegisterSingleTimeout(
      timeout_interval,
      NewSingleCallback(this, &TimeoutManagerTest::HandleEvent, 2u));
  OLA_ASSERT_NE(id2, ola::thread::INVALID_TIMEOUT);
  OLA_ASSERT_TRUE(timeout_manager.EventsPending());
  OLA_ASSERT_EQ(0u, GetEventCounter(2));

  timeout_manager.CancelTimeout(id2);

  clock.AdvanceTime(1, 0);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_FALSE(timeout_manager.EventsPending());
  OLA_ASSERT_EQ(0u, GetEventCounter(2));
}

/*
 * Check RegisterRepeatingTimeout works.
 */
void TimeoutManagerTest::testRepeatingTimeouts() {
  MockClock clock;
  TimeoutManager timeout_manager(&m_map, &clock);

  OLA_ASSERT_FALSE(timeout_manager.EventsPending());

  TimeInterval timeout_interval(1, 0);
  timeout_id id1 = timeout_manager.RegisterRepeatingTimeout(
      timeout_interval,
      NewCallback(this, &TimeoutManagerTest::HandleRepeatingEvent, 1u));
  OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT);

  TimeStamp last_checked_time;

  clock.AdvanceTime(0, 1);  // Small offset to work around timer precision
  clock.CurrentMonotonicTime(&last_checked_time);
  TimeInterval next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_EQ(0u, GetEventCounter(1));
  OLA_ASSERT_LT(next, timeout_interval);

  clock.AdvanceTime(0, 500000);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_EQ(0u, GetEventCounter(1));
  OLA_ASSERT_LT(next, TimeInterval(0, 500000));

  clock.AdvanceTime(0, 500000);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_LTE(next, timeout_interval);
  OLA_ASSERT_EQ(1u, GetEventCounter(1));

  OLA_ASSERT_TRUE(timeout_manager.EventsPending());

  // fire the event again
  clock.AdvanceTime(1, 0);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_LTE(next, timeout_interval);
  OLA_ASSERT_EQ(2u, GetEventCounter(1));

  // cancel the event
  timeout_manager.CancelTimeout(id1);
  clock.AdvanceTime(1, 0);
  clock.CurrentMonotonicTime(&last_checked_time);
  next = timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_TRUE(next.IsZero());
  OLA_ASSERT_EQ(2u, GetEventCounter(1));
}


/*
 * Check returning false from a repeating timeout cancels the timeout.
 */
void TimeoutManagerTest::testAbortedRepeatingTimeouts() {
  MockClock clock;
  TimeoutManager timeout_manager(&m_map, &clock);

  OLA_ASSERT_FALSE(timeout_manager.EventsPending());

  TimeInterval timeout_interval(1, 0);
  timeout_id id1 = timeout_manager.RegisterRepeatingTimeout(
      timeout_interval,
      NewCallback(this, &TimeoutManagerTest::HandleAbortedEvent, 1u));
  OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT);

  TimeStamp last_checked_time;

  clock.AdvanceTime(0, 1);  // Small offset to work around timer precision
  clock.AdvanceTime(1, 0);
  clock.CurrentMonotonicTime(&last_checked_time);
  timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_EQ(1u, GetEventCounter(1));

  clock.AdvanceTime(1, 0);
  clock.CurrentMonotonicTime(&last_checked_time);
  timeout_manager.ExecuteTimeouts(&last_checked_time);
  OLA_ASSERT_EQ(2u, GetEventCounter(1));

  OLA_ASSERT_FALSE(timeout_manager.EventsPending());
}

/*
 * Check we don't leak if there are events pending when the manager is
 * destroyed.
 */
void TimeoutManagerTest::testPendingEventShutdown() {
  MockClock clock;
  TimeoutManager timeout_manager(&m_map, &clock);

  OLA_ASSERT_FALSE(timeout_manager.EventsPending());

  TimeInterval timeout_interval(1, 0);
  timeout_id id1 = timeout_manager.RegisterSingleTimeout(
      timeout_interval,
      NewSingleCallback(this, &TimeoutManagerTest::HandleEvent, 1u));
  OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT);

  timeout_id id2 = timeout_manager.RegisterRepeatingTimeout(
      timeout_interval,
      NewCallback(this, &TimeoutManagerTest::HandleRepeatingEvent, 1u));
  OLA_ASSERT_NE(id2, ola::thread::INVALID_TIMEOUT);

  OLA_ASSERT_TRUE(timeout_manager.EventsPending());
}