-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.cpp
More file actions
155 lines (114 loc) · 3.57 KB
/
Copy pathmain_test.cpp
File metadata and controls
155 lines (114 loc) · 3.57 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
#include "log_container.h"
#include "log_container_iterator.h"
#include "log_formatter.h"
#include "util.h"
#include <iostream>
#include <string>
#include <cassert>
#include <thread>
#include <chrono>
#include <sstream>
#include <mutex>
#include <locale>
class thread_test {
private:
log_container *log_;
std::mutex lock_;
std::vector<std::vector<std::string>> out_;
public:
thread_test(log_container *log): log_(log) {}
void write_thread() {
for (uint64_t i = 1; i <= 100; i++) {
log_->append(string_record(random_string(100) + int_to_string(i)));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
void read_thread() {
std::vector<std::string> records;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
for (log_container_iterator itr = log_->begin(); itr != log_->end(); itr++) {
records.push_back(log_formatter::to_string(*itr));
}
std::lock_guard<std::mutex> lock(lock_);
out_.push_back(records);
}
std::vector<std::vector<std::string>> get_out() const {
return std::move(out_);
}
};
bool check_read_data(std::vector<std::vector<std::string>> &&out) {
for (auto &vec : out) {
uint64_t prev_count = 0;
for (auto &e : vec) {
size_t pos = e.find_first_of("0123456789");
if (pos == std::string::npos) {
return false;
}
std::string counter = e.substr(pos, e.size() - pos);
uint64_t count = string_to_int(counter);
if (count <= prev_count) {
return false;
}
prev_count = count;
}
}
return true;
}
void multithreading_test() {
log_container log("system_log");
thread_test t(&log);
std::vector<std::thread> workers;
// create write thread
workers.push_back(std::thread(&thread_test::write_thread, &t));
// create multiple read threads
for (uint64_t i = 0; i < 10; i++) {
workers.push_back(std::thread(&thread_test::read_thread, &t));
}
// wait for all threads to finish
for (auto& thread : workers) {
thread.join();
}
// check read data
assert(check_read_data(t.get_out()) == true);
// empty log store
uint64_t pos = log.get_position();
log.truncate(pos-1);
assert(log.get_size() == 0);
}
void basic_test() {
std::string out;
log_container log("system_log");
log.append(string_record("hello_how_are_you"));
log.append(string_record("_great"));
log.append(string_record("_amazing"));
log.append(string_record("_yes"));
assert(log.get_size() == 4);
out.clear();
for (log_container_iterator itr = log.begin(); itr != log.end(); itr++) {
out += log_formatter::to_string(*itr);
}
assert(out == "hello_how_are_you_great_amazing_yes");
log.truncate(0);
assert(log.get_size() == 3);
out.clear();
for (log_container_iterator itr = log.begin(); itr != log.end(); itr++) {
out += log_formatter::to_string(*itr);
}
assert(out == "_great_amazing_yes");
log.append(string_record("_wonderful"));
assert(log.get_size() == 4);
out.clear();
for (log_container_iterator itr = log.begin(); itr != log.end(); itr++) {
out += log_formatter::to_string(*itr);
}
assert(out == "_great_amazing_yes_wonderful");
// empty log store
uint64_t pos = log.get_position();
log.truncate(pos-1);
assert(log.get_size() == 0);
}
int main() {
basic_test();
multithreading_test();
return 0;
}