~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to src/test/TestTimers.cc

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum, Clint Byrum, Micah Gersten
  • Date: 2011-02-12 22:50:26 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110212225026-yyyw4tk0msgql3ul
Tags: 0.24.2-0ubuntu1
[ Clint Byrum <clint@ubuntu.com> ]
* New upstream release. (LP: #658670, LP: #684011)
* debian/patches/fix-mkcephfs.patch: dropped (applied upstream)
* Removed .la files from libceph1-dev, libcrush1-dev and 
  librados1-dev (per Debian policy v3.9.1 10.2).
* debian/control: adding pkg-config as a build dependency
* debian/control: depend on libcrypto++-dev instead of libssl-dev
* debian/watch: added watch file

[ Micah Gersten <micahg@ubuntu.com> ]
* debian/control: add Homepage

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "common/Mutex.h"
 
2
#include "common/Timer.h"
 
3
#include "common/common_init.h"
 
4
 
 
5
#include <iostream>
 
6
 
 
7
/*
 
8
 * TestTimers
 
9
 *
 
10
 * Tests the timer classes
 
11
 */
 
12
#define MAX_TEST_CONTEXTS 5
 
13
 
 
14
class TestContext;
 
15
 
 
16
namespace
 
17
{
 
18
  int array[MAX_TEST_CONTEXTS];
 
19
  int array_idx;
 
20
  TestContext* test_contexts[MAX_TEST_CONTEXTS];
 
21
 
 
22
  Mutex array_lock("test_timers_mutex");
 
23
}
 
24
 
 
25
class TestContext : public Context
 
26
{
 
27
public:
 
28
  TestContext(int num_)
 
29
    : num(num_)
 
30
  {
 
31
  }
 
32
 
 
33
  virtual void finish(int r)
 
34
  {
 
35
    array_lock.Lock();
 
36
    cout << "TestContext " << num << std::endl;
 
37
    array[array_idx++] = num;
 
38
    array_lock.Unlock();
 
39
  }
 
40
 
 
41
  virtual ~TestContext()
 
42
  {
 
43
  }
 
44
 
 
45
protected:
 
46
  int num;
 
47
};
 
48
 
 
49
class StrictOrderTestContext : public TestContext
 
50
{
 
51
public:
 
52
  StrictOrderTestContext (int num_)
 
53
    : TestContext(num_)
 
54
  {
 
55
  }
 
56
 
 
57
  virtual void finish(int r)
 
58
  {
 
59
    array_lock.Lock();
 
60
    cout << "StrictOrderTestContext " << num << std::endl;
 
61
    array[num] = num;
 
62
    array_lock.Unlock();
 
63
  }
 
64
 
 
65
  virtual ~StrictOrderTestContext()
 
66
  {
 
67
  }
 
68
};
 
69
 
 
70
static void print_status(const char *str, int ret)
 
71
{
 
72
  cout << str << ": ";
 
73
  cout << ((ret == 0) ? "SUCCESS" : "FAILURE");
 
74
  cout << std::endl;
 
75
}
 
76
 
 
77
template <typename T>
 
78
static int basic_timer_test(T &timer, Mutex *lock)
 
79
{
 
80
  int ret = 0;
 
81
  memset(&array, 0, sizeof(array));
 
82
  array_idx = 0;
 
83
  memset(&test_contexts, 0, sizeof(test_contexts));
 
84
 
 
85
  cout << __PRETTY_FUNCTION__ << std::endl;
 
86
 
 
87
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
88
    test_contexts[i] = new TestContext(i);
 
89
  }
 
90
 
 
91
 
 
92
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
93
    if (lock)
 
94
      lock->Lock();
 
95
    utime_t inc(2 * i, 0);
 
96
    utime_t t = g_clock.now() + inc;
 
97
    timer.add_event_at(t, test_contexts[i]);
 
98
    if (lock)
 
99
      lock->Unlock();
 
100
  }
 
101
 
 
102
  bool done = false;
 
103
  do {
 
104
    sleep(1);
 
105
    array_lock.Lock();
 
106
    done = (array_idx == MAX_TEST_CONTEXTS);
 
107
    array_lock.Unlock();
 
108
  } while (!done);
 
109
 
 
110
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
111
    if (array[i] != i) {
 
112
      ret = 1;
 
113
      cout << "error: expected array[" << i << "] = " << i
 
114
           << "; got " << array[i] << " instead." << std::endl;
 
115
    }
 
116
  }
 
117
 
 
118
  return ret;
 
119
}
 
120
 
 
121
static int test_out_of_order_insertion(SafeTimer &timer, Mutex *lock)
 
122
{
 
123
  int ret = 0;
 
124
  memset(&array, 0, sizeof(array));
 
125
  array_idx = 0;
 
126
  memset(&test_contexts, 0, sizeof(test_contexts));
 
127
 
 
128
  cout << __PRETTY_FUNCTION__ << std::endl;
 
129
 
 
130
  test_contexts[0] = new StrictOrderTestContext(0);
 
131
  test_contexts[1] = new StrictOrderTestContext(1);
 
132
 
 
133
  {
 
134
    utime_t inc(100, 0);
 
135
    utime_t t = g_clock.now() + inc;
 
136
    lock->Lock();
 
137
    timer.add_event_at(t, test_contexts[0]);
 
138
    lock->Unlock();
 
139
  }
 
140
 
 
141
  {
 
142
    utime_t inc(2, 0);
 
143
    utime_t t = g_clock.now() + inc;
 
144
    lock->Lock();
 
145
    timer.add_event_at(t, test_contexts[1]);
 
146
    lock->Unlock();
 
147
  }
 
148
 
 
149
  int secs = 0;
 
150
  for (; secs < 100 ; ++secs) {
 
151
    sleep(1);
 
152
    array_lock.Lock();
 
153
    int a = array[1];
 
154
    array_lock.Unlock();
 
155
    if (a == 1)
 
156
      break;
 
157
  }
 
158
 
 
159
  if (secs == 100) {
 
160
    ret = 1;
 
161
    cout << "error: expected array[" << 1 << "] = " << 1
 
162
         << "; got " << array[1] << " instead." << std::endl;
 
163
  }
 
164
 
 
165
  return ret;
 
166
}
 
167
 
 
168
static int safe_timer_cancel_all_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
 
169
{
 
170
  cout << __PRETTY_FUNCTION__ << std::endl;
 
171
 
 
172
  int ret = 0;
 
173
  memset(&array, 0, sizeof(array));
 
174
  array_idx = 0;
 
175
  memset(&test_contexts, 0, sizeof(test_contexts));
 
176
 
 
177
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
178
    test_contexts[i] = new TestContext(i);
 
179
  }
 
180
 
 
181
  safe_timer_lock.Lock();
 
182
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
183
    utime_t inc(4 * i, 0);
 
184
    utime_t t = g_clock.now() + inc;
 
185
    safe_timer.add_event_at(t, test_contexts[i]);
 
186
  }
 
187
  safe_timer_lock.Unlock();
 
188
 
 
189
  sleep(10);
 
190
 
 
191
  safe_timer_lock.Lock();
 
192
  safe_timer.cancel_all_events();
 
193
  safe_timer_lock.Unlock();
 
194
 
 
195
  for (int i = 0; i < array_idx; ++i) {
 
196
    if (array[i] != i) {
 
197
      ret = 1;
 
198
      cout << "error: expected array[" << i << "] = " << i
 
199
           << "; got " << array[i] << " instead." << std::endl;
 
200
    }
 
201
  }
 
202
 
 
203
  return ret;
 
204
}
 
205
 
 
206
static int safe_timer_cancellation_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
 
207
{
 
208
  cout << __PRETTY_FUNCTION__ << std::endl;
 
209
 
 
210
  int ret = 0;
 
211
  memset(&array, 0, sizeof(array));
 
212
  array_idx = 0;
 
213
  memset(&test_contexts, 0, sizeof(test_contexts));
 
214
 
 
215
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
216
    test_contexts[i] = new StrictOrderTestContext(i);
 
217
  }
 
218
 
 
219
  safe_timer_lock.Lock();
 
220
  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
 
221
    utime_t inc(4 * i, 0);
 
222
    utime_t t = g_clock.now() + inc;
 
223
    safe_timer.add_event_at(t, test_contexts[i]);
 
224
  }
 
225
  safe_timer_lock.Unlock();
 
226
 
 
227
  // cancel the even-numbered events
 
228
  for (int i = 0; i < MAX_TEST_CONTEXTS; i += 2) {
 
229
    safe_timer_lock.Lock();
 
230
    safe_timer.cancel_event(test_contexts[i]);
 
231
    safe_timer_lock.Unlock();
 
232
  }
 
233
 
 
234
  sleep(20);
 
235
 
 
236
  safe_timer_lock.Lock();
 
237
  safe_timer.cancel_all_events();
 
238
  safe_timer_lock.Unlock();
 
239
 
 
240
  for (int i = 1; i < array_idx; i += 2) {
 
241
    if (array[i] != i) {
 
242
      ret = 1;
 
243
      cout << "error: expected array[" << i << "] = " << i
 
244
           << "; got " << array[i] << " instead." << std::endl;
 
245
    }
 
246
  }
 
247
 
 
248
  return ret;
 
249
}
 
250
 
 
251
int main(int argc, const char **argv)
 
252
{
 
253
  vector<const char*> args;
 
254
  argv_to_vec(argc, argv, args);
 
255
  env_to_vec(args);
 
256
 
 
257
  ceph_set_default_id("admin");
 
258
  common_set_defaults(false);
 
259
  common_init(args, "ceph", true);
 
260
 
 
261
  int ret;
 
262
  Mutex safe_timer_lock("safe_timer_lock");
 
263
  SafeTimer safe_timer(safe_timer_lock);
 
264
 
 
265
  ret = basic_timer_test <SafeTimer>(safe_timer, &safe_timer_lock);
 
266
  if (ret)
 
267
    goto done;
 
268
 
 
269
  ret = safe_timer_cancel_all_test(safe_timer, safe_timer_lock);
 
270
  if (ret)
 
271
    goto done;
 
272
 
 
273
  ret = safe_timer_cancellation_test(safe_timer, safe_timer_lock);
 
274
  if (ret)
 
275
    goto done;
 
276
 
 
277
  ret = test_out_of_order_insertion(safe_timer, &safe_timer_lock);
 
278
  if (ret)
 
279
    goto done;
 
280
 
 
281
done:
 
282
  print_status(argv[0], ret);
 
283
  return ret;
 
284
}