Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
471d443e
Commit
471d443e
authored
3 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
tests: add multithread test
parent
ba9a4452
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/dhtrunnertester.cpp
+37
-0
37 additions, 0 deletions
tests/dhtrunnertester.cpp
tests/dhtrunnertester.h
+5
-0
5 additions, 0 deletions
tests/dhtrunnertester.h
with
42 additions
and
0 deletions
tests/dhtrunnertester.cpp
+
37
−
0
View file @
471d443e
...
...
@@ -19,6 +19,8 @@
#include
"dhtrunnertester.h"
#include
<opendht/thread_pool.h>
#include
<chrono>
#include
<mutex>
#include
<condition_variable>
...
...
@@ -298,4 +300,39 @@ DhtRunnerTester::testListenLotOfBytes() {
node3
.
cancelListen
(
foo
,
ftokenfoo
.
get
());
}
void
DhtRunnerTester
::
testMultithread
()
{
std
::
mutex
mutex
;
std
::
condition_variable
cv
;
unsigned
putCount
(
0
);
unsigned
putOkCount
(
0
);
constexpr
unsigned
N
=
2048
;
for
(
unsigned
i
=
0
;
i
<
N
;
i
++
)
{
dht
::
ThreadPool
::
computation
().
run
([
&
]{
node2
.
put
(
dht
::
InfoHash
::
get
(
"123"
+
std
::
to_string
(
i
)),
"hehe"
,
[
&
](
bool
ok
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex
);
putCount
++
;
if
(
ok
)
putOkCount
++
;
cv
.
notify_all
();
});
node2
.
get
(
dht
::
InfoHash
::
get
(
"123"
+
std
::
to_string
(
N
-
i
-
1
)),
[](
const
std
::
shared_ptr
<
dht
::
Value
>&
){
return
true
;
},
[
&
](
bool
ok
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex
);
putCount
++
;
if
(
ok
)
putOkCount
++
;
cv
.
notify_all
();
});
});
}
std
::
unique_lock
<
std
::
mutex
>
lk
(
mutex
);
CPPUNIT_ASSERT
(
cv
.
wait_for
(
lk
,
30s
,
[
&
]{
return
putCount
==
2
*
N
;
}));
CPPUNIT_ASSERT_EQUAL
(
2
*
N
,
putOkCount
);
}
}
// namespace test
This diff is collapsed.
Click to expand it.
tests/dhtrunnertester.h
+
5
−
0
View file @
471d443e
...
...
@@ -67,6 +67,11 @@ class DhtRunnerTester : public CppUnit::TestFixture {
* Test listen method with lot of datas
*/
void
testListenLotOfBytes
();
/**
* Test multithread
*/
void
testMultithread
();
};
}
// namespace test
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment