-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasync_map_test.cpp
55 lines (45 loc) · 1.66 KB
/
async_map_test.cpp
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
#include <gtest/gtest.h>
#include <pomagma/util/async_map.hpp>
#include <pomagma/util/util.hpp>
#include <pomagma/util/worker_pool.hpp>
namespace pomagma {
namespace {
typedef const std::pair<int, int>* Key;
typedef int Value;
typedef AsyncMap<Key, Value> Cache;
TEST(AsyncMapTest, IsCorrect) {
const size_t thread_count = 10;
const size_t eval_count = 100;
const size_t max_wait = 100;
WorkerPool pool(thread_count);
Cache cache([&pool](Key key, Cache::Callback callback) {
pool.schedule([key, callback] {
Value* value = new Value(key->first + key->second);
std::this_thread::sleep_for(std::chrono::milliseconds(*value));
callback(value);
});
});
std::random_device device;
rng_t rng(device());
std::uniform_int_distribution<> random_int(0, max_wait);
std::atomic<uint_fast64_t> pending_count(eval_count);
for (size_t i = 0; i < eval_count; ++i) {
POMAGMA_INFO("starting task " << i);
Key key = new std::pair<int, int>(random_int(rng), random_int(rng));
auto delay = std::chrono::milliseconds(random_int(rng));
new std::thread([delay, &cache, key, &pending_count, i] {
std::this_thread::sleep_for(delay);
cache.find_async(key, [&pending_count, i](const Value*) {
--pending_count;
POMAGMA_INFO("finished task " << i);
});
});
}
for (size_t periods = 0; pending_count; ++periods) {
EXPECT_LT(periods, 20UL);
POMAGMA_INFO("waiting " << periods);
std::this_thread::sleep_for(std::chrono::milliseconds(max_wait));
}
}
} // namespace
} // namespace pomagma