diff --git a/.travis.yml b/.travis.yml index 63cf756..6b1d981 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,7 +30,7 @@ script: - 'which build-wrapper-linux-x86-64 && build-wrapper-linux-x86-64 --out-dir bw-output make -j 2 || make -j 2' - set -o pipefail - tests/run-parallel-tests.sh tests/all_tests - - "tests/api 2>&1 | grep -vE 'callback result 9|remote_calc->add. 4, 5 .: 9|set callback|] \\.$'" + - tests/api 2>&1 | cat - tests/hmac_test 2>&1 | cat - tests/ecc_test README.md 2>&1 | cat - 'find CMakeFiles/fc.dir -type d | while read d; do gcov -o "$d" "${d/CMakeFiles*.dir/./}"/*.cpp; done >/dev/null' diff --git a/include/fc/api.hpp b/include/fc/api.hpp index c9fb41b..d75a159 100644 --- a/include/fc/api.hpp +++ b/include/fc/api.hpp @@ -48,7 +48,7 @@ namespace fc { using type = short_pack; }; template - struct pack_cutter_impl, T, Types...> + struct pack_cutter_impl, T, Types...> : public pack_cutter_impl {}; template struct pack_cutter : public pack_cutter_impl {}; diff --git a/tests/api.cpp b/tests/api.cpp index fbe42f2..5734758 100644 --- a/tests/api.cpp +++ b/tests/api.cpp @@ -28,6 +28,17 @@ class login_api }; FC_API( login_api, (get_calc)(test) ); + +class optionals_api +{ +public: + std::string foo( const std::string& first, const fc::optional& second, + const fc::optional& third ) { + return fc::json::to_string(fc::variants{first, {second, 2}, {third, 2}}); + } +}; +FC_API( optionals_api, (foo) ); + using namespace fc; class some_calculator @@ -59,8 +70,8 @@ int main( int argc, char** argv ) try { fc::api calc_api( std::make_shared() ); - fc::http::websocket_server server; - server.on_connection([&]( const websocket_connection_ptr& c ){ + auto server = std::make_shared(); + server->on_connection([&]( const websocket_connection_ptr& c ){ auto wsc = std::make_shared(c, MAX_DEPTH); auto login = std::make_shared(); login->calc = calc_api; @@ -68,124 +79,60 @@ int main( int argc, char** argv ) c->set_session_data( wsc ); }); - server.listen( 8090 ); - server.start_accept(); + server->listen( 8090 ); + server->start_accept(); - for( uint32_t i = 0; i < 5000; ++i ) - { - try { - fc::http::websocket_client client; - auto con = client.connect( "ws://localhost:8090" ); - auto apic = std::make_shared(con, MAX_DEPTH); - auto remote_login_api = apic->get_remote_api(); - auto remote_calc = remote_login_api->get_calc(); - remote_calc->on_result( []( uint32_t r ) { elog( "callback result ${r}", ("r",r) ); } ); - wdump((remote_calc->add( 4, 5 ))); - } catch ( const fc::exception& e ) - { - edump((e.to_detail_string())); - } - } - wlog( "exit scope" ); - } - catch( const fc::exception& e ) - { - edump((e.to_detail_string())); - } - wlog( "returning now..." ); - - return 0; + try { + auto client = std::make_shared(); + auto con = client->connect( "ws://localhost:8090" ); + auto apic = std::make_shared(con, MAX_DEPTH); + auto remote_login_api = apic->get_remote_api(); + auto remote_calc = remote_login_api->get_calc(); + bool remote_triggered = false; + remote_calc->on_result( [&remote_triggered]( uint32_t r ) { remote_triggered = true; } ); + FC_ASSERT(remote_calc->add( 4, 5 ) == 9); + FC_ASSERT(remote_triggered); - some_calculator calc; - variant_calculator vcalc; + client.reset(); + fc::usleep(fc::milliseconds(100)); + server.reset(); + } FC_LOG_AND_RETHROW() + } FC_LOG_AND_RETHROW() - fc::api api_calc( &calc ); - fc::api api_vcalc( &vcalc ); - fc::api api_nested_calc( api_calc ); - - wdump( (api_calc->add(5,4)) ); - wdump( (api_calc->sub(5,4)) ); - wdump( (api_vcalc->add(5,4)) ); - wdump( (api_vcalc->sub(5,4)) ); - wdump( (api_nested_calc->sub(5,4)) ); - wdump( (api_nested_calc->sub(5,4)) ); - - /* - variants v = { 4, 5 }; - auto g = to_generic( api_calc->add ); - auto r = call_generic( api_calc->add, v.begin(), v.end() ); - wdump((r)); - wdump( (g(v)) ); - */ - - /* try { - fc::api_server server; - auto api_id = server.register_api( api_calc ); - wdump( (api_id) ); - auto result = server.call( api_id, "add", {4, 5} ); - wdump( (result) ); - } catch ( const fc::exception& e ) - { - elog( "${e}", ("e",e.to_detail_string() ) ); - } + auto optionals = std::make_shared(); + fc::api oapi(optionals); + FC_ASSERT(oapi->foo("a") == "[\"a\",null,null]"); + FC_ASSERT(oapi->foo("a", "b") == "[\"a\",\"b\",null]"); + FC_ASSERT(oapi->foo("a", "b", "c") == "[\"a\",\"b\",\"c\"]"); + FC_ASSERT(oapi->foo("a", {}, "c") == "[\"a\",null,\"c\"]"); - ilog( "------------------ NESTED TEST --------------" ); - try { - login_api napi_impl; - napi_impl.calc = api_calc; - fc::api napi(&napi_impl); + auto server = std::make_shared(); + server->on_connection([&]( const websocket_connection_ptr& c ){ + auto wsc = std::make_shared(*c, MAX_DEPTH); + wsc->register_api(fc::api(optionals)); + c->set_session_data( wsc ); + }); - fc::api_server server; - auto api_id = server.register_api( napi ); - wdump( (api_id) ); - auto result = server.call( api_id, "get_calc" ); - wdump( (result) ); - result = server.call( result.as_uint64(), "add", {4,5} ); - wdump( (result) ); + server->listen( 8090 ); + server->start_accept(); + try { + auto client = std::make_shared(); + auto con = client->connect( "ws://localhost:8090" ); + auto apic = std::make_shared(*con, MAX_DEPTH); + auto remote_optionals = apic->get_remote_api(); - fc::api serv( &server ); + FC_ASSERT(remote_optionals->foo("a") == "[\"a\",null,null]"); + FC_ASSERT(remote_optionals->foo("a", "b") == "[\"a\",\"b\",null]"); + FC_ASSERT(remote_optionals->foo("a", "b", "c") == "[\"a\",\"b\",\"c\"]"); + FC_ASSERT(remote_optionals->foo("a", {}, "c") == "[\"a\",null,\"c\"]"); - fc::api_client apic( serv ); - - fc::api remote_api = apic; - - - auto remote_calc = remote_api->get_calc(); - int r = remote_calc->add( 4, 5 ); - idump( (r) ); - - } catch ( const fc::exception& e ) - { - elog( "${e}", ("e",e.to_detail_string() ) ); - } - */ - - ilog( "------------------ NESTED TEST --------------" ); - try { - login_api napi_impl; - napi_impl.calc = api_calc; - fc::api napi(&napi_impl); - - - auto client_side = std::make_shared(MAX_DEPTH); - auto server_side = std::make_shared(MAX_DEPTH); - server_side->set_remote_connection( client_side ); - client_side->set_remote_connection( server_side ); - - server_side->register_api( napi ); - - fc::api remote_api = client_side->get_remote_api(); - - auto remote_calc = remote_api->get_calc(); - int r = remote_calc->add( 4, 5 ); - idump( (r) ); - - } catch ( const fc::exception& e ) - { - elog( "${e}", ("e",e.to_detail_string() ) ); - } + client.reset(); + fc::usleep(fc::milliseconds(100)); + server.reset(); + } FC_LOG_AND_RETHROW() + } FC_LOG_AND_RETHROW() return 0; }