forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedHashMapTest.cc
More file actions
166 lines (142 loc) · 5.46 KB
/
Copy pathSynchronizedHashMapTest.cc
File metadata and controls
166 lines (142 loc) · 5.46 KB
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <gtest/gtest.h>
#include <algorithm>
#include <atomic>
#include <boost/optional/optional_io.hpp>
#include <chrono>
#include <thread>
#include <vector>
#include "lib/Latch.h"
#include "lib/SynchronizedHashMap.h"
using namespace pulsar;
using SyncMapType = SynchronizedHashMap<int, int>;
using OptValue = typename SyncMapType::OptValue;
using PairVector = typename SyncMapType::PairVector;
inline void sleepMs(long millis) { std::this_thread::sleep_for(std::chrono::milliseconds(millis)); }
inline PairVector sort(PairVector pairs) {
std::sort(pairs.begin(), pairs.end(), [](const std::pair<int, int>& lhs, const std::pair<int, int>& rhs) {
return lhs.first < rhs.first;
});
return pairs;
}
TEST(SynchronizedHashMap, testClear) {
SyncMapType m({{1, 100}, {2, 200}});
m.clear();
ASSERT_EQ(m.toPairVector(), PairVector{});
PairVector expectedPairs({{3, 300}, {4, 400}});
SyncMapType m2(expectedPairs);
PairVector pairs;
m2.clear([&pairs](const int& key, const int& value) { pairs.emplace_back(key, value); });
ASSERT_EQ(m2.toPairVector(), PairVector{});
ASSERT_EQ(sort(pairs), expectedPairs);
}
TEST(SynchronizedHashMap, testRemoveAndFind) {
SyncMapType m({{1, 100}, {2, 200}, {3, 300}});
OptValue optValue;
optValue = m.findFirstValueIf([](const int& x) { return x == 200; });
ASSERT_TRUE(optValue);
ASSERT_EQ(optValue.value(), 200);
optValue = m.findFirstValueIf([](const int& x) { return x >= 301; });
ASSERT_FALSE(optValue);
optValue = m.find(1);
ASSERT_TRUE(optValue);
ASSERT_EQ(optValue.value(), 100);
ASSERT_FALSE(m.find(0));
ASSERT_FALSE(m.remove(0));
optValue = m.remove(1);
ASSERT_TRUE(optValue);
ASSERT_EQ(optValue.value(), 100);
ASSERT_FALSE(m.remove(1));
ASSERT_FALSE(m.find(1));
}
TEST(SynchronizedHashMapTest, testForEach) {
SyncMapType m({{1, 100}, {2, 200}, {3, 300}});
std::vector<int> values;
m.forEachValue([&values](const int& value) { values.emplace_back(value); });
std::sort(values.begin(), values.end());
ASSERT_EQ(values, std::vector<int>({100, 200, 300}));
PairVector pairs;
m.forEach([&pairs](const int& key, const int& value) { pairs.emplace_back(key, value); });
PairVector expectedPairs({{1, 100}, {2, 200}, {3, 300}});
ASSERT_EQ(sort(pairs), expectedPairs);
m.clear();
int result = 0;
values.clear();
m.forEachValue([&values](int value, SharedFuture) { values.emplace_back(value); },
[&result] { result = 1; });
ASSERT_TRUE(values.empty());
ASSERT_EQ(result, 1);
ASSERT_EQ(m.putIfAbsent(1, 100), boost::none);
ASSERT_EQ(m.putIfAbsent(1, 101), boost::optional<int>(100));
m.forEachValue([&values](int value, SharedFuture) { values.emplace_back(value); },
[&result] { result = 2; });
ASSERT_EQ(values, (std::vector<int>({100})));
ASSERT_EQ(result, 1);
m.put(1, 102);
values.clear();
m.forEachValue([&values](int value, SharedFuture) { values.emplace_back(value); },
[&result] { result = 2; });
ASSERT_EQ(values, (std::vector<int>({102})));
ASSERT_EQ(result, 1);
values.clear();
ASSERT_EQ(m.putIfAbsent(2, 200), boost::none);
ASSERT_EQ(m.putIfAbsent(2, 201), boost::optional<int>(200));
m.forEachValue([&values](int value, SharedFuture) { values.emplace_back(value); },
[&result] { result = 2; });
std::sort(values.begin(), values.end());
ASSERT_EQ(values, (std::vector<int>({102, 200})));
ASSERT_EQ(result, 1);
}
TEST(SynchronizedHashMap, testRecursiveMutex) {
SyncMapType m({{1, 100}});
OptValue optValue;
m.forEach([&m, &optValue](const int& key, const int& value) {
optValue = m.find(key); // the internal mutex was locked again
});
ASSERT_TRUE(optValue);
ASSERT_EQ(optValue.value(), 100);
}
TEST(SynchronizedHashMapTest, testThreadSafeForEach) {
SyncMapType m({{1, 100}, {2, 200}, {3, 300}});
Latch latch(1);
std::thread t{[&m, &latch] {
latch.wait(); // this thread must start after `m.forEach` started
m.remove(2);
}};
std::atomic_bool firstElementDone{false};
PairVector pairs;
m.forEach([&latch, &firstElementDone, &pairs](const int& key, const int& value) {
pairs.emplace_back(key, value);
if (!firstElementDone) {
latch.countdown();
firstElementDone = true;
}
sleepMs(200);
});
{
PairVector expectedPairs({{1, 100}, {2, 200}, {3, 300}});
ASSERT_EQ(sort(pairs), expectedPairs);
}
t.join();
{
PairVector expectedPairs({{1, 100}, {3, 300}});
ASSERT_EQ(sort(m.toPairVector()), expectedPairs);
}
}