Refactored + extended websocket test

This commit is contained in:
Peter Conrad 2015-08-30 22:15:20 +02:00
parent 899a5c489b
commit a9364db96b
4 changed files with 82 additions and 59 deletions

View file

@ -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

View 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()

View file

@ -40,6 +40,8 @@ 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));
{
fc::scoped_lock<fc::mutex> test_lock2(test_mutex);
test_task.cancel();
try
{
@ -54,6 +56,7 @@ BOOST_AUTO_TEST_CASE( cancel_task_blocked_on_mutex)
{
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");
}
fc::usleep(fc::seconds(3));

View file

@ -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()));
}
}