Refactored + extended websocket test
This commit is contained in:
parent
899a5c489b
commit
a9364db96b
4 changed files with 82 additions and 59 deletions
|
|
@ -370,6 +370,7 @@ add_executable( all_tests tests/all_tests.cpp
|
|||
tests/crypto/rand_test.cpp
|
||||
tests/crypto/sha_tests.cpp
|
||||
tests/network/ntp_test.cpp
|
||||
tests/network/http/websocket_test.cpp
|
||||
tests/thread/task_cancel.cpp
|
||||
tests/bloom_test.cpp
|
||||
tests/real128_test.cpp
|
||||
|
|
|
|||
70
tests/network/http/websocket_test.cpp
Normal file
70
tests/network/http/websocket_test.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <fc/network/http/websocket.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(fc_network)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(websocket_test)
|
||||
{
|
||||
fc::http::websocket_client client;
|
||||
fc::http::websocket_connection_ptr s_conn, c_conn;
|
||||
{
|
||||
fc::http::websocket_server server;
|
||||
server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){
|
||||
s_conn = c;
|
||||
c->on_message_handler([&](const std::string& s){
|
||||
c->send_message("echo: " + s);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen( 8090 );
|
||||
server.start_accept();
|
||||
|
||||
std::string echo;
|
||||
c_conn = client.connect( "ws://localhost:8090" );
|
||||
c_conn->on_message_handler([&](const std::string& s){
|
||||
echo = s;
|
||||
});
|
||||
c_conn->send_message( "hello world" );
|
||||
fc::usleep( fc::seconds(1) );
|
||||
BOOST_CHECK_EQUAL("echo: hello world", echo);
|
||||
c_conn->send_message( "again" );
|
||||
fc::usleep( fc::seconds(1) );
|
||||
BOOST_CHECK_EQUAL("echo: again", echo);
|
||||
|
||||
s_conn->close(0, "test");
|
||||
fc::usleep( fc::seconds(1) );
|
||||
try {
|
||||
c_conn->send_message( "again" );
|
||||
BOOST_FAIL("expected assertion failure");
|
||||
} catch (const fc::assert_exception& e) {
|
||||
//std::cerr << e.to_string() << "\n";
|
||||
}
|
||||
|
||||
c_conn = client.connect( "ws://localhost:8090" );
|
||||
c_conn->on_message_handler([&](const std::string& s){
|
||||
echo = s;
|
||||
});
|
||||
c_conn->send_message( "hello world" );
|
||||
fc::usleep( fc::seconds(1) );
|
||||
BOOST_CHECK_EQUAL("echo: hello world", echo);
|
||||
}
|
||||
|
||||
try {
|
||||
c_conn->send_message( "again" );
|
||||
BOOST_FAIL("expected assertion failure");
|
||||
} catch (const fc::assert_exception& e) {
|
||||
std::cerr << e.to_string() << "\n";
|
||||
}
|
||||
|
||||
try {
|
||||
c_conn = client.connect( "ws://localhost:8090" );
|
||||
BOOST_FAIL("expected assertion failure");
|
||||
} catch (const fc::assert_exception& e) {
|
||||
std::cerr << e.to_string() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
@ -40,19 +40,22 @@ BOOST_AUTO_TEST_CASE( cancel_task_blocked_on_mutex)
|
|||
BOOST_TEST_MESSAGE("--- In test_task, sleeps done, exiting");
|
||||
}, "test_task");
|
||||
fc::usleep(fc::seconds(3));
|
||||
test_task.cancel();
|
||||
try
|
||||
{
|
||||
fc::scoped_lock<fc::mutex> test_lock2(test_mutex);
|
||||
test_task.cancel();
|
||||
try
|
||||
{
|
||||
test_task.wait(fc::seconds(1));
|
||||
BOOST_ERROR("test should have been canceled, not exited cleanly");
|
||||
}
|
||||
catch (const fc::canceled_exception&)
|
||||
{
|
||||
}
|
||||
catch (const fc::canceled_exception&)
|
||||
{
|
||||
BOOST_TEST_PASSPOINT();
|
||||
}
|
||||
catch (const fc::timeout_exception&)
|
||||
{
|
||||
}
|
||||
catch (const fc::timeout_exception&)
|
||||
{
|
||||
BOOST_ERROR("unable to cancel task blocked on mutex");
|
||||
}
|
||||
}
|
||||
BOOST_TEST_MESSAGE("Unlocking mutex locked from the main task so the test task will have the opportunity to lock it and be canceled");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
#include <fc/network/http/websocket.hpp>
|
||||
#include <fc/log/logger.hpp>
|
||||
#include <fc/thread/thread.hpp>
|
||||
#include <fc/rpc/websocket_api.hpp>
|
||||
|
||||
using namespace fc::http;
|
||||
|
||||
class echo_session : public fc::http::websocket_session
|
||||
{
|
||||
public:
|
||||
echo_session( const websocket_connection_ptr c ):fc::http::websocket_session(c){}
|
||||
void on_message( const std::string& message )
|
||||
{
|
||||
idump((message));
|
||||
if( message.size() < 64 )
|
||||
send_message( "echo " + message );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
try {
|
||||
auto create_session = [&]( const websocket_connection_ptr& c ){
|
||||
return std::make_shared<echo_session>(c);
|
||||
};
|
||||
fc::http::websocket_server server;
|
||||
server.on_connection(create_session);
|
||||
|
||||
server.listen( 8090 );
|
||||
server.start_accept();
|
||||
|
||||
fc::http::websocket_client client;
|
||||
auto session = client.connect( "ws://localhost:8090", create_session );
|
||||
wlog( "connected" );
|
||||
session->send_message( "hello world" );
|
||||
|
||||
fc::usleep( fc::seconds(2) );
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
catch ( const websocketpp::lib::error_code& e )
|
||||
{
|
||||
edump( (e.message()) );
|
||||
}
|
||||
*/
|
||||
catch ( const fc::exception& e )
|
||||
{
|
||||
edump((e.to_detail_string()));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue