Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/async #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions include/turtle/detail/expectation_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// http://www.boost.org/LICENSE_1_0.txt)

#include "matcher_base_template.hpp"
#include <atomic>

#define MOCK_EXPECTATION_INITIALIZE(z, n, d) \
BOOST_PP_COMMA_IF(n) c##n##_( c##n )
Expand Down Expand Up @@ -133,7 +134,10 @@ namespace detail
> () )
, file_( "unknown location" )
, line_( 0 )
{}
#if defined(MOCK_THREAD_SAFE)
, blocked(false)
#endif
{ }
expectation( const char* file, int line )
: invocation_( boost::make_shared< unlimited >() )
, matcher_(
Expand All @@ -144,6 +148,19 @@ namespace detail
> () )
, file_( file )
, line_( line )
#if defined(MOCK_THREAD_SAFE)
, blocked(false)
#endif
{ }

expectation(expectation && e)
: invocation_ ( e.invocation_)
, matcher_(e.matcher_)
, file_(e.file_)
, line_(e.line_)
#if defined(MOCK_THREAD_SAFE)
, blocked(false)
#endif
{}

~expectation()
Expand Down Expand Up @@ -185,7 +202,15 @@ namespace detail
}
#endif
#endif

#if defined(MOCK_THREAD_SAFE)
template < typename duration>
expectation &async(const duration timeout)
{
timeout_ = timeout;
blocked.store(false,std::memory_order_release);
return *this;
}
#endif
void add( sequence& s )
{
s.impl_->add( this );
Expand All @@ -196,16 +221,41 @@ namespace detail
{
return invocation_->verify();
}

#if defined(MOCK_THREAD_SAFE)
bool verify(const boost::shared_ptr< condition_variable> &cv, lock &lk) const
{
if (timeout_)
{
while (!invocation_->verify())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this blocking until all the expectations are fulfilled or time out ?
What happens for other objects expectations ? Won't they sometimes succeed when they should have timed out ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is the idea. This determine if the objects expectations are met before the time out occurs. If you write that a function is expected to be called twice, then the function would wait for the function to be called twice until the timeout occurs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this can only be used with one expectation because it will block on that expectation ? What if test mock object has two methods with two expectations ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't have time to work on this earlier. But here is an answer: it also works. As proof you can look in the test_async.cpp file for a demonstration of this.

Every expectation can have a time out value associated with it. The delaying of the execution is done inside the expectation verification.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is this will pass whereas it should have failed

BOOST_FIXTURE_TEST_CASE( mock_object_asynchonous_call_expectation_in_sequence, mock_error_fixture )
{
#if defined(MOCK_ASYNC)
    const mock_class m{};
    MOCK_EXPECT( m.my_tag ).async(MOCK_THREAD_NAMESPACE::chrono::milliseconds(50)).once().with( "some parameter" );
    MOCK_EXPECT( m.my_tag ).async(MOCK_THREAD_NAMESPACE::chrono::milliseconds(5)).once().with( "some parameter2" );
    MOCK_THREAD_NAMESPACE::thread context([&](){
        MOCK_THREAD_NAMESPACE::this_thread::sleep_for(MOCK_THREAD_NAMESPACE::chrono::milliseconds(10));
        m.my_method("some parameter");
        m.my_method("some parameter2");
    });
    mock::verify();
    CHECK_CALLS( 2 );
    context.join();
#endif
}

and this will fail whereas it should have succeeded

BOOST_FIXTURE_TEST_CASE( mock_object_asynchonous_call_expectation_in_sequence, mock_error_fixture )
{
#if defined(MOCK_ASYNC)
    const mock_class m{};
    MOCK_EXPECT( m.my_tag ).async(MOCK_THREAD_NAMESPACE::chrono::milliseconds(50)).once().with( "some parameter" );
    MOCK_EXPECT( m.my_tag ).async(MOCK_THREAD_NAMESPACE::chrono::milliseconds(20)).once().with( "some parameter2" );
    MOCK_THREAD_NAMESPACE::thread context([&](){
        MOCK_THREAD_NAMESPACE::this_thread::sleep_for(MOCK_THREAD_NAMESPACE::chrono::milliseconds(10));
        m.my_method("some parameter2");
        MOCK_THREAD_NAMESPACE::this_thread::sleep_for(MOCK_THREAD_NAMESPACE::chrono::milliseconds(20));
        m.my_method("some parameter");
    });
    mock::verify();
    CHECK_CALLS( 2 );
    context.join();
#endif
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well sorry, actually the second one will pass with the current implementation, I was already thinking that first testing the timeout before the expectation completion would not work either.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I get your point. The difficulty is actually making clear which time reference point is taken for checking the async expectation. I'll try to come up with something.

{
if (MOCK_THREAD_NAMESPACE::cv_status::timeout == cv->wait_for(lk.get_unique_lock(), *timeout_))
{
blocked.store(true, std::memory_order_release);
return false;
}
}
}
return invocation_->verify();
}
#endif
bool is_valid(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, a) ) const
{
#if defined(MOCK_THREAD_SAFE)
return !blocked.load(std::memory_order_acquire) && !invocation_->exhausted()
&& (*matcher_)( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, a) );
#else
return !invocation_->exhausted()
&& (*matcher_)( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, a) );
#endif
}

bool invoke() const
{
#if defined(MOCK_THREAD_SAFE)
if (blocked.load(std::memory_order_acquire))
return false;
#endif
for( sequences_cit it = sequences_.begin();
it != sequences_.end(); ++it )
if( ! (*it)->is_valid( this ) )
Expand Down Expand Up @@ -252,6 +302,10 @@ namespace detail
sequences_type sequences_;
const char* file_;
int line_;
#if defined(MOCK_THREAD_SAFE)
boost::optional<nanoseconds> timeout_;
mutable std::atomic<bool> blocked;
#endif
};
}
} // mock
Expand Down
13 changes: 13 additions & 0 deletions include/turtle/detail/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ namespace detail
: e_( &e )
{}

wrapper_base( wrapper_base && w )
: e_( w.e_ )
{}

template< typename T >
void returns( T t )
{
Expand All @@ -64,6 +68,11 @@ namespace detail
: e_( &e )
{}

wrapper_base( wrapper_base && w )
: e_( w.e_ )
{}


E* e_;
};
template< typename R, typename E >
Expand All @@ -73,6 +82,10 @@ namespace detail
: e_( &e )
{}

wrapper_base( wrapper_base && w )
: e_( w.e_ )
{}

void returns( R* r )
{
e_->returns( r );
Expand Down
104 changes: 77 additions & 27 deletions include/turtle/detail/function_impl_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// http://www.boost.org/LICENSE_1_0.txt)

#include "expectation_template.hpp"
#include <boost/move/utility.hpp>
#include <atomic>

#ifndef MOCK_ERROR_POLICY
# error no error policy has been set
Expand Down Expand Up @@ -44,28 +46,42 @@ namespace detail
: context_( 0 )
, valid_( true )
, mutex_( boost::make_shared< mutex >() )
#if defined(MOCK_THREAD_SAFE)
, cv_( boost::make_shared< condition_variable>() )
#endif
{}
virtual ~function_impl()
{
if( valid_ && ! std::uncaught_exception() )
for( expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it )
if( ! it->verify() )
error_type::fail( "untriggered expectation",
boost::unit_test::lazy_ostream::instance()
<< lazy_context( this )
<< lazy_expectations( this ),
it->file(), it->line() );
if( context_ )
context_->remove( *this );
if (valid_ && !std::uncaught_exception()){
lock _(mutex_);
for (expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it)
#if defined(MOCK_THREAD_SAFE)
if (!it->verify(cv_, _))
#else
if (!it->verify())
#endif
error_type::fail("untriggered expectation",
boost::unit_test::lazy_ostream::instance()
<< lazy_context(this)
<< lazy_expectations(this),
it->file(), it->line());
}
context *c = context_.load(std::memory_order_acquire);
if (c)
c->remove( *this );
}

virtual bool verify() const
{
lock _( mutex_ );
for( expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it )
#if defined(MOCK_THREAD_SAFE)
if( ! it->verify(cv_,_) )
#else
if( ! it->verify() )
#endif
{
valid_ = false;
error_type::fail( "verification failed",
Expand Down Expand Up @@ -93,40 +109,52 @@ namespace detail

struct wrapper : wrapper_base< R, expectation_type >
{
private:
BOOST_MOVABLE_BUT_NOT_COPYABLE(wrapper);

public:

wrapper( const boost::shared_ptr< mutex >& m, expectation_type& e )
: wrapper_base< R, expectation_type >( e )
, lock_( m )
{}

wrapper once()
wrapper( BOOST_RV_REF(wrapper) w)
: wrapper_base< R, expectation_type > (*w.e_)
, lock_( std::move(w.lock_) )
{

}

wrapper &once()
{
this->e_->invoke( boost::make_shared< detail::once >() );
return *this;
}
wrapper never()
wrapper &never()
{
this->e_->invoke( boost::make_shared< detail::never >() );
return *this;
}
wrapper exactly( std::size_t count )
wrapper &exactly( std::size_t count )
{
this->e_->invoke(
boost::make_shared< detail::exactly >( count ) );
return *this;
}
wrapper at_least( std::size_t min )
wrapper &at_least( std::size_t min )
{
this->e_->invoke(
boost::make_shared< detail::at_least >( min ) );
return *this;
}
wrapper at_most( std::size_t max )
wrapper &at_most( std::size_t max )
{
this->e_->invoke(
boost::make_shared< detail::at_most >( max ) );
return *this;
}
wrapper between( std::size_t min, std::size_t max )
wrapper &between( std::size_t min, std::size_t max )
{
this->e_->invoke(
boost::make_shared< detail::between >( min, max ) );
Expand All @@ -137,7 +165,7 @@ namespace detail
template<
BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, typename Constraint_)
>
wrapper with(
wrapper &with(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, Constraint_, c) )
{
this->e_->with(
Expand All @@ -146,7 +174,7 @@ namespace detail
}
#if MOCK_NUM_ARGS > 1
template< typename Constraint >
wrapper with( const Constraint& c )
wrapper &with( const Constraint& c )
{
this->e_->with( c );
return *this;
Expand All @@ -158,7 +186,7 @@ namespace detail
this->e_->add( s##n );

#define MOCK_FUNCTION_IN(z, n, d) \
wrapper in( BOOST_PP_ENUM_PARAMS(n, sequence& s) ) \
wrapper &in( BOOST_PP_ENUM_PARAMS(n, sequence& s) ) \
{ \
BOOST_PP_REPEAT(n, MOCK_FUNCTION_IN_ADD, _) \
return *this; \
Expand All @@ -169,7 +197,13 @@ namespace detail

#undef MOCK_FUNCTION_IN
#undef MOCK_FUNCTION_IN_ADD

#if defined(MOCK_THREAD_SAFE)
wrapper &async(const nanoseconds &timeout)
{
this->e_->async(timeout);
return *this;
}
#endif
template< typename TT >
void calls( TT t )
{
Expand Down Expand Up @@ -211,6 +245,19 @@ namespace detail
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, t) ) const
{
lock _( mutex_ );
#if defined(MOCK_THREAD_SAFE)
struct notify_cv_on_exit
{
notify_cv_on_exit(condition_variable &cv)
: cv(cv){}
~notify_cv_on_exit()
{
cv.notify_one();
}
condition_variable &cv;
};
notify_cv_on_exit _cv(*cv_);
#endif
valid_ = false;
for( expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it )
Expand Down Expand Up @@ -246,11 +293,9 @@ namespace detail
boost::optional< type_name > type,
boost::unit_test::const_string name )
{
lock _( mutex_ );
if( ! context_ )
if (!context_.exchange(&c,std::memory_order_release))
c.add( *this );
c.add( p, *this, instance, type, name );
context_ = &c;
}

friend std::ostream& operator<<(
Expand All @@ -268,8 +313,10 @@ namespace detail
friend std::ostream& operator<<(
std::ostream& s, const lazy_context& c )
{
if( c.impl_->context_ )
c.impl_->context_->serialize( s, *c.impl_ );

context *pContext = c.impl_->context_.load(std::memory_order_acquire);
if( pContext )
pContext->serialize( s, *c.impl_ );
else
s << '?';
return s;
Expand Down Expand Up @@ -297,9 +344,12 @@ namespace detail
typedef typename expectations_type::const_iterator expectations_cit;

expectations_type expectations_;
context* context_;
std::atomic<context*> context_;
mutable bool valid_;
const boost::shared_ptr< mutex > mutex_;
#if defined(MOCK_THREAD_SAFE)
const boost::shared_ptr<condition_variable> cv_;
#endif
};
}
} // mock
Expand Down
Loading