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
7558ca0d
Commit
7558ca0d
authored
6 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
peerdiscovery: unit test cleanup
parent
38141322
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/peerdiscoverytester.cpp
+59
-54
59 additions, 54 deletions
tests/peerdiscoverytester.cpp
with
59 additions
and
54 deletions
tests/peerdiscoverytester.cpp
+
59
−
54
View file @
7558ca0d
...
@@ -22,16 +22,14 @@
...
@@ -22,16 +22,14 @@
namespace
test
{
namespace
test
{
class
NodeInsertion
{
struct
NodeInsertion
{
public:
dht
::
InfoHash
nodeid
;
dht
::
InfoHash
nodeid_
;
in_port_t
node_port
;
in_port_t
node_port_
;
dht
::
NetId
nid
;
dht
::
NetId
nid_
;
MSGPACK_DEFINE
(
nodeid
,
node_port
,
nid
)
MSGPACK_DEFINE
(
nodeid_
,
node_port_
,
nid_
)
};
};
class
TestPack
{
struct
TestPack
{
public:
int
num
;
int
num
;
char
cha
;
char
cha
;
std
::
string
str
;
std
::
string
str
;
...
@@ -42,63 +40,70 @@ CPPUNIT_TEST_SUITE_REGISTRATION(PeerDiscoveryTester);
...
@@ -42,63 +40,70 @@ CPPUNIT_TEST_SUITE_REGISTRATION(PeerDiscoveryTester);
void
PeerDiscoveryTester
::
setUp
(){}
void
PeerDiscoveryTester
::
setUp
(){}
void
PeerDiscoveryTester
::
testTransmission
()
{
void
PeerDiscoveryTester
::
testTransmission
()
{
// Node for getnode id
// Node for getnode id
const
std
::
string
type
{
"dht"
};
const
std
::
string
type
{
"dht"
};
const
std
::
string
test_type
{
"pdd"
};
const
std
::
string
test_type
{
"pdd"
};
dht
::
InfoHash
data_n
=
dht
::
InfoHash
::
get
(
"applepin"
);
constexpr
int
MULTICAST_PORT
=
2222
;
int
port
=
2222
;
in_port_t
port_n
=
50000
;
msgpack
::
sbuffer
sbuf
;
NodeInsertion
adc
;
NodeInsertion
adc
;
adc
.
nid_
=
10
;
adc
.
nid
=
10
;
adc
.
node_port_
=
port_n
;
adc
.
node_port
=
50000
;
adc
.
nodeid_
=
data_n
;
adc
.
nodeid
=
dht
::
InfoHash
::
get
(
"applepin"
);
msgpack
::
pack
(
sbuf
,
adc
);
msgpack
::
sbuffer
pbuf
;
TestPack
pdd
;
TestPack
pdd
;
pdd
.
num
=
100
;
pdd
.
num
=
100
;
pdd
.
cha
=
'a'
;
pdd
.
cha
=
'a'
;
pdd
.
str
=
"apple"
;
pdd
.
str
=
"apple"
;
msgpack
::
pack
(
pbuf
,
pdd
);
dht
::
PeerDiscovery
test_n
(
MULTICAST_PORT
);
try
{
dht
::
PeerDiscovery
test_s
(
MULTICAST_PORT
);
dht
::
PeerDiscovery
test_n
(
port
);
dht
::
PeerDiscovery
test_s
(
port
);
std
::
mutex
lock
;
try
{
std
::
condition_variable
cv
;
test_s
.
startDiscovery
(
type
,[
&
](
msgpack
::
object
&&
obj
,
dht
::
SockAddr
&&
add
){
std
::
unique_lock
<
std
::
mutex
>
l
(
lock
);
auto
v
=
obj
.
as
<
NodeInsertion
>
();
unsigned
count_node
{
0
};
CPPUNIT_ASSERT_EQUAL
(
v
.
node_port_
,
port_n
);
unsigned
count_test
{
0
};
CPPUNIT_ASSERT_EQUAL
(
v
.
nodeid_
,
data_n
);
test_s
.
startDiscovery
<
NodeInsertion
>
(
type
,[
&
](
NodeInsertion
&&
v
,
dht
::
SockAddr
&&
add
){
CPPUNIT_ASSERT_EQUAL
(
v
.
node_port
,
adc
.
node_port
);
CPPUNIT_ASSERT_EQUAL
(
v
.
nodeid
,
adc
.
nodeid
);
CPPUNIT_ASSERT_EQUAL
(
v
.
nid
,
adc
.
nid
);
{
std
::
lock_guard
<
std
::
mutex
>
l
(
lock
);
count_node
++
;
}
cv
.
notify_all
();
});
});
test_s
.
startDiscovery
(
test_type
,[
&
](
msgpack
::
object
&&
obj
,
dht
::
SockAddr
&&
add
){
test_s
.
startDiscovery
(
test_type
,[
&
](
msgpack
::
object
&&
obj
,
dht
::
SockAddr
&&
add
){
auto
v
=
obj
.
as
<
TestPack
>
();
auto
v
=
obj
.
as
<
TestPack
>
();
CPPUNIT_ASSERT_EQUAL
(
v
.
num
,
100
);
CPPUNIT_ASSERT_EQUAL
(
v
.
num
,
pdd
.
num
);
CPPUNIT_ASSERT_EQUAL
(
v
.
cha
,
'a'
);
CPPUNIT_ASSERT_EQUAL
(
v
.
cha
,
pdd
.
cha
);
CPPUNIT_ASSERT_EQUAL
(
v
.
str
,
pdd
.
str
);
{
std
::
lock_guard
<
std
::
mutex
>
l
(
lock
);
count_test
++
;
}
cv
.
notify_all
();
});
});
test_n
.
startPublish
(
type
,
sbuf
);
test_n
.
startPublish
(
type
,
adc
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
5
));
CPPUNIT_ASSERT
(
cv
.
wait_for
(
l
,
std
::
chrono
::
seconds
(
5
),
[
&
]{
test_n
.
startPublish
(
test_type
,
pbuf
);
return
count_node
>
0
;
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
5
));
}));
test_n
.
stopPublish
(
test_type
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
10
));
test_n
.
startPublish
(
test_type
,
pdd
);
test_n
.
stop
();
CPPUNIT_ASSERT
(
cv
.
wait_for
(
l
,
std
::
chrono
::
seconds
(
5
),
[
&
]{
test_s
.
stop
();
return
count_node
>
1
and
count_test
>
0
;
test_n
.
join
();
}));
test_s
.
join
();
l
.
unlock
();
}
catch
(
std
::
exception
&
exception
){
perror
(
exception
.
what
());
test_n
.
stopPublish
(
type
);
CPPUNIT_ASSERT
(
false
);
test_n
.
stopPublish
(
test_type
);
}
test_n
.
stopDiscovery
(
type
);
}
catch
(
std
::
exception
&
exception
){
test_n
.
stopDiscovery
(
test_type
);
perror
(
exception
.
what
());
}
}
}
void
PeerDiscoveryTester
::
tearDown
(){}
void
PeerDiscoveryTester
::
tearDown
(){}
...
...
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