Skip to content
GitLab
Explore
Sign in
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
32a443a1
Commit
32a443a1
authored
6 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
thread pool: add unit test
parent
6d0fcba3
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CMakeLists.txt
+2
-0
2 additions, 0 deletions
CMakeLists.txt
tests/threadpooltester.cpp
+90
-0
90 additions, 0 deletions
tests/threadpooltester.cpp
tests/threadpooltester.h
+48
-0
48 additions, 0 deletions
tests/threadpooltester.h
with
140 additions
and
0 deletions
CMakeLists.txt
+
2
−
0
View file @
32a443a1
...
...
@@ -321,6 +321,8 @@ if (OPENDHT_TESTS)
tests/dhtrunnertester.cpp
tests/peerdiscoverytester.h
tests/peerdiscoverytester.cpp
tests/threadpooltester.h
tests/threadpooltester.cpp
)
if
(
OPENDHT_PROXY_SERVER AND OPENDHT_PROXY_CLIENT
)
list
(
APPEND test_FILES
...
...
This diff is collapsed.
Click to expand it.
tests/threadpooltester.cpp
0 → 100644
+
90
−
0
View file @
32a443a1
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include
"threadpooltester.h"
#include
"opendht/thread_pool.h"
#include
<atomic>
namespace
test
{
CPPUNIT_TEST_SUITE_REGISTRATION
(
ThreadPoolTester
);
using
clock
=
std
::
chrono
::
steady_clock
;
void
ThreadPoolTester
::
setUp
()
{
}
void
ThreadPoolTester
::
testThreadPool
()
{
dht
::
ThreadPool
pool
(
16
);
constexpr
unsigned
N
=
64
*
1024
;
std
::
atomic_uint
count
{
0
};
for
(
unsigned
i
=
0
;
i
<
N
;
i
++
)
pool
.
run
([
&
]
{
count
++
;
});
auto
start
=
clock
::
now
();
while
(
count
.
load
()
!=
N
&&
clock
::
now
()
-
start
<
std
::
chrono
::
seconds
(
10
))
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
pool
.
join
();
CPPUNIT_ASSERT
(
count
.
load
()
==
N
);
}
void
ThreadPoolTester
::
testExecutor
()
{
dht
::
ThreadPool
pool
(
8
);
auto
executor1
=
std
::
make_shared
<
dht
::
Executor
>
(
pool
,
1
);
auto
executor4
=
std
::
make_shared
<
dht
::
Executor
>
(
pool
,
4
);
auto
executor8
=
std
::
make_shared
<
dht
::
Executor
>
(
pool
,
8
);
constexpr
unsigned
N
=
64
*
1024
;
std
::
atomic_uint
count1
{
0
};
std
::
atomic_uint
count4
{
0
};
std
::
atomic_uint
count8
{
0
};
for
(
unsigned
i
=
0
;
i
<
N
;
i
++
)
{
executor1
->
run
([
&
]
{
count1
++
;
});
executor4
->
run
([
&
]
{
count4
++
;
});
executor8
->
run
([
&
]
{
count8
++
;
});
}
auto
start
=
clock
::
now
();
while
((
count1
.
load
()
!=
N
||
count4
.
load
()
!=
N
||
count8
.
load
()
!=
N
)
&&
clock
::
now
()
-
start
<
std
::
chrono
::
seconds
(
20
))
{
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
10
));
}
executor1
.
reset
();
executor4
.
reset
();
executor8
.
reset
();
CPPUNIT_ASSERT_EQUAL
(
N
,
count1
.
load
());
CPPUNIT_ASSERT_EQUAL
(
N
,
count4
.
load
());
CPPUNIT_ASSERT_EQUAL
(
N
,
count8
.
load
());
}
void
ThreadPoolTester
::
tearDown
()
{
}
}
// namespace test
This diff is collapsed.
Click to expand it.
tests/threadpooltester.h
0 → 100644
+
48
−
0
View file @
32a443a1
/*
* Copyright (C) 2019 Savoir-faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
// cppunit
#include
<cppunit/TestFixture.h>
#include
<cppunit/extensions/HelperMacros.h>
namespace
test
{
class
ThreadPoolTester
:
public
CppUnit
::
TestFixture
{
CPPUNIT_TEST_SUITE
(
ThreadPoolTester
);
CPPUNIT_TEST
(
testThreadPool
);
CPPUNIT_TEST
(
testExecutor
);
CPPUNIT_TEST_SUITE_END
();
public:
/**
* Method automatically called before each test by CppUnit
*/
void
setUp
();
/**
* Method automatically called after each test CppUnit
*/
void
tearDown
();
void
testThreadPool
();
void
testExecutor
();
};
}
// 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