summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_sharedptr_registry.cc
blob: 01d0a8489942497a52493dbf8ed1d9a8bd3a5ca0 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
 *
 * Author: Loic Dachary <loic@dachary.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 Library Public License for more details.
 *
 */

#include <stdio.h>
#include <signal.h>
#include "gtest/gtest.h"
#include "common/Thread.h"
#include "common/sharedptr_registry.hpp"
#include "common/ceph_argparse.h"

class SharedPtrRegistryTest : public SharedPtrRegistry<unsigned int, int> {
public:
  ceph::mutex &get_lock() { return lock; }
  map<unsigned int, pair<std::weak_ptr<int>, int*> > &get_contents() {
    return contents;
  }
};

class SharedPtrRegistry_all : public ::testing::Test {
public:

  class Thread_wait : public Thread {
  public:
    SharedPtrRegistryTest &registry;
    unsigned int key;
    int value;
    std::shared_ptr<int> ptr;
    enum in_method_t { LOOKUP, LOOKUP_OR_CREATE } in_method;

    Thread_wait(SharedPtrRegistryTest& _registry, unsigned int _key, int _value, in_method_t _in_method) : 
      registry(_registry),
      key(_key),
      value(_value),
      in_method(_in_method)
    {
    }
    
    void *entry() override {
      switch(in_method) {
      case LOOKUP_OR_CREATE:
	if (value) 
	  ptr = registry.lookup_or_create<int>(key, value);
	else
	  ptr = registry.lookup_or_create(key);
	break;
      case LOOKUP:
	ptr = std::shared_ptr<int>(new int);
	*ptr = value;
	ptr = registry.lookup(key);
	break;
      }
      return NULL;
    }
  };

  static const useconds_t DELAY_MAX = 20 * 1000 * 1000;
  static useconds_t delay;

  bool wait_for(SharedPtrRegistryTest &registry, int waiting) {
    do {
      //
      // the delay variable is supposed to be initialized to zero. It would be fine
      // to usleep(0) but we take this opportunity to test the loop. It will try 
      // again and therefore show that the logic ( increasing the delay ) actually
      // works. 
      //
      if (delay > 0)
	usleep(delay);
      {
	std::lock_guard l(registry.get_lock());
	if (registry.waiting == waiting) 
	  break;
      }
      if (delay > 0)
	cout << "delay " << delay << "us, is not long enough, try again\n";
    } while (( delay = delay * 2 + 1) < DELAY_MAX);
    return delay < DELAY_MAX;
  }
};

useconds_t SharedPtrRegistry_all::delay = 0;

TEST_F(SharedPtrRegistry_all, lookup_or_create) {
  SharedPtrRegistryTest registry;
  unsigned int key = 1;
  int value = 2;
  std::shared_ptr<int> ptr = registry.lookup_or_create(key);
  *ptr = value;
  ASSERT_EQ(value, *registry.lookup_or_create(key));
}

TEST_F(SharedPtrRegistry_all, wait_lookup_or_create) {
  SharedPtrRegistryTest registry;

  //
  // simulate the following: The last reference to a shared_ptr goes
  // out of scope and the shared_ptr object is about to be removed and
  // marked as such. The weak_ptr stored in the registry will show
  // that it has expired(). However, the SharedPtrRegistry::OnRemoval
  // object has not yet been called and did not get a chance to
  // acquire the lock. The lookup_or_create and lookup methods must
  // detect that situation and wait until the weak_ptr is removed from
  // the registry.
  //
  {
    unsigned int key = 1;
    {
      std::shared_ptr<int> ptr(new int);
      registry.get_contents()[key] = make_pair(ptr, ptr.get());
    }
    EXPECT_FALSE(registry.get_contents()[key].first.lock());

    Thread_wait t(registry, key, 0, Thread_wait::LOOKUP_OR_CREATE);
    t.create("wait_lookcreate");
    ASSERT_TRUE(wait_for(registry, 1));
    EXPECT_FALSE(t.ptr);
    // waiting on a key does not block lookups on other keys
    EXPECT_TRUE(registry.lookup_or_create(key + 12345).get());
    registry.remove(key);
    ASSERT_TRUE(wait_for(registry, 0));
    t.join();
    EXPECT_TRUE(t.ptr.get());
  }
  {
    unsigned int key = 2;
    int value = 3;
    {
      std::shared_ptr<int> ptr(new int);
      registry.get_contents()[key] = make_pair(ptr, ptr.get());
    }
    EXPECT_FALSE(registry.get_contents()[key].first.lock());

    Thread_wait t(registry, key, value, Thread_wait::LOOKUP_OR_CREATE);
    t.create("wait_lookcreate");
    ASSERT_TRUE(wait_for(registry, 1));
    EXPECT_FALSE(t.ptr);
    // waiting on a key does not block lookups on other keys
    {
      int other_value = value + 1;
      unsigned int other_key = key + 1;
      std::shared_ptr<int> ptr = registry.lookup_or_create<int>(other_key, other_value);
      EXPECT_TRUE(ptr.get());
      EXPECT_EQ(other_value, *ptr);
    }
    registry.remove(key);
    ASSERT_TRUE(wait_for(registry, 0));
    t.join();
    EXPECT_TRUE(t.ptr.get());
    EXPECT_EQ(value, *t.ptr);
  }
}

TEST_F(SharedPtrRegistry_all, lookup) {
  SharedPtrRegistryTest registry;
  unsigned int key = 1;
  {
    std::shared_ptr<int> ptr = registry.lookup_or_create(key);
    int value = 2;
    *ptr = value;
    ASSERT_EQ(value, *registry.lookup(key));
  }
  ASSERT_FALSE(registry.lookup(key));
}

TEST_F(SharedPtrRegistry_all, wait_lookup) {
  SharedPtrRegistryTest registry;

  unsigned int key = 1;
  int value = 2;
  {
    std::shared_ptr<int> ptr(new int);
    registry.get_contents()[key] = make_pair(ptr, ptr.get());
  }
  EXPECT_FALSE(registry.get_contents()[key].first.lock());

  Thread_wait t(registry, key, value, Thread_wait::LOOKUP);
  t.create("wait_lookup");
  ASSERT_TRUE(wait_for(registry, 1));
  EXPECT_EQ(value, *t.ptr);
  // waiting on a key does not block lookups on other keys
  EXPECT_FALSE(registry.lookup(key + 12345));
  registry.remove(key);
  ASSERT_TRUE(wait_for(registry, 0));
  t.join();
  EXPECT_FALSE(t.ptr);
}

TEST_F(SharedPtrRegistry_all, get_next) {

  {
    SharedPtrRegistry<unsigned int,int> registry;
    const unsigned int key = 0;
    pair<unsigned int, int> i;
    EXPECT_FALSE(registry.get_next(key, &i));
  }
  {
    SharedPtrRegistryTest registry;

    const unsigned int key2 = 333;
    std::shared_ptr<int> ptr2 = registry.lookup_or_create(key2);
    const int value2 = *ptr2 = 400;

    // entries with expired pointers are silentely ignored
    const unsigned int key_gone = 222;
    registry.get_contents()[key_gone] = make_pair(std::shared_ptr<int>(), (int*)0);

    const unsigned int key1 = 111;
    std::shared_ptr<int> ptr1 = registry.lookup_or_create(key1);
    const int value1 = *ptr1 = 800;

    pair<unsigned int, int> i;
    EXPECT_TRUE(registry.get_next(i.first, &i));
    EXPECT_EQ(key1, i.first);
    EXPECT_EQ(value1, i.second);

    EXPECT_TRUE(registry.get_next(i.first, &i));
    EXPECT_EQ(key2, i.first);
    EXPECT_EQ(value2, i.second);

    EXPECT_FALSE(registry.get_next(i.first, &i));
  }
  {
    //
    // http://tracker.ceph.com/issues/6117
    // reproduce the issue.
    //
    SharedPtrRegistryTest registry;
    const unsigned int key1 = 111;
    std::shared_ptr<int> *ptr1 = new std::shared_ptr<int>(registry.lookup_or_create(key1));
    const unsigned int key2 = 222;
    std::shared_ptr<int> ptr2 = registry.lookup_or_create(key2);
    
    pair<unsigned int, std::shared_ptr<int> > i;
    EXPECT_TRUE(registry.get_next(i.first, &i));
    EXPECT_EQ(key1, i.first);
    delete ptr1;
    EXPECT_TRUE(registry.get_next(i.first, &i));    
    EXPECT_EQ(key2, i.first);
  }
}

TEST_F(SharedPtrRegistry_all, remove) {
  {
    SharedPtrRegistryTest registry;
    const unsigned int key1 = 1;
    std::shared_ptr<int> ptr1 = registry.lookup_or_create(key1);
    *ptr1 = 400;
    registry.remove(key1);

    std::shared_ptr<int> ptr2 = registry.lookup_or_create(key1);
    *ptr2 = 500;

    ptr1 = std::shared_ptr<int>();
    std::shared_ptr<int> res = registry.lookup(key1);
    ceph_assert(res);
    ceph_assert(res == ptr2);
    ceph_assert(*res == 500);
  }
  {
    SharedPtrRegistryTest registry;
    const unsigned int key1 = 1;
    std::shared_ptr<int> ptr1 = registry.lookup_or_create(key1, 400);
    registry.remove(key1);

    std::shared_ptr<int> ptr2 = registry.lookup_or_create(key1, 500);

    ptr1 = std::shared_ptr<int>();
    std::shared_ptr<int> res = registry.lookup(key1);
    ceph_assert(res);
    ceph_assert(res == ptr2);
    ceph_assert(*res == 500);
  }
}

class SharedPtrRegistry_destructor : public ::testing::Test {
public:

  typedef enum { UNDEFINED, YES, NO } DieEnum;
  static DieEnum died;

  struct TellDie {
    TellDie() { died = NO; }
    ~TellDie() { died = YES; }
    
    int value = 0;
  };

  void SetUp() override {
    died = UNDEFINED;
  }
};

SharedPtrRegistry_destructor::DieEnum SharedPtrRegistry_destructor::died = SharedPtrRegistry_destructor::UNDEFINED;

TEST_F(SharedPtrRegistry_destructor, destructor) {
  SharedPtrRegistry<int,TellDie> registry;
  EXPECT_EQ(UNDEFINED, died);
  int key = 101;
  {
    std::shared_ptr<TellDie> a = registry.lookup_or_create(key);
    EXPECT_EQ(NO, died);
    EXPECT_TRUE(a.get());
  }
  EXPECT_EQ(YES, died);
  EXPECT_FALSE(registry.lookup(key));
}

// Local Variables:
// compile-command: "cd ../.. ; make unittest_sharedptr_registry && ./unittest_sharedptr_registry # --gtest_filter=*.* --log-to-stderr=true"
// End: