-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
81 lines (64 loc) · 1.69 KB
/
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
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
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE LoggerTestModule
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <chrono>
#include <random>
#include <stdexcept>
#include "logger.h"
BOOST_AUTO_TEST_SUITE(LoggerTestSuite)
int countFileLines(std::string filePath) {
// check lines
int count = 0;
std::string line;
std::ifstream file(filePath);
while (getline(file, line))
count++;
return count;
}
MessageType getRandomMsgType() {
std::random_device rd;
std::uniform_int_distribution<int> dist2(0, (int)MessageType::ERROR);
return (MessageType)dist2(rd);
}
void threadFunc(Logger* log, int id) {
log->info("Thread " + std::to_string(id) + " begins");
// each thread produces 100000 messages
for (int i = 0;i < 100000 - 2; i++) {
MessageType msgType = getRandomMsgType();
log->message(msgType,
"Thread " + std::to_string(id) + " Result from message " + std::to_string(i));
}
log->info("Thread " + std::to_string(id) + " ends ");
}
BOOST_AUTO_TEST_CASE(logger_test1) {
BOOST_CHECK_EQUAL(1, 1);
}
BOOST_AUTO_TEST_CASE(logger_test2)
{
const std::string filePath = "myfile.log";
Logger *log;
try {
log = new Logger(filePath);
} catch (const std::exception& e) {
BOOST_FAIL( e.what() );
}
log->info("Main Thread Begins");
std::vector<std::thread> threads;
const int numThreads = 20;
for (int i = 0; i < numThreads; i++) {
threads.push_back(std::thread(threadFunc, log, i));
}
log->info("Main Thread Point 1");
for(auto &t : threads){
t.join();
}
log->info("Main Thread ends");
delete log;
int count = countFileLines(filePath);
BOOST_CHECK_EQUAL(count, 2000003);
}
BOOST_AUTO_TEST_SUITE_END()