c->add_callback(new_connection,bind(&command_client::read_hello, boost::ref(*this)));
// don't expect hello from an always closed connection (like dummy_client_connection)
- if (!c->is_closed())
- read_hello();
+ if (!is_connection_closed())
+ {
+ try
+ {
+ read_hello();
+ }
+ catch (t2n_communication_error &e)
+ {
+ c->close();
+
+ // store a copy of the exception that you can find out details about the error later
+ std::auto_ptr<t2n_exception> tmp(e.clone());
+ constructorException=tmp;
+ }
+ catch (...)
+ {
+ throw;
+ }
+ }
}
/** @brief replace the connection currently in use with a new one
command_container cc(cmd);
boost::archive::binary_oarchive oa(ofs);
+ if (is_connection_closed())
+ throw t2n_transfer_error("connection to server is closed");
+
try
{
oa << cc;
#ifndef __LIBT2N_COMMAND_CLIENT
#define __LIBT2N_COMMAND_CLIENT
+#include <functional>
+#include <string>
+
#include "client.hxx"
#include "container.hxx"
std::string read_packet(const long long &usec_timeout);
bool check_hello(const std::string& hellostr);
+ std::auto_ptr<t2n_exception> constructorException;
+
public:
command_client(client_connection* _c,
long long _command_timeout_usec=command_timeout_usec_default,
{ return command_timeout_usec; }
long long get_hello_timeout_usec(void)
{ return hello_timeout_usec; }
+ bool is_connection_closed(void)
+ { return c->is_closed(); }
+ t2n_exception* get_constuctor_exception(void)
+ { return constructorException.get(); }
};
}
std::vector<bool> callback_done;
+ pid_t child_pid;
+
public:
void setUp()
void tearDown()
{
callback_done.clear();
+
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
}
void callback_func(callback_event_type ev, int conn_id)
void ServerNewConnCallback()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void ServerConnClosedCallback()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void ServerConnDeletedCallback()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void ServerCallbackOrder()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void ClientConnClosedCallback()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void ClientConnDeletedCallback()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
void setUp()
{ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void GroupOk()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void WrongGroup()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
void setUp()
{ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void UnixCommToServer()
{
- pid_t pid;
string data;
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void UnixCommToServerAndBack()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void UnixCommToServerAndBackBig()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void IPCommToServer()
{
- pid_t pid;
string data;
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void IPCommToServerAndBack()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void IPCommToServerAndBackBig()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
}
}
} // eo IPCommToServerAndBackBig()
-
-
-
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_comm);
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
void setUp()
{ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void send_hello(string hello_string, socket_server* ss, int conn_id)
{
void HelloOk()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void BadTag()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
sleep(1);
socket_client_connection sc("./socket");
- string errormsg;
+ command_client cc(&sc);
+
+ t2n_exception* ep=cc.get_constuctor_exception();
- try
- {
- command_client cc(&sc);
- }
- catch(t2n_version_mismatch &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
}
void BadVersion()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
sleep(1);
socket_client_connection sc("./socket");
- string errormsg;
+ command_client cc(&sc);
- try
- {
- command_client cc(&sc);
- }
- catch(t2n_version_mismatch &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ t2n_exception* ep=cc.get_constuctor_exception();
+
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("not compatible with the server protocol version"),errormsg);
}
void SeparatorMissing()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
sleep(1);
socket_client_connection sc("./socket");
- string errormsg;
+ command_client cc(&sc);
- try
- {
- command_client cc(&sc);
- }
- catch(t2n_version_mismatch &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ t2n_exception* ep=cc.get_constuctor_exception();
+
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("illegal hello received (1. ;)"),errormsg);
}
void WrongByteOrder()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
sleep(1);
socket_client_connection sc("./socket");
- string errormsg;
+ command_client cc(&sc);
- try
- {
- command_client cc(&sc);
- }
- catch(t2n_version_mismatch &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ t2n_exception* ep=cc.get_constuctor_exception();
+
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("host byte order not matching"),errormsg);
}
void OtherServerBig()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
sleep(1);
socket_client_connection sc("./socket");
- string errormsg;
+ command_client cc(&sc);
+
+ t2n_exception* ep=cc.get_constuctor_exception();
- try
- {
- command_client cc(&sc);
- }
- catch(t2n_version_mismatch &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
}
void OtherServerSmall()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
sleep(1);
socket_client_connection sc("./socket");
- string errormsg;
+ command_client cc(&sc);
- try
- {
- command_client cc(&sc);
- }
- catch(t2n_version_mismatch &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ t2n_exception* ep=cc.get_constuctor_exception();
+
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("illegal hello received (T2N)"),errormsg);
}
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
void setUp()
{ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void send_raw_socket(string hello_string, socket_server* ss, int conn_id)
{
void simple_reconnect()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void reconnect_with_close()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void reconnect_buffer_complete()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void reconnect_buffer_several_complete()
{
- pid_t pid;
-
const int packets=3;
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void reconnect_buffer_no_incomplete1()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
void reconnect_buffer_no_incomplete2()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
void setUp()
{ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void ClientSerializeErr()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
void setUp()
- {
-}
+ { }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void SimpleCmd()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
}
}
- kill(pid,SIGKILL);
}
void SimpleException()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
{ throw; }
CPPUNIT_ASSERT_EQUAL(string("throw me around"),ret);
-
- kill(pid,SIGKILL);
}
}
}
void BigReturn()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'x'),ret);
-
- kill(pid,SIGKILL);
}
}
}
void BigParameter()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
CPPUNIT_ASSERT_EQUAL(string().insert(0,100*1024,'y')+", testfunc() was here",ret);
-
- kill(pid,SIGKILL);
}
}
}
CPPUNIT_TEST_SUITE_END();
+ pid_t child_pid;
+
public:
typedef uint32_t packet_size_indicator;
{ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
void send_hello(string hello_string, socket_server* ss, unsigned int conn_id)
{
void ConnectTimeout()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
socket_client_connection sc("./socket");
- CPPUNIT_ASSERT_EQUAL(true,sc.connection::is_closed());
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("connection not closed",true,sc.connection::is_closed());
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong errormessage",string("no more retries left after connect error"),sc.get_last_error_msg());
}
}
}
void HelloTimeoutNothing()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
// wait till server is up
sleep(1);
socket_client_connection sc("./socket");
+ command_client cc(&sc,1000000,1000000);
- string errormsg;
+ t2n_exception* ep=cc.get_constuctor_exception();
- try
- {
- command_client cc(&sc,1000000,1000000);
- }
- catch(t2n_transfer_error &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
- kill(pid,SIGKILL);
}
}
}
void HelloTimeoutSlowData()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
// wait till server is up
sleep(1);
socket_client_connection sc("./socket");
+ command_client cc(&sc,1000000,1000000);
- string errormsg;
+ t2n_exception* ep=cc.get_constuctor_exception();
- try
- {
- command_client cc(&sc,1000000,1000000);
- }
- catch(t2n_transfer_error &e)
- { errormsg=e.what(); }
- catch(...)
- { throw; }
+ string errormsg;
+ if (ep)
+ errormsg=ep->what();
CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
- kill(pid,SIGKILL);
}
}
}
void CommandTimeout()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
{ throw; }
CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
- kill(pid,SIGKILL);
}
}
}
void CommandSlowResponse()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
{ throw; }
CPPUNIT_ASSERT_EQUAL(string("timeout exceeded"),errormsg);
-
- kill(pid,SIGKILL);
}
}
}
void DisconnectOnWrite()
{
- pid_t pid;
-
- switch(pid=fork())
+ switch(child_pid=fork())
{
case -1:
{
{ throw; }
CPPUNIT_ASSERT_EQUAL(string("write() returned Broken pipe"),errormsg);
-
- kill(pid,SIGKILL);
}
}
}
void DisconnectOnRead()
{
- pid_t pid1, pid2;
+ pid_t pid2;
- switch(pid1=fork())
+ switch(child_pid=fork())
{
case -1:
{
}
}
}
- kill(pid1,SIGKILL);
kill(pid2,SIGKILL);
}
void BreakAccept()
{
- pid_t pid1,pid2;
+ pid_t pid2;
- switch(pid1=fork())
+ switch(child_pid=fork())
{
case -1:
{
}
}
}
- kill(pid1,SIGKILL);
kill(pid2,SIGKILL);
}
};
CPPUNIT_TEST(ignore_server_disconnect);
CPPUNIT_TEST(ignore_handler_reconnects);
-// TODO: missing tests:
-// ignore: init, no server, ignore
-// ignore: init, no server, ignore, server ok, connect?
-
CPPUNIT_TEST_SUITE_END();
public:
CPPUNIT_ASSERT_EQUAL(2,cnt);
}
-
-
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_wrapper);
CPPUNIT_TEST(ignore_noserver);
CPPUNIT_TEST(ignore_finds_lateserver);
+ CPPUNIT_TEST(ignore_wrongserver);
CPPUNIT_TEST_SUITE_END();
pid_t child_pid;
void setUp()
- { }
+ {
+ child_pid=0;
+ }
void tearDown()
- { }
+ {
+ // make sure the server-child is dead before the next test runs
+ if (child_pid != 0)
+ {
+ kill(child_pid,SIGKILL);
+ sleep(1);
+ }
+ }
void ignore_noserver()
{
t2n_exec(&cmd_group_x_client::serverfunc)(1);
// launch a server
-
close_server=false;
kill_server=false;
int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
CPPUNIT_ASSERT_EQUAL(2,i);
+ }
- // make sure the server-child is dead before the next test runs
- kill(child_pid,SIGKILL);
- sleep(1);
+ void send_hello(string hello_string, socket_server* ss, int conn_id)
+ {
+ server_connection *sc=ss->get_connection(conn_id);
+ sc->write(hello_string);
+ }
+
+ void ignore_wrongserver()
+ {
+ wraptype::set_connection(auto_ptr<ConnectionWrapper>
+ (new ReconnectIgnoreFailureSocketWrapper("./socket")));
+
+ // launch a server
+
+ switch(child_pid=fork())
+ {
+ case -1:
+ {
+ CPPUNIT_FAIL("fork error");
+ break;
+ }
+ case 0:
+ // child
+ {
+ socket_server ss("./socket");
+
+ // server sends garbage
+
+ ostringstream hello;
+ hello << "XYZ 123";
+
+ ss.add_callback(new_connection,bind(&test_wrapper_noserver::send_hello, boost::ref(*this), hello.str(),&ss, _1));
+
+ // max 10 sec
+ for (int i=0; i < 10; i++)
+ ss.fill_buffer(1000000);
+ // don't call atexit and stuff
+ _exit(0);
+ }
+
+ default:
+ // parent
+ {
+ // wait till server is up
+ sleep(1);
+ }
+ }
+
+ // there is no valid server
+
+ int i=t2n_exec(&cmd_group_x_client::serverfunc)(1);
+
+ // result is constructed with default constructor on error-and-ignore -> i=0
+
+ CPPUNIT_ASSERT_EQUAL(0,i);
}