]> git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/contrib/pzstd/utils/test/ResourcePoolTest.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / zstd / contrib / pzstd / utils / test / ResourcePoolTest.cpp
1 /**
2 * Copyright (c) 2016-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9 #include "utils/ResourcePool.h"
10
11 #include <gtest/gtest.h>
12 #include <atomic>
13 #include <thread>
14
15 using namespace pzstd;
16
17 TEST(ResourcePool, FullTest) {
18 unsigned numCreated = 0;
19 unsigned numDeleted = 0;
20 {
21 ResourcePool<int> pool(
22 [&numCreated] { ++numCreated; return new int{5}; },
23 [&numDeleted](int *x) { ++numDeleted; delete x; });
24
25 {
26 auto i = pool.get();
27 EXPECT_EQ(5, *i);
28 *i = 6;
29 }
30 {
31 auto i = pool.get();
32 EXPECT_EQ(6, *i);
33 auto j = pool.get();
34 EXPECT_EQ(5, *j);
35 *j = 7;
36 }
37 {
38 auto i = pool.get();
39 EXPECT_EQ(6, *i);
40 auto j = pool.get();
41 EXPECT_EQ(7, *j);
42 }
43 }
44 EXPECT_EQ(2, numCreated);
45 EXPECT_EQ(numCreated, numDeleted);
46 }
47
48 TEST(ResourcePool, ThreadSafe) {
49 std::atomic<unsigned> numCreated{0};
50 std::atomic<unsigned> numDeleted{0};
51 {
52 ResourcePool<int> pool(
53 [&numCreated] { ++numCreated; return new int{0}; },
54 [&numDeleted](int *x) { ++numDeleted; delete x; });
55 auto push = [&pool] {
56 for (int i = 0; i < 100; ++i) {
57 auto x = pool.get();
58 ++*x;
59 }
60 };
61 std::thread t1{push};
62 std::thread t2{push};
63 t1.join();
64 t2.join();
65
66 auto x = pool.get();
67 auto y = pool.get();
68 EXPECT_EQ(200, *x + *y);
69 }
70 EXPECT_GE(2, numCreated);
71 EXPECT_EQ(numCreated, numDeleted);
72 }