[complement-svn] SF.net SVN: complement: [1631] trunk/complement/explore/test/sockios
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2007-07-18 20:32:36
|
Revision: 1631 http://svn.sourceforge.net/complement/?rev=1631&view=rev Author: complement Date: 2007-07-18 13:32:22 -0700 (Wed, 18 Jul 2007) Log Message: ----------- refine tests Modified Paths: -------------- trunk/complement/explore/test/sockios/ConnectionProcessor.cc trunk/complement/explore/test/sockios/ConnectionProcessor.h trunk/complement/explore/test/sockios/Makefile.inc trunk/complement/explore/test/sockios/sockios_test_suite.cc Removed Paths: ------------- trunk/complement/explore/test/sockios/client-wc.cc trunk/complement/explore/test/sockios/client-wc.h trunk/complement/explore/test/sockios/close_socket.cc Modified: trunk/complement/explore/test/sockios/ConnectionProcessor.cc =================================================================== --- trunk/complement/explore/test/sockios/ConnectionProcessor.cc 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/ConnectionProcessor.cc 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/07/18 10:17:29 ptr> +// -*- C++ -*- Time-stamp: <07/07/18 23:10:22 ptr> /* * @@ -17,7 +17,10 @@ #include <sockios/sockmgr.h> +#include <mt/xmt.h> + using namespace std; +using namespace xmt; ConnectionProcessor::ConnectionProcessor( std::sockstream& s ) { @@ -334,3 +337,237 @@ return EXAM_RESULT; } +/* ****************** + * + * Check correct processing of case when server close connection. + * Suspicious processing with FreeBSD and OpenBSD servers. + * + */ +static condition cnd_close; + +class Srv // +{ + public: + Srv( std::sockstream& ); + + void connect( std::sockstream& ); + void close(); +}; + +Srv::Srv( std::sockstream& s ) +{ + s << "hello" << endl; + + // xmt::delay( xmt::timespec( 1, 0 ) ); + + s.close(); + // ::shutdown( s.rdbuf()->fd(), 2 ); + cnd_close.set( true ); +} + +void Srv::connect( std::sockstream& ) +{ +} + +void Srv::close() +{ +} + +#ifndef __FIT_NO_POLL +typedef sockmgr_stream_MP<Srv> srv_type; +#elif defined(__FIT_NO_SELECT) +typedef sockmgr_stream_MP_SELECT<Srv> srv_type; +#else +# error Either poll or select should be present! +#endif + +static srv_type *srv_p; +condition cnd; + +Thread::ret_code server_proc( void * ) +{ + Thread::ret_code rt; + rt.iword = 0; + + cnd.set( false ); + srv_type srv( port ); // start server + + ::srv_p = &srv; + + if ( !srv.is_open() || !srv.good() ) { + ++rt.iword; + } + + cnd.set( true ); + + srv.wait(); + + return rt; +} + +Thread::ret_code client_proc( void * ) +{ + Thread::ret_code rt; + rt.iword = 0; + + cnd.try_wait(); + + EXAM_MESSAGE_ASYNC( "Client start" ); + std::sockstream sock( "localhost", ::port ); + + string buf; + + getline( sock, buf ); + + if ( !sock.is_open() || !sock.good() ) { + ++rt.iword; + } + + EXAM_CHECK_ASYNC( buf == "hello" ); + + // xmt::delay( xmt::timespec( 5, 0 ) ); + + // sock << 'a' << endl; + + /* + read required here, due to we can see FIN packet only on read, + and no other solution! (another solution is nonblock sockets or + aio, but this is another story) + */ + cnd_close.try_wait(); + + char a; + sock.read( &a, 1 ); + + EXAM_CHECK_ASYNC( !sock.good() ); + + srv_p->close(); + + EXAM_MESSAGE_ASYNC( "Client end" ); + + return rt; +} + +int EXAM_IMPL(trivial_sockios_test::srv_close_connection) +{ + Thread srv( server_proc ); + cnd_close.set( false ); + Thread client( client_proc ); + + EXAM_CHECK( client.join().iword == 0 ); + EXAM_CHECK( srv.join().iword == 0 ); + + return EXAM_RESULT; +} + +/* + * Server listen tcp socket; client connect to server and try to read + * what server write to socket; server don't write anything, but we + * try to close connection (close socket on client's side, but from + * differrent thread from reading socket). + * I suspect that closing socket on client side don't lead to break down + * through read call. + */ + +class ConnectionProcessor3 // dummy variant +{ + public: + ConnectionProcessor3( std::sockstream& ); + + void connect( std::sockstream& ); + void close(); +}; + +ConnectionProcessor3::ConnectionProcessor3( std::sockstream& s ) +{ + EXAM_MESSAGE_ASYNC( "Server seen connection" ); + + EXAM_CHECK_ASYNC( s.good() ); + connect( s ); + // cerr << "Server see connection\n"; // Be silent, avoid interference + // with Input line prompt +} + +void ConnectionProcessor3::connect( std::sockstream& s ) +{ + EXAM_MESSAGE_ASYNC( "Server start connection processing" ); + + EXAM_CHECK_ASYNC( s.good() ); + + // string msg; + + // getline( s, msg ); + char c = '1'; + s.write( &c, 1 ); + s.flush(); + // cnd2.set( true ); + // EXAM_CHECK_EQUAL( msg, ::message ); + EXAM_CHECK_ASYNC( s.good() ); + + // s << ::message_rsp << endl; // server's response + + // BOOST_REQUIRE( s.good() ); + EXAM_MESSAGE_ASYNC( "Server stop connection processing" ); + + return; +} + +void ConnectionProcessor3::close() +{ + EXAM_MESSAGE_ASYNC( "Server: client close connection" ); +} + +std::sockstream *psock = 0; + +Thread::ret_code thread_entry_call( void * ) +{ + Thread::ret_code rt; + rt.iword = 0; + + cnd.set( true ); + + EXAM_MESSAGE_ASYNC( "Client start" ); + + EXAM_CHECK_ASYNC( psock->good() ); + + char c = '0'; + psock->read( &c, 1 ); + EXAM_CHECK_ASYNC( c == '1' ); + cnd_close.set( true ); + psock->read( &c, 1 ); + + return rt; +} + +int EXAM_IMPL(trivial_sockios_test::client_close_socket) +{ +#ifndef __FIT_NO_POLL + sockmgr_stream_MP<ConnectionProcessor3> srv( port ); // start server + + cnd.set( false ); + cnd_close.set( false ); + + // open client's socket _before_ thread launch to demonstrate problem with + // socket close (close socket's descriptor in one thread don't lead to real + // shutdown events if socket in use in another thread) + psock = new std::sockstream( "localhost", ::port ); + xmt::Thread thr( thread_entry_call ); + cnd.try_wait(); + // close socket; you may expect that sock.read break down, but this + // will not happens: this thread has one copy of (psock) file descriptor, + // thread_entry_call has another; we close only one + cnd_close.try_wait(); + // but call shutdown is what you want here: + psock->rdbuf()->shutdown( sock_base::stop_in | sock_base::stop_out ); + psock->close(); + thr.join(); + delete psock; + + srv.close(); // close server, so we don't wait server termination on next line + srv.wait(); // Wait for server stop to serve clients connections +#else + EXAM_ERROR( "select-based sockmgr not implemented on this platform" ); +#endif + + return EXAM_RESULT; +} Modified: trunk/complement/explore/test/sockios/ConnectionProcessor.h =================================================================== --- trunk/complement/explore/test/sockios/ConnectionProcessor.h 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/ConnectionProcessor.h 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/07/18 10:08:07 ptr> +// -*- C++ -*- Time-stamp: <07/07/19 00:16:00 ptr> /* * @@ -42,6 +42,9 @@ int EXAM_DECL(listen_iface); int EXAM_DECL(shared_socket); + int EXAM_DECL(srv_close_connection); + int EXAM_DECL(client_close_socket); + private: in_addr hostaddr; // sockaddr hostaddr; Modified: trunk/complement/explore/test/sockios/Makefile.inc =================================================================== --- trunk/complement/explore/test/sockios/Makefile.inc 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/Makefile.inc 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,6 +1,6 @@ -# -*- makefile -*- Time-stamp: <07/07/18 08:37:31 ptr> +# -*- makefile -*- Time-stamp: <07/07/19 00:16:55 ptr> PRGNAME = sockios_ut SRC_CC = ConnectionProcessor.cc message.cc \ - client-wc.cc close_socket.cc bytes_in_socket.cc bytes_in_socket2.cc \ + bytes_in_socket.cc bytes_in_socket2.cc \ names.cc sockios_test.cc sockios_test_suite.cc unit_test.cc Deleted: trunk/complement/explore/test/sockios/client-wc.cc =================================================================== --- trunk/complement/explore/test/sockios/client-wc.cc 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/client-wc.cc 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,146 +0,0 @@ -// -*- C++ -*- Time-stamp: <07/07/18 08:46:23 ptr> - -/* - * Copyright (c) 2004, 2006, 2007 - * Petr Ovtchenkov - * - * Licensed under the Academic Free License Version 3.0 - * - */ - -#include <exam/suite.h> - -#include <string> -#include <sockios/sockstream> -#include <sockios/sockmgr.h> -#include <iostream> -#include <iomanip> -#include <mt/xmt.h> - -#include "client-wc.h" -#include "message.h" - -using namespace std; -using namespace xmt; - -/* - Check correct processing of case when server close connection. - Suspicious processing with FreeBSD and OpenBSD servers. - - */ -static condition cnd_close; - -class Srv // -{ - public: - Srv( std::sockstream& ); - - void connect( std::sockstream& ); - void close(); -}; - -Srv::Srv( std::sockstream& s ) -{ - s << "hello" << endl; - - // xmt::delay( xmt::timespec( 1, 0 ) ); - - s.close(); - // ::shutdown( s.rdbuf()->fd(), 2 ); - cnd_close.set( true ); -} - -void Srv::connect( std::sockstream& ) -{ -} - -void Srv::close() -{ -} - -#ifndef __FIT_NO_POLL -typedef sockmgr_stream_MP<Srv> srv_type; -#elif defined(__FIT_NO_SELECT) -typedef sockmgr_stream_MP_SELECT<Srv> srv_type; -#else -# error Either poll or select should be present! -#endif - -static srv_type *srv_p; -condition cnd; - -Thread::ret_code server_proc( void * ) -{ - Thread::ret_code rt; - rt.iword = 0; - - cnd.set( false ); - srv_type srv( port ); // start server - - ::srv_p = &srv; - - if ( !srv.is_open() || !srv.good() ) { - ++rt.iword; - } - - cnd.set( true ); - - srv.wait(); - - return rt; -} - -Thread::ret_code client_proc( void * ) -{ - Thread::ret_code rt; - rt.iword = 0; - - cnd.try_wait(); - - EXAM_MESSAGE_ASYNC( "Client start" ); - std::sockstream sock( "localhost", ::port ); - - string buf; - - getline( sock, buf ); - - if ( !sock.is_open() || !sock.good() ) { - ++rt.iword; - } - - EXAM_CHECK_ASYNC( buf == "hello" ); - - // xmt::delay( xmt::timespec( 5, 0 ) ); - - // sock << 'a' << endl; - - /* - read required here, due to we can see FIN packet only on read, - and no other solution! (another solution is nonblock sockets or - aio, but this is another story) - */ - cnd_close.try_wait(); - - char a; - sock.read( &a, 1 ); - - EXAM_CHECK_ASYNC( !sock.good() ); - - srv_p->close(); - - EXAM_MESSAGE_ASYNC( "Client end" ); - - return rt; -} - -int EXAM_IMPL(srv_close_connection_test) -{ - Thread srv( server_proc ); - cnd_close.set( false ); - Thread client( client_proc ); - - EXAM_CHECK( client.join().iword == 0 ); - EXAM_CHECK( srv.join().iword == 0 ); - - return EXAM_RESULT; -} Deleted: trunk/complement/explore/test/sockios/client-wc.h =================================================================== --- trunk/complement/explore/test/sockios/client-wc.h 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/client-wc.h 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,19 +0,0 @@ -// -*- C++ -*- Time-stamp: <07/07/18 09:20:14 ptr> - -/* - * - * Copyright (c) 2004, 2007 - * Petr Ovtchenkov - * - * Licensed under the Academic Free License version 3.0 - * - */ - -#ifndef __client_wc_h -#define __client_wc_h - -#include <exam/suite.h> - -int EXAM_DECL(srv_close_connection_test); - -#endif // __client_wc_h Deleted: trunk/complement/explore/test/sockios/close_socket.cc =================================================================== --- trunk/complement/explore/test/sockios/close_socket.cc 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/close_socket.cc 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,148 +0,0 @@ -// -*- C++ -*- Time-stamp: <07/07/11 21:34:53 ptr> - -/* - * - * Copyright (c) 2006 - * Petr Ovtchenkov - * - * Licensed under the Academic Free License Version 2.1 - * - * This material is provided "as is", with absolutely no warranty expressed - * or implied. Any use is at your own risk. - * - * Permission to use, copy, modify, distribute and sell this software - * and its documentation for any purpose is hereby granted without fee, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. - */ - -#include <exam/suite.h> - -#include <iostream> -#include <list> -#include <mt/xmt.h> - -#include <sockios/sockstream> -#include <sockios/sockmgr.h> - -using namespace std; -using namespace xmt; - -/* - * Server listen tcp socket; client connect to server and try to read - * what server write to socket; server don't write anything, but we - * try to close connection (close socket on client's side, but from - * differrent thread from reading socket). - * I suspect that closing socket on client side don't lead to break down - * through read call. - */ - -extern int port; - -class ConnectionProcessor3 // dummy variant -{ - public: - ConnectionProcessor3( std::sockstream& ); - - void connect( std::sockstream& ); - void close(); -}; - -ConnectionProcessor3::ConnectionProcessor3( std::sockstream& s ) -{ - EXAM_MESSAGE_ASYNC( "Server seen connection" ); - - EXAM_CHECK_ASYNC( s.good() ); - connect( s ); - // cerr << "Server see connection\n"; // Be silent, avoid interference - // with Input line prompt -} - -condition cnd2; - -void ConnectionProcessor3::connect( std::sockstream& s ) -{ - EXAM_MESSAGE_ASYNC( "Server start connection processing" ); - - EXAM_CHECK_ASYNC( s.good() ); - - // string msg; - - // getline( s, msg ); - char c = '1'; - s.write( &c, 1 ); - s.flush(); - // cnd2.set( true ); - // EXAM_CHECK_EQUAL( msg, ::message ); - EXAM_CHECK_ASYNC( s.good() ); - - // s << ::message_rsp << endl; // server's response - - // BOOST_REQUIRE( s.good() ); - EXAM_MESSAGE_ASYNC( "Server stop connection processing" ); - - return; -} - -void ConnectionProcessor3::close() -{ - EXAM_MESSAGE_ASYNC( "Server: client close connection" ); -} - -condition cnd1; -// Condition cnd2; -std::sockstream *psock = 0; - -Thread::ret_code thread_entry_call( void * ) -{ - Thread::ret_code rt; - rt.iword = 0; - - cnd1.set( true ); - - EXAM_MESSAGE_ASYNC( "Client start" ); - - EXAM_CHECK_ASYNC( psock->good() ); - - char c = '0'; - psock->read( &c, 1 ); - EXAM_CHECK_ASYNC( c == '1' ); - cnd2.set( true ); - psock->read( &c, 1 ); - - return rt; -} - -int EXAM_IMPL(test_client_close_socket) -{ -#ifndef __FIT_NO_POLL - sockmgr_stream_MP<ConnectionProcessor3> srv( port ); // start server - - cnd1.set( false ); - cnd2.set( false ); - - // open client's socket _before_ thread launch to demonstrate problem with - // socket close (close socket's descriptor in one thread don't lead to real - // shutdown events if socket in use in another thread) - psock = new std::sockstream( "localhost", ::port ); - xmt::Thread thr( thread_entry_call ); - cnd1.try_wait(); - // close socket; you may expect that sock.read break down, but this - // will not happens: this thread has one copy of (psock) file descriptor, - // thread_entry_call has another; we close only one - cnd2.try_wait(); - // but call shutdown is what you want here: - psock->rdbuf()->shutdown( sock_base::stop_in | sock_base::stop_out ); - psock->close(); - thr.join(); - delete psock; - - srv.close(); // close server, so we don't wait server termination on next line - srv.wait(); // Wait for server stop to serve clients connections -#else - EXAM_ERROR( "select-based sockmgr not implemented on this platform" ); -#endif - - return EXAM_RESULT; -} Modified: trunk/complement/explore/test/sockios/sockios_test_suite.cc =================================================================== --- trunk/complement/explore/test/sockios/sockios_test_suite.cc 2007-07-18 16:31:53 UTC (rev 1630) +++ trunk/complement/explore/test/sockios/sockios_test_suite.cc 2007-07-18 20:32:22 UTC (rev 1631) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/07/18 10:23:40 ptr> +// -*- C++ -*- Time-stamp: <07/07/19 00:19:19 ptr> /* * @@ -25,11 +25,8 @@ #include "ConnectionProcessor.h" -#include "client-wc.h" - using namespace std; -int EXAM_DECL(test_client_close_socket); int EXAM_DECL(test_more_bytes_in_socket); int EXAM_DECL(test_more_bytes_in_socket2); @@ -65,9 +62,9 @@ t.add( &trivial_sockios_test::listen_iface, trivial_test, "trivial_sockios_test::listen_iface", tc[0] ); - t.add( srv_close_connection_test, "srv_close_connection_test" ); + t.add( &trivial_sockios_test::srv_close_connection, trivial_test, "trivial_sockios_test::srv_close_connection", tc[0] ); t.add( &trivial_sockios_test::shared_socket, trivial_test, "trivial_sockios_test::shared_socket", tc[0] ); - t.add( test_client_close_socket, "test_client_close_socket" ); + t.add( &trivial_sockios_test::client_close_socket, trivial_test, "trivial_sockios_test::client_close_socket", tc[0] ); t.add( test_more_bytes_in_socket, "test_more_bytes_in_socket" ); // timeout 5 t.add( test_more_bytes_in_socket2, "test_more_bytes_in_socket2" ); // timeout 5 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |