memgraph/tests/concurrent/conncurent_list.cpp

95 lines
3.3 KiB
C++
Raw Normal View History

2016-09-14 00:53:40 +08:00
#include "common.h"
constexpr size_t THREADS_NO = std::min(max_no_threads, 8);
constexpr size_t key_range = 1e2;
constexpr size_t op_per_thread = 1e4;
2016-09-14 00:53:40 +08:00
// Depending on value there is a possiblity of numerical overflow
constexpr size_t max_number = 10;
constexpr size_t no_find_per_change = 2;
2016-09-14 00:53:40 +08:00
constexpr size_t no_insert_for_one_delete = 1;
2016-12-22 22:51:16 +08:00
// This test simulates behavior of a transactions.
2016-09-14 00:53:40 +08:00
// Each thread makes a series of finds interleaved with method which change.
// Exact ratio of finds per change and insert per delete can be regulated with
// no_find_per_change and no_insert_for_one_delete.
int main()
{
init_log();
memory_check(THREADS_NO, [] {
ConcurrentList<std::pair<int, int>> list;
permanent_assert(list.size() == 0, "The list isn't empty");
2016-09-14 00:53:40 +08:00
auto futures = run<std::pair<long long, long long>>(
THREADS_NO, [&](auto index) mutable {
auto rand = rand_gen(key_range);
2016-09-14 00:53:40 +08:00
auto rand_change = rand_gen_bool(no_find_per_change);
auto rand_delete = rand_gen_bool(no_insert_for_one_delete);
long long sum = 0;
long long count = 0;
2016-09-14 00:53:40 +08:00
for (int i = 0; i < op_per_thread; i++)
{
auto num = rand();
2016-09-14 00:53:40 +08:00
auto data = num % max_number;
if (rand_change())
{
if (rand_delete())
{
for (auto it = list.begin(); it != list.end(); it++)
{
if (it->first == num)
{
if (it.remove())
{
2016-09-14 00:53:40 +08:00
sum -= data;
count--;
}
break;
}
}
}
else
{
2016-09-14 00:53:40 +08:00
list.begin().push(std::make_pair(num, data));
sum += data;
count++;
}
}
else
{
for (auto &v : list)
{
if (v.first == num)
{
2016-09-14 00:53:40 +08:00
permanent_assert(v.second == data,
"Data is invalid");
break;
}
}
}
}
return std::pair<long long, long long>(sum, count);
});
auto it = list.begin();
long long sums = 0;
2016-09-14 00:53:40 +08:00
long long counters = 0;
for (auto &data : collect(futures))
{
2016-09-14 00:53:40 +08:00
sums += data.second.first;
counters += data.second.second;
}
for (auto &e : list)
{
2016-09-14 00:53:40 +08:00
sums -= e.second;
}
2016-09-14 00:53:40 +08:00
permanent_assert(sums == 0, "Same values aren't present");
check_size_list<ConcurrentList<std::pair<int, int>>>(list, counters);
std::this_thread::sleep_for(1s);
2016-09-14 00:53:40 +08:00
});
}