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 all 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
6 changes: 6 additions & 0 deletions include/turtle/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@
# endif
#endif

#if defined(MOCK_RVALUE_REFERENCES) && defined(MOCK_THREAD_SAFE)
# ifndef MOCK_NO_ASYNC
# define MOCK_ASYNC
# endif
#endif

#endif // MOCK_CONFIG_HPP_INCLUDED
6 changes: 3 additions & 3 deletions include/turtle/detail/action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace detail
value_imp( BOOST_RV_REF( value_type ) t )
: t_( boost::move( t ) )
{}
value_imp( const T& t )
value_imp( const typename boost::remove_reference<T>::type & t )
: t_( t )
{}
template< typename Y >
Expand All @@ -150,10 +150,10 @@ namespace detail
return static_cast< value_imp< T >& >( *v_ ).t_;
}
template< typename T >
Result& store( T* t )
typename boost::remove_reference<Result>::type & store( T* t )
{
v_.reset( new value_imp< Result >( t ) );
return static_cast< value_imp< Result >& >( *v_ ).t_;
return static_cast< value_imp< typename boost::remove_reference< Result >::type >& >( *v_ ).t_;
}

boost::shared_ptr< value > v_;
Expand Down
71 changes: 68 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 <boost/atomic.hpp>

#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_ASYNC)
, blocked(false)
#endif
{ }
expectation( const char* file, int line )
: invocation_( boost::make_shared< unlimited >() )
, matcher_(
Expand All @@ -144,8 +148,32 @@ namespace detail
> () )
, file_( file )
, line_( line )
#if defined(MOCK_ASYNC)
, blocked(false)
#endif
{ }

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

expectation &operator=(BOOST_RV_REF(expectation) e)
{
invocation_ = e.invocation_;
matcher_ =e.matcher_;
file_ = e.file_;
line_ = e.line_;
#if defined(MOCK_ASYNC)
blocked.store(false, boost::memory_order_release);
#endif
}

~expectation()
{
for( sequences_cit it = sequences_.begin();
Expand Down Expand Up @@ -185,7 +213,15 @@ namespace detail
}
#endif
#endif

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

#if defined(MOCK_ASYNC)
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, boost::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_ASYNC)
return !blocked.load(boost::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_ASYNC)
if (blocked.load(boost::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 +313,10 @@ namespace detail
sequences_type sequences_;
const char* file_;
int line_;
#if defined(MOCK_ASYNC)
boost::optional<nanoseconds> timeout_;
mutable boost::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( BOOST_RV_REF(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( BOOST_RV_REF(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( BOOST_RV_REF(wrapper_base) w )
: e_( w.e_ )
{}

void returns( R* r )
{
e_->returns( r );
Expand Down
Loading