Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
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
Operations
Operations
Metrics
Environments
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 Framework
Commits
33b0914b
Commit
33b0914b
authored
Sep 26, 2016
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix error handling
parent
cc6e1751
Changes
16
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
169 additions
and
195 deletions
+169
-195
examples/curl/curl_fuse.cpp
examples/curl/curl_fuse.cpp
+13
-8
examples/message_passing/calculator.cpp
examples/message_passing/calculator.cpp
+0
-1
examples/message_passing/fixed_stack.cpp
examples/message_passing/fixed_stack.cpp
+2
-2
examples/remoting/distributed_calculator.cpp
examples/remoting/distributed_calculator.cpp
+10
-14
examples/remoting/remote_spawn.cpp
examples/remoting/remote_spawn.cpp
+28
-28
libcaf_core/src/blocking_actor.cpp
libcaf_core/src/blocking_actor.cpp
+6
-0
libcaf_core/test/actor_pool.cpp
libcaf_core/test/actor_pool.cpp
+1
-1
libcaf_core/test/typed_spawn.cpp
libcaf_core/test/typed_spawn.cpp
+2
-3
libcaf_io/caf/io/network/test_multiplexer.hpp
libcaf_io/caf/io/network/test_multiplexer.hpp
+1
-1
libcaf_io/src/basp_broker.cpp
libcaf_io/src/basp_broker.cpp
+24
-31
libcaf_io/src/middleman.cpp
libcaf_io/src/middleman.cpp
+10
-14
libcaf_io/src/test_multiplexer.cpp
libcaf_io/src/test_multiplexer.cpp
+4
-4
libcaf_io/test/basp.cpp
libcaf_io/test/basp.cpp
+1
-1
libcaf_io/test/http_broker.cpp
libcaf_io/test/http_broker.cpp
+1
-1
libcaf_test/caf/test/unit_test.hpp
libcaf_test/caf/test/unit_test.hpp
+25
-36
libcaf_test/caf/test/unit_test_impl.hpp
libcaf_test/caf/test/unit_test_impl.hpp
+41
-50
No files found.
examples/curl/curl_fuse.cpp
View file @
33b0914b
...
...
@@ -112,10 +112,11 @@ struct base_state {
return
aout
(
self
)
<<
color
<<
name
<<
" (id = "
<<
self
->
id
()
<<
"): "
;
}
virtual
void
init
(
std
::
string
m_name
,
std
::
string
m_color
)
{
virtual
bool
init
(
std
::
string
m_name
,
std
::
string
m_color
)
{
name
=
std
::
move
(
m_name
);
color
=
std
::
move
(
m_color
);
print
()
<<
"started"
<<
color
::
reset_endl
;
return
true
;
}
virtual
~
base_state
()
{
...
...
@@ -129,7 +130,8 @@ struct base_state {
// encapsulates an HTTP request
behavior
client_job
(
stateful_actor
<
base_state
>*
self
,
actor
parent
)
{
self
->
state
.
init
(
"client-job"
,
color
::
blue
);
if
(
!
self
->
state
.
init
(
"client-job"
,
color
::
blue
))
return
{};
// returning an empty behavior terminates the actor
self
->
send
(
parent
,
read_atom
::
value
,
"http://www.example.com/index.html"
,
uint64_t
{
0
},
uint64_t
{
4095
});
...
...
@@ -165,7 +167,8 @@ struct client_state : base_state {
behavior
client
(
stateful_actor
<
client_state
>*
self
,
actor
parent
)
{
using
std
::
chrono
::
milliseconds
;
self
->
link_to
(
parent
);
self
->
state
.
init
(
"client"
,
color
::
green
);
if
(
!
self
->
state
.
init
(
"client"
,
color
::
green
))
return
{};
// returning an empty behavior terminates the actor
self
->
send
(
self
,
next_atom
::
value
);
return
{
[
=
](
next_atom
)
{
...
...
@@ -201,13 +204,13 @@ struct curl_state : base_state {
return
size
;
}
void
init
(
std
::
string
m_name
,
std
::
string
m_color
)
override
{
bool
init
(
std
::
string
m_name
,
std
::
string
m_color
)
override
{
curl
=
curl_easy_init
();
if
(
!
curl
)
throw
std
::
runtime_error
(
"Unable initialize CURL."
)
;
return
false
;
curl_easy_setopt
(
curl
,
CURLOPT_WRITEFUNCTION
,
&
curl_state
::
callback
);
curl_easy_setopt
(
curl
,
CURLOPT_NOSIGNAL
,
1
);
base_state
::
init
(
std
::
move
(
m_name
),
std
::
move
(
m_color
));
return
base_state
::
init
(
std
::
move
(
m_name
),
std
::
move
(
m_color
));
}
CURL
*
curl
=
nullptr
;
...
...
@@ -216,7 +219,8 @@ struct curl_state : base_state {
// manages a CURL session
behavior
curl_worker
(
stateful_actor
<
curl_state
>*
self
,
actor
parent
)
{
self
->
state
.
init
(
"curl-worker"
,
color
::
yellow
);
if
(
!
self
->
state
.
init
(
"curl-worker"
,
color
::
yellow
))
return
{};
// returning an empty behavior terminates the actor
return
{
[
=
](
read_atom
,
const
std
::
string
&
fname
,
uint64_t
offset
,
uint64_t
range
)
->
message
{
...
...
@@ -282,7 +286,8 @@ struct master_state : base_state {
};
behavior
curl_master
(
stateful_actor
<
master_state
>*
self
)
{
self
->
state
.
init
(
"curl-master"
,
color
::
magenta
);
if
(
!
self
->
state
.
init
(
"curl-master"
,
color
::
magenta
))
return
{};
// returning an empty behavior terminates the actor
// spawn workers
for
(
size_t
i
=
0
;
i
<
num_curl_workers
;
++
i
)
self
->
state
.
idle
.
push_back
(
self
->
spawn
<
detached
+
linked
>
(
curl_worker
,
self
));
...
...
examples/message_passing/calculator.cpp
View file @
33b0914b
...
...
@@ -117,7 +117,6 @@ void tester(scoped_actor& self, const Handle& hdl, int x, int y, Ts&&... xs) {
auto
handle_err
=
[
&
](
const
error
&
err
)
{
aout
(
self
)
<<
"AUT (actor under test) failed: "
<<
self
->
system
().
render
(
err
)
<<
endl
;
throw
std
::
runtime_error
(
"AUT responded with an error"
);
};
// first test: x + y = z
self
->
request
(
hdl
,
infinite
,
add_atom
::
value
,
x
,
y
).
receive
(
...
...
examples/message_passing/fixed_stack.cpp
View file @
33b0914b
#include <cassert>
#include <cstdint>
#include <iostream>
#include "caf/all.hpp"
...
...
@@ -58,8 +59,7 @@ public:
}
behavior
make_behavior
()
override
{
if
(
size_
<
2
)
throw
std
::
runtime_error
(
"size < 2 is not supported for fixed_stack"
);
assert
(
size_
<
2
);
return
empty_
;
}
...
...
examples/remoting/distributed_calculator.cpp
View file @
33b0914b
...
...
@@ -262,21 +262,17 @@ void client_repl(actor_system& system, const config& cfg) {
},
[
&
](
string
&
arg0
,
string
&
arg1
,
string
&
arg2
)
{
if
(
arg0
==
"connect"
)
{
try
{
auto
lport
=
std
::
stoul
(
arg2
);
if
(
lport
<
std
::
numeric_limits
<
uint16_t
>::
max
())
{
char
*
end
=
nullptr
;
auto
lport
=
strtoul
(
arg2
.
c_str
(),
&
end
,
10
);
if
(
end
!=
arg2
.
c_str
()
+
arg2
.
size
())
cout
<<
"
\"
"
<<
arg2
<<
"
\"
is not an unsigned integer"
<<
endl
;
else
if
(
lport
>
std
::
numeric_limits
<
uint16_t
>::
max
())
cout
<<
"
\"
"
<<
arg2
<<
"
\"
> "
<<
std
::
numeric_limits
<
uint16_t
>::
max
()
<<
endl
;
else
anon_send
(
client
,
connect_atom
::
value
,
move
(
arg1
),
static_cast
<
uint16_t
>
(
lport
));
}
else
{
cout
<<
lport
<<
" is not a valid port"
<<
endl
;
}
}
catch
(
std
::
exception
&
)
{
cout
<<
"
\"
"
<<
arg2
<<
"
\"
is not an unsigned integer"
<<
endl
;
}
}
else
{
auto
x
=
toint
(
arg0
);
auto
op
=
plus_or_minus
(
arg1
);
...
...
examples/remoting/remote_spawn.cpp
View file @
33b0914b
...
...
@@ -39,18 +39,18 @@ using calculator = typed_actor<replies_to<add_atom, int, int>::with<int>,
calculator
::
behavior_type
calculator_fun
(
calculator
::
pointer
self
)
{
return
{
[
=
](
add_atom
,
int
a
,
int
b
)
->
int
{
aout
(
self
)
<<
"received task from a remote node"
<<
std
::
endl
;
aout
(
self
)
<<
"received task from a remote node"
<<
endl
;
return
a
+
b
;
},
[
=
](
sub_atom
,
int
a
,
int
b
)
->
int
{
aout
(
self
)
<<
"received task from a remote node"
<<
std
::
endl
;
aout
(
self
)
<<
"received task from a remote node"
<<
endl
;
return
a
-
b
;
}
};
}
// removes leading and trailing whitespaces
string
trim
(
st
d
::
st
ring
s
)
{
string
trim
(
string
s
)
{
auto
not_space
=
[](
char
c
)
{
return
!
isspace
(
c
);
};
// trim left
s
.
erase
(
s
.
begin
(),
find_if
(
s
.
begin
(),
s
.
end
(),
not_space
));
...
...
@@ -65,15 +65,13 @@ void client_repl(function_view<calculator> f) {
cout
<<
"Usage:"
<<
endl
<<
" quit : terminate program"
<<
endl
<<
" <x> + <y> : adds two integers"
<<
endl
<<
" <x> - <y> : subtracts two integers"
<<
endl
<<
endl
;
<<
" <x> - <y> : subtracts two integers"
<<
endl
<<
endl
;
};
usage
();
// read next line, split it, and evaluate user input
string
line
;
while
(
std
::
getline
(
std
::
cin
,
line
))
{
line
=
trim
(
std
::
move
(
line
));
// ignore leading and trailing whitespaces
if
(
line
==
"quit"
)
if
((
line
=
trim
(
std
::
move
(
line
)))
==
"quit"
)
return
;
std
::
vector
<
string
>
words
;
split
(
words
,
line
,
is_any_of
(
" "
),
token_compress_on
);
...
...
@@ -81,18 +79,20 @@ void client_repl(function_view<calculator> f) {
usage
();
continue
;
}
try
{
auto
x
=
stoi
(
words
[
0
])
;
auto
y
=
stoi
(
words
[
2
]
);
if
(
words
[
1
]
==
"+"
)
std
::
cout
<<
"= "
<<
f
(
add_atom
::
value
,
x
,
y
)
<<
std
::
endl
;
else
if
(
words
[
1
]
==
"-"
)
std
::
cout
<<
"= "
<<
f
(
sub_atom
::
value
,
x
,
y
)
<<
std
::
endl
;
else
usage
(
);
}
catch
(...)
{
auto
to_int
=
[](
const
string
&
str
)
->
optional
<
int
>
{
char
*
end
=
nullptr
;
auto
res
=
strtol
(
str
.
c_str
(),
&
end
,
10
);
if
(
end
==
str
.
c_str
()
+
str
.
size
()
)
return
static_cast
<
int
>
(
res
)
;
return
none
;
}
;
auto
x
=
to_int
(
words
[
0
]);
auto
y
=
to_int
(
words
[
2
]
);
if
(
!
x
||
!
y
||
(
words
[
1
]
!=
"+"
&&
words
[
1
]
!=
"-"
))
usage
();
}
else
cout
<<
" = "
<<
(
words
[
1
]
==
"+"
?
f
(
add_atom
::
value
,
*
x
,
*
y
)
:
f
(
sub_atom
::
value
,
*
x
,
*
y
))
<<
"
\n
"
;
}
}
...
...
@@ -105,28 +105,28 @@ struct config : actor_system_config {
.
add
(
server_mode
,
"server-mode,s"
,
"enable server mode"
);
}
uint16_t
port
=
0
;
st
d
::
st
ring
host
=
"localhost"
;
string
host
=
"localhost"
;
bool
server_mode
=
false
;
};
void
server
(
actor_system
&
system
,
const
config
&
cfg
)
{
auto
res
=
system
.
middleman
().
open
(
cfg
.
port
);
if
(
!
res
)
{
std
::
cerr
<<
"*** cannot open port: "
<<
system
.
render
(
res
.
error
())
<<
std
::
endl
;
cerr
<<
"*** cannot open port: "
<<
system
.
render
(
res
.
error
())
<<
endl
;
return
;
}
std
::
cout
<<
"*** running on port: "
<<
*
res
<<
std
::
endl
<<
"*** press <enter> to shutdown server"
<<
std
::
endl
;
cout
<<
"*** running on port: "
<<
*
res
<<
endl
<<
"*** press <enter> to shutdown server"
<<
endl
;
getchar
();
}
void
client
(
actor_system
&
system
,
const
config
&
cfg
)
{
auto
node
=
system
.
middleman
().
connect
(
cfg
.
host
,
cfg
.
port
);
if
(
!
node
)
{
std
::
cerr
<<
"*** connect failed: "
<<
system
.
render
(
node
.
error
())
<<
std
::
endl
;
cerr
<<
"*** connect failed: "
<<
system
.
render
(
node
.
error
())
<<
endl
;
return
;
}
auto
type
=
"calculator"
;
// type of the actor we wish to spawn
...
...
@@ -135,8 +135,8 @@ void client(actor_system& system, const config& cfg) {
auto
worker
=
system
.
middleman
().
remote_spawn
<
calculator
>
(
*
node
,
type
,
args
,
tout
);
if
(
!
worker
)
{
std
::
cerr
<<
"*** remote spawn failed: "
<<
system
.
render
(
worker
.
error
())
<<
std
::
endl
;
cerr
<<
"*** remote spawn failed: "
<<
system
.
render
(
worker
.
error
())
<<
endl
;
return
;
}
// start using worker in main loop
...
...
libcaf_core/src/blocking_actor.cpp
View file @
33b0914b
...
...
@@ -86,6 +86,7 @@ void blocking_actor::launch(execution_unit*, bool, bool hide) {
CAF_ASSERT
(
dynamic_cast
<
blocking_actor
*>
(
this_ptr
)
!=
0
);
auto
self
=
static_cast
<
blocking_actor
*>
(
this_ptr
);
error
rsn
;
# ifndef CAF_NO_EXCEPTIONS
try
{
self
->
act
();
rsn
=
self
->
fail_state_
;
...
...
@@ -99,6 +100,11 @@ void blocking_actor::launch(execution_unit*, bool, bool hide) {
catch
(...)
{
// simply ignore exception
}
# else
self
->
act
();
rsn
=
self
->
fail_state_
;
self
->
on_exit
();
# endif
self
->
cleanup
(
std
::
move
(
rsn
),
self
->
context
());
ptr
->
home_system
->
dec_detached_threads
();
},
ctrl
()).
detach
();
...
...
libcaf_core/test/actor_pool.cpp
View file @
33b0914b
...
...
@@ -79,7 +79,7 @@ struct fixture {
};
void
handle_err
(
const
error
&
err
)
{
throw
std
::
runtime_error
(
"AUT responded with an error: "
+
to_string
(
err
));
CAF_FAIL
(
"AUT responded with an error: "
+
to_string
(
err
));
}
}
// namespace <anonymous>
...
...
libcaf_core/test/typed_spawn.cpp
View file @
33b0914b
...
...
@@ -419,8 +419,7 @@ CAF_TEST(event_testee_series) {
result
=
str
;
},
after
(
chrono
::
minutes
(
1
))
>>
[
&
]
{
CAF_ERROR
(
"event_testee does not reply"
);
throw
runtime_error
(
"event_testee does not reply"
);
CAF_FAIL
(
"event_testee does not reply"
);
}
);
CAF_CHECK_EQUAL
(
result
,
"wait4int"
);
...
...
@@ -448,7 +447,7 @@ CAF_TEST(maybe_string_delegator_chain) {
CAF_MESSAGE
(
"send empty string, expect error"
);
self
->
request
(
aut
,
infinite
,
""
).
receive
(
[](
ok_atom
,
const
string
&
)
{
throw
std
::
logic_error
(
"unexpected result!
"
);
CAF_FAIL
(
"unexpected string response
"
);
},
[](
const
error
&
err
)
{
CAF_CHECK_EQUAL
(
err
.
category
(),
atom
(
"mock"
));
...
...
libcaf_io/caf/io/network/test_multiplexer.hpp
View file @
33b0914b
...
...
@@ -120,7 +120,7 @@ public:
bool
has_pending_scribe
(
std
::
string
host
,
uint16_t
port
);
/// Accepts a pending connect on `hdl`.
void
accept_connection
(
accept_handle
hdl
);
bool
accept_connection
(
accept_handle
hdl
);
/// Reads data from the external input buffer until
/// the configured read policy no longer allows receiving.
...
...
libcaf_io/src/basp_broker.cpp
View file @
33b0914b
...
...
@@ -608,30 +608,22 @@ behavior basp_broker::make_behavior() {
CAF_LOG_WARNING
(
"invalid handle"
);
return
;
}
try
{
assign_tcp_doorman
(
hdl
);
}
catch
(...)
{
CAF_LOG_DEBUG
(
"failed to assign doorman from handle"
);
return
;
}
auto
res
=
assign_tcp_doorman
(
hdl
);
if
(
res
)
{
if
(
whom
)
system
().
registry
().
put
(
whom
->
id
(),
whom
);
state
.
instance
.
add_published_actor
(
port
,
whom
,
std
::
move
(
sigs
));
}
else
{
CAF_LOG_DEBUG
(
"failed to assign doorman from handle"
<<
CAF_ARG
(
whom
));
}
},
// received from middleman actor (delegated)
[
=
](
connect_atom
,
connection_handle
hdl
,
uint16_t
port
)
{
CAF_LOG_TRACE
(
CAF_ARG
(
hdl
.
id
()));
auto
rp
=
make_response_promise
();
try
{
assign_tcp_scribe
(
hdl
);
}
catch
(
std
::
exception
&
e
)
{
CAF_LOG_DEBUG
(
"failed to assign scribe from handle: "
<<
e
.
what
());
CAF_IGNORE_UNUSED
(
e
);
rp
.
deliver
(
sec
::
failed_to_assign_scribe_from_handle
);
return
;
}
auto
res
=
assign_tcp_scribe
(
hdl
);
if
(
res
)
{
auto
&
ctx
=
state
.
ctx
[
hdl
];
ctx
.
hdl
=
hdl
;
ctx
.
remote_port
=
port
;
...
...
@@ -639,6 +631,10 @@ behavior basp_broker::make_behavior() {
ctx
.
callback
=
rp
;
// await server handshake
configure_read
(
hdl
,
receive_policy
::
exactly
(
basp
::
header_size
));
}
else
{
CAF_LOG_DEBUG
(
"failed to assign scribe from handle"
<<
CAF_ARG
(
res
));
rp
.
deliver
(
std
::
move
(
res
.
error
()));
}
},
[
=
](
delete_atom
,
const
node_id
&
nid
,
actor_id
aid
)
{
CAF_LOG_TRACE
(
CAF_ARG
(
nid
)
<<
", "
<<
CAF_ARG
(
aid
));
...
...
@@ -660,13 +656,10 @@ behavior basp_broker::make_behavior() {
// it is well-defined behavior to not have an actor published here,
// hence the result can be ignored safely
state
.
instance
.
remove_published_actor
(
port
,
nullptr
);
try
{
close
(
hdl_by_port
(
port
));
}
catch
(
std
::
exception
&
)
{
return
sec
::
cannot_close_invalid_port
;
}
auto
res
=
close
(
hdl_by_port
(
port
));
if
(
res
)
return
unit
;
return
sec
::
cannot_close_invalid_port
;
},
[
=
](
get_atom
,
const
node_id
&
x
)
->
std
::
tuple
<
node_id
,
std
::
string
,
uint16_t
>
{
...
...
libcaf_io/src/middleman.cpp
View file @
33b0914b
...
...
@@ -236,7 +236,6 @@ strong_actor_ptr middleman::remote_lookup(atom_value name, const node_id& nid) {
auto
basp
=
named_broker
<
basp_broker
>
(
atom
(
"BASP"
));
strong_actor_ptr
result
;
scoped_actor
self
{
system
(),
true
};
try
{
self
->
send
(
basp
,
forward_atom
::
value
,
nid
,
atom
(
"ConfigServ"
),
make_message
(
get_atom
::
value
,
name
));
self
->
receive
(
...
...
@@ -247,9 +246,6 @@ strong_actor_ptr middleman::remote_lookup(atom_value name, const node_id& nid) {
// nop
}
);
}
catch
(
std
::
exception
&
)
{
// nop
}
return
result
;
}
...
...
libcaf_io/src/test_multiplexer.cpp
View file @
33b0914b
...
...
@@ -300,15 +300,15 @@ bool test_multiplexer::has_pending_scribe(std::string x, uint16_t y) {
return
scribes_
.
count
(
std
::
make_pair
(
std
::
move
(
x
),
y
))
>
0
;
}
void
test_multiplexer
::
accept_connection
(
accept_handle
hdl
)
{
bool
test_multiplexer
::
accept_connection
(
accept_handle
hdl
)
{
if
(
passive_mode
(
hdl
))
return
;
return
false
;
auto
&
dd
=
doorman_data_
[
hdl
];
if
(
!
dd
.
ptr
)
throw
std
::
logic_error
(
"accept_connection: this doorman was not "
"assigned to a broker yet"
);
return
false
;
if
(
!
dd
.
ptr
->
new_connection
())
passive_mode
(
hdl
)
=
true
;
return
true
;
}
void
test_multiplexer
::
read_data
(
connection_handle
hdl
)
{
...
...
libcaf_io/test/basp.cpp
View file @
33b0914b
...
...
@@ -282,7 +282,7 @@ public:
auto
hdl
=
n
.
connection
;
mpx_
->
add_pending_connect
(
src
,
hdl
);
mpx_
->
assign_tcp_scribe
(
aut
(),
hdl
);
mpx_
->
accept_connection
(
src
);
CAF_REQUIRE
(
mpx_
->
accept_connection
(
src
)
);
// technically, the server handshake arrives
// before we send the client handshake
mock
(
hdl
,
...
...
libcaf_io/test/http_broker.cpp
View file @
33b0914b
...
...
@@ -182,7 +182,7 @@ public:
// "open" a new connection to our server
mpx_
->
add_pending_connect
(
acceptor_
,
connection_
);
mpx_
->
assign_tcp_scribe
(
aut_ptr_
,
connection_
);
mpx_
->
accept_connection
(
acceptor_
);
CAF_REQUIRE
(
mpx_
->
accept_connection
(
acceptor_
)
);
}
~
fixture
()
{
...
...
libcaf_test/caf/test/unit_test.hpp
View file @
33b0914b
...
...
@@ -120,14 +120,7 @@ public:
namespace
detail
{
// thrown when a required check fails
class
require_error
:
std
::
logic_error
{
public:
require_error
(
const
std
::
string
&
msg
);
require_error
(
const
require_error
&
)
=
default
;
require_error
(
require_error
&&
)
=
default
;
~
require_error
()
noexcept
;
};
[[
noreturn
]]
void
requirement_failed
(
const
std
::
string
&
msg
);
// constructs spacing given a line number.
const
char
*
fill
(
size_t
line
);
...
...
@@ -501,53 +494,49 @@ using caf_test_case_auto_fixture = caf::test::dummy_fixture;
auto CAF_UNIQUE(__str) = CAF_TEST_PRINT_ERROR(msg).str(); \
::caf::test::detail::remove_trailing_spaces(CAF_UNIQUE(__str)); \
::caf::test::engine::current_test()->fail(CAF_UNIQUE(__str), false); \
throw ::caf::test::detail::require_error{"test failure"};
\
} while(false)
::caf::test::detail::requirement_failed("test failure");
\
} while
(false)
#define CAF_REQUIRE(...) \
do { \
auto CAF_UNIQUE(__result) = \
::caf::test::detail::check(::caf::test::engine::current_test(), \
__FILE__, __LINE__, #__VA_ARGS__, false, \
static_cast<bool>(__VA_ARGS__)); \
if (! CAF_UNIQUE(__result)) { \
throw ::caf::test::detail::require_error{#__VA_ARGS__}; \
} \
auto CAF_UNIQUE(__result) = ::caf::test::detail::check( \
::caf::test::engine::current_test(), __FILE__, __LINE__, #__VA_ARGS__, \
false, static_cast<bool>(__VA_ARGS__)); \
if (!CAF_UNIQUE(__result)) \
::caf::test::detail::requirement_failed(#__VA_ARGS__); \
::caf::test::engine::last_check_file(__FILE__); \
::caf::test::engine::last_check_line(__LINE__); \
} while(false)
} while
(false)
#define CAF_REQUIRE_PRED(pred, x_expr, y_expr) \
do { \
const auto& x_val___ = x_expr; \
const auto& y_val___ = y_expr; \
auto CAF_UNIQUE(__result) = \
::caf::test::detail::check(::caf::test::engine::current_test(), \
__FILE__, __LINE__, CAF_PRED_EXPR(pred, x_expr, y_expr), false, \
x_val___ pred y_val___, x_val___, y_val___); \
if (! CAF_UNIQUE(__result)) { \
throw ::caf::test::detail::require_error{ \
CAF_PRED_EXPR(pred, x_expr, y_expr)}; \
} \
auto CAF_UNIQUE(__result) = ::caf::test::detail::check( \
::caf::test::engine::current_test(), __FILE__, __LINE__, \
CAF_PRED_EXPR(pred, x_expr, y_expr), false, x_val___ pred y_val___, \
x_val___, y_val___); \
if (!CAF_UNIQUE(__result)) \
::caf::test::detail::requirement_failed( \
CAF_PRED_EXPR(pred, x_expr, y_expr)); \
::caf::test::engine::last_check_file(__FILE__); \
::caf::test::engine::last_check_line(__LINE__); \
} while(false)
} while
(false)
#define CAF_REQUIRE_FUNC(func, x_expr, y_expr) \
do { \
const auto& x_val___ = x_expr; \
const auto& y_val___ = y_expr; \
auto CAF_UNIQUE(__result) = \
::caf::test::detail::check(::caf::test::engine::current_test(), \
__FILE__, __LINE__, CAF_FUNC_EXPR(func, x_expr, y_expr), false, \
func(x_val___, y_val___), x_val___, y_val___); \
if (! CAF_UNIQUE(__result)) { \
throw ::caf::test::detail::require_error{ \
CAF_FUNC_EXPR(func, x_expr, y_expr)}; \
} \
auto CAF_UNIQUE(__result) = ::caf::test::detail::check( \
::caf::test::engine::current_test(), __FILE__, __LINE__, \
CAF_FUNC_EXPR(func, x_expr, y_expr), false, func(x_val___, y_val___), \
x_val___, y_val___); \
if (!CAF_UNIQUE(__result)) \
::caf::test::detail::requirement_failed( \
CAF_FUNC_EXPR(func, x_expr, y_expr)); \
::caf::test::engine::last_check_file(__FILE__); \
::caf::test::engine::last_check_line(__LINE__); \
} while(false)
} while
(false)
#define CAF_TEST(name) \
namespace { \
...
...
libcaf_test/caf/test/unit_test_impl.hpp
View file @
33b0914b
...
...
@@ -29,6 +29,12 @@
#include <algorithm>
#include <condition_variable>
#include "caf/config.hpp"
#ifndef CAF_WINDOWS
#include <unistd.h>
#endif
#include "caf/message_builder.hpp"
#include "caf/message_handler.hpp"
#include "caf/string_algorithms.hpp"
...
...
@@ -121,24 +127,27 @@ const std::string& test::name() const {
namespace
detail
{
require_error
::
require_error
(
const
std
::
string
&
msg
)
:
std
::
logic_error
(
msg
)
{
// nop
}
require_error
::~
require_error
()
noexcept
{
// nop
[[
noreturn
]]
void
requirement_failed
(
const
std
::
string
&
msg
)
{
auto
&
log
=
logger
::
instance
();
log
.
error
()
<<
engine
::
color
(
red
)
<<
" REQUIRED: "
<<
msg
<<
engine
::
color
(
reset
)
<<
'\n'
<<
" "
<<
engine
::
color
(
blue
)
<<
engine
::
last_check_file
()
<<
engine
::
color
(
yellow
)
<<
":"
<<
engine
::
color
(
cyan
)
<<
engine
::
last_check_line
()
<<
engine
::
color
(
reset
)
<<
detail
::
fill
(
engine
::
last_check_line
())
<<
"had last successful check"
<<
'\n'
;
abort
();
}
const
char
*
fill
(
size_t
line
)
{
if
(
line
<
10
)
{
if
(
line
<
10
)
return
" "
;
}
else
if
(
line
<
100
)
{
else
if
(
line
<
100
)
return
" "
;
}
else
if
(
line
<
1000
)
{
else
if
(
line
<
1000
)
return
" "
;
}
else
{
else
return
" "
;
}
}
void
remove_trailing_spaces
(
std
::
string
&
x
)
{
...
...
@@ -325,9 +334,8 @@ bool engine::run(bool colorize,
size_t
total_bad
=
0
;
size_t
total_bad_expected
=
0
;
auto
bar
=
'+'
+
std
::
string
(
70
,
'-'
)
+
'+'
;
auto
failed_require
=
false
;
# if (! defined(__clang__) && defined(__GNUC__) \
&& __GNUC__ == 4 && __GNUC_MINOR__ < 9) \
#if (!defined(__clang__) && defined(__GNUC__) && __GNUC__ == 4 \
&& __GNUC_MINOR__ < 9) \
|| (defined(__clang__) && !defined(_LIBCPP_VERSION))
// regex implementation is broken prior to 4.9
using
strvec
=
std
::
vector
<
std
::
string
>
;
...
...
@@ -358,12 +366,10 @@ bool engine::run(bool colorize,
std
::
regex
not_suites
;
std
::
regex
not_tests
;
// a default constructored regex matches is not equal to an "empty" regex
if
(
!
not_suites_str
.
empty
())
{
if
(
!
not_suites_str
.
empty
())
not_suites
.
assign
(
not_suites_str
);
}
if
(
!
not_tests_str
.
empty
())
{
if
(
!
not_tests_str
.
empty
())
not_tests
.
assign
(
not_tests_str
);
}
auto
enabled
=
[](
const
std
::
regex
&
whitelist
,
const
std
::
regex
&
blacklist
,
const
std
::
string
&
x
)
{
...
...
@@ -394,11 +400,7 @@ bool engine::run(bool colorize,
<<
'\n'
;
auto
start
=
std
::
chrono
::
high_resolution_clock
::
now
();
watchdog
::
start
(
max_runtime
());
try
{
t
->
run
();
}
catch
(
const
detail
::
require_error
&
)
{
failed_require
=
true
;
}
watchdog
::
stop
();
auto
stop
=
std
::
chrono
::
high_resolution_clock
::
now
();
auto
elapsed
=
...
...
@@ -407,13 +409,6 @@ bool engine::run(bool colorize,
++
total_tests
;
size_t
good
=
t
->
good
();
size_t
bad
=
t
->
bad
();
if
(
failed_require
)
{
log
.
error
()
<<
color
(
red
)
<<
" REQUIRED"
<<
color
(
reset
)
<<
'\n'
<<
" "
<<
color
(
blue
)
<<
last_check_file
()
<<
color
(
yellow
)
<<
":"
<<
color
(
cyan
)
<<
last_check_line
()
<<
color
(
reset
)
<<
detail
::
fill
(
last_check_line
())
<<
"had last successful check"
<<
'\n'
;
}
total_good
+=
good
;
total_bad
+=
bad
;
total_bad_expected
+=
t
->
expected_failures
();
...
...
@@ -431,21 +426,14 @@ bool engine::run(bool colorize,
}
else
{
log
.
verbose
()
<<
'\n'
;
}
if
(
failed_require
)
{
break
;
}
}
// only count suites which have executed one or more tests
if
(
tests_ran
>
0
)
{
++
total_suites
;
}
if
(
displayed_header
)
{
if
(
displayed_header
)
log
.
verbose
()
<<
'\n'
;
}
if
(
failed_require
)
{
break
;
}
}
unsigned
percent_good
=
100
;
if
(
total_bad
>
0
)
{
auto
tmp
=
(
100000.0
*
static_cast
<
double
>
(
total_good
))
...
...
@@ -602,10 +590,19 @@ int main(int argc, char** argv) {
<<
res
.
helptext
<<
std
::
endl
;
return
1
;
}
# if
n
def CAF_WINDOWS
auto
colorize
=
res
.
opts
.
count
(
"no-colors"
)
==
0
;
# ifdef CAF_WINDOWS
constexpr
bool
colorize
=
false
;
# else
auto
colorize
=
false
;
auto
color_support
=
[]()
->
bool
{
if
(
!
isatty
(
0
))
return
false
;
char
ttybuf
[
50
];
if
(
ttyname_r
(
0
,
ttybuf
,
sizeof
(
ttybuf
))
!=
0
)
return
false
;
char
prefix
[]
=
"/dev/tty"
;
return
strncmp
(
prefix
,
ttybuf
,
sizeof
(
prefix
)
-
1
)
==
0
;
};
auto
colorize
=
color_support
();
# endif
std
::
vector
<
char
*>
args
;
if
(
divider
<
argc
)
{
...
...
@@ -630,14 +627,8 @@ int main(int argc, char** argv) {
#ifndef CAF_TEST_NO_MAIN
int
main
(
int
argc
,
char
**
argv
)
{
try
{
return
caf
::
test
::
main
(
argc
,
argv
);
}
catch
(
std
::
exception
&
e
)
{
std
::
cerr
<<
"exception "
<<
typeid
(
e
).
name
()
<<
": "
<<
e
.
what
()
<<
std
::
endl
;
}
return
1
;
}
#endif
#endif
// CAF_TEST_UNIT_TEST_IMPL_HPP
#endif // CAF_TEST_UNIT_TEST_IMPL_HPP
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