Skip to content

Commit 0a7e003

Browse files
this one is for you Nuno
- pull request might have new bugs given that build is broken. - this test doesn't expose race conditions under simple tests, yet. It is a starting point. - run under cuzz (app-verifier) should expose races, this is what it was made for.
1 parent 96e871c commit 0a7e003

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/test/scoped_timer.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
// test driver for scoped timer.
22
// fixes are required to be fuzzed
33
// with single and multi-threaded mode and short timeouts.
4+
// run it with app-verifier (becuzz yes ...)
45

56
#include "util/scoped_timer.h"
7+
#include "util/util.h"
8+
#include "util/vector.h"
9+
#include "util/trace.h"
10+
#include <thread>
11+
12+
class test_scoped_eh : public event_handler {
13+
std::atomic<bool> m_called = false;
14+
public:
15+
void operator()(event_handler_caller_t id) override {
16+
m_caller_id = id;
17+
m_called = true;
18+
}
19+
bool called() const { return m_called; }
20+
};
21+
22+
static void worker_thread(unsigned tid) {
23+
for (unsigned i = 0; i < 100; ++i) {
24+
test_scoped_eh eh;
25+
scoped_timer sc(1, &eh);
26+
unsigned_vector v;
27+
for (unsigned j = 0; j < (2 << 25); ++j) {
28+
v.push_back(j);
29+
if (eh.called()) {
30+
// IF_VERBOSE(0, verbose_stream() << tid << " " << i << " " << j << "\n");
31+
break;
32+
}
33+
}
34+
}
35+
}
636

737
void tst_scoped_timer() {
38+
39+
std::cout << "sequential test\n";
40+
worker_thread(0);
41+
42+
std::cout << "thread test\n";
43+
unsigned num_threads = 3;
44+
vector<std::thread> threads(num_threads);
45+
for (unsigned i = 0; i < num_threads; ++i)
46+
threads[i] = std::thread([&, i]() { worker_thread(i); });
47+
48+
for (auto& th : threads)
49+
th.join();
850

951
}

0 commit comments

Comments
 (0)