Skip to content

Commit

Permalink
Add unit test for sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
terranpro committed May 21, 2015
1 parent a04ff26 commit 7cdb904
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
17 changes: 5 additions & 12 deletions async-task/Sync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,18 @@
#define AS_SYNC_HPP

#include "Executor.hpp"
#include "Async.hpp"

#include <memory>

namespace as {

template<class Func, class... Args>
auto sync(Executor& context, Func&& func, Args&&... args)
template<class Ex, class Func, class... Args>
auto sync(Ex& context, Func&& func, Args&&... args)
-> decltype( std::declval<Func>()(std::declval<Args>()...) )
{
if ( context.IsCurrent() )
return std::forward<Func>(func)( std::forward<Args>(args)... );


auto timpl = make_task<TaskImplBase>( std::forward<Func>(func),
std::forward<Args>(args)... );

context.Schedule( {timpl} );

return timpl->GetControlBlock()->Get();
return as::async( context, std::forward<Func>(func),
std::forward<Args>(args)... ).get();
}

template<class Func, class... Args>
Expand Down
1 change: 1 addition & 0 deletions async-task/TaskImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "TaskStatus.hpp"
#include "CallableTraits.hpp"
#include "AsyncResult.hpp"

#include <memory>
#include <functional>
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ CreateTest( thread_work_performance_test.cpp )
CreateTest( chain_test.cpp )
CreateTest( traits_test.cpp )
# CreateTest( await_test.cpp )
CreateTest( sync_test.cpp )
24 changes: 24 additions & 0 deletions test/sync_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Sync.hpp"

#include <cassert>

int main(int argc, char *argv[])
{
auto r = as::sync( []() {
std::this_thread::sleep_for( std::chrono::seconds(5) );
return 42;
} );

assert( r == 42 );

as::ThreadExecutor ex;

auto s = as::sync( ex, []() {
std::this_thread::sleep_for( std::chrono::seconds(2) );
return 99;
} );

assert( s == 99 );

return 0;
}

0 comments on commit 7cdb904

Please sign in to comment.