Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
actor-incubator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
actor-incubator
Commits
ffa21887
Commit
ffa21887
authored
Aug 27, 2019
by
Jakob Otto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test for udp_datagram_socket
parent
712dddb2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
+89
-0
libcaf_net/test/udp_datagram_socket.cpp
libcaf_net/test/udp_datagram_socket.cpp
+89
-0
No files found.
libcaf_net/test/udp_datagram_socket.cpp
0 → 100644
View file @
ffa21887
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE udp_datagram_socket
#include "caf/net/udp_datagram_socket.hpp"
#include "caf/test/dsl.hpp"
#include "host_fixture.hpp"
#include "caf/detail/net_syscall.hpp"
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ip_address.hpp"
using
namespace
caf
;
using
namespace
caf
::
net
;
namespace
{
expected
<
udp_datagram_socket
>
make_socket
()
{
CAF_NET_SYSCALL
(
"socket"
,
fd
,
==
,
invalid_socket_id
,
::
socket
(
AF_INET6
,
SOCK_DGRAM
,
0
));
return
udp_datagram_socket
{
fd
};
}
struct
fixture
:
host_fixture
{
actor_system_config
cfg
;
actor_system
sys
{
cfg
};
};
constexpr
string_view
hello_test
=
"Hello test!"
;
}
// namespace
CAF_TEST_FIXTURE_SCOPE
(
udp_datagram_socket_test
,
fixture
)
CAF_TEST
(
send
and
receive
)
{
std
::
vector
<
byte
>
buf
(
1024
);
ip_endpoint
ep
;
if
(
auto
err
=
detail
::
parse
(
"[::1]:55555"
,
ep
))
CAF_FAIL
(
"unable to parse input: "
<<
err
);
auto
sender
=
unbox
(
make_socket
());
auto
receiver
=
unbox
(
make_socket
());
auto
guard
=
detail
::
make_scope_guard
([
&
]
{
close
(
socket_cast
<
net
::
socket
>
(
sender
));
close
(
socket_cast
<
net
::
socket
>
(
receiver
));
});
if
(
auto
err
=
bind
(
receiver
,
ep
))
CAF_FAIL
(
"unable to bind socket"
<<
err
);
if
(
nonblocking
(
socket_cast
<
net
::
socket
>
(
receiver
),
true
))
CAF_FAIL
(
"nonblocking failed"
);
auto
test_read_res
=
read
(
receiver
,
make_span
(
buf
));
if
(
auto
err
=
get_if
<
sec
>
(
&
test_read_res
))
CAF_CHECK_EQUAL
(
*
err
,
sec
::
unavailable_or_would_block
);
else
CAF_FAIL
(
"read should have failed"
);
auto
write_ret
=
write
(
sender
,
as_bytes
(
make_span
(
hello_test
)),
ep
);
if
(
auto
num_bytes
=
get_if
<
size_t
>
(
&
write_ret
))
CAF_CHECK_EQUAL
(
*
num_bytes
,
hello_test
.
size
());
else
CAF_FAIL
(
"write returned an error: "
<<
sys
.
render
(
get
<
sec
>
(
write_ret
)));
auto
read_ret
=
read
(
receiver
,
make_span
(
buf
));
if
(
auto
read_res
=
get_if
<
std
::
pair
<
size_t
,
ip_endpoint
>>
(
&
read_ret
))
{
CAF_CHECK_EQUAL
(
read_res
->
first
,
hello_test
.
size
());
buf
.
resize
(
read_res
->
first
);
}
else
{
CAF_FAIL
(
"write returned an error: "
<<
sys
.
render
(
get
<
sec
>
(
read_ret
)));
}
string_view
buf_view
{
reinterpret_cast
<
const
char
*>
(
buf
.
data
()),
buf
.
size
()};
CAF_CHECK_EQUAL
(
buf_view
,
hello_test
);
}
CAF_TEST_FIXTURE_SCOPE_END
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment