1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/**
* @file Bug_2609_Regression_Test.cpp
*
* $Id: Bug_2609_Regression_Test.cpp 91673 2010-09-08 18:49:47Z johnnyw $
*
* Reproduces the problems reported in bug 2609:
* http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=2609
*
* @author Milan Cvetkovic <milan.cvetkovic@mpathix.com>
*/
#include "test_config.h"
#include "ace/Svc_Handler.h"
#include "ace/Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/INET_Addr.h"
#include "ace/Trace.h"
#include "ace/SOCK_Connector.h"
#include "ace/Thread_Semaphore.h"
#if defined (ACE_HAS_THREADS)
# define TEST_TRACE(X) ACE_Trace ____ (ACE_TEXT (X), __LINE__, ACE_TEXT (__FILE__))
bool g_handler_deleted = false;
// use semaphore for synchronization
//
ACE_Thread_Semaphore g_semaphore (0);
class My_Svc_Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM,ACE_NULL_SYNCH>
{
public:
typedef ACE_Svc_Handler<ACE_SOCK_STREAM,ACE_NULL_SYNCH> super;
My_Svc_Handler()
{
TEST_TRACE ("My_Svc_Handler:My_Svc_Handler");
g_handler_deleted = false;
reference_counting_policy().value(
Reference_Counting_Policy::ENABLED);
}
~My_Svc_Handler()
{
TEST_TRACE ("My_Svc_Handler::~My_Svc_Handler");
g_handler_deleted = true;
}
//FUZZ: disable check_for_lack_ACE_OS
int open (void* pv)
{
TEST_TRACE ("open");
g_semaphore.release (); // signal open completed
// super::open() will register_handler for input. The default
// handle_input() from ACE_Event_Handler will return -1, triggering
// handler removal from the reactor.
return super::open (pv);
}
//FUZZ: enable check_for_lack_ACE_OS
int handle_close (ACE_HANDLE, ACE_Reactor_Mask)
{
TEST_TRACE ("handle_close");
if (g_handler_deleted)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Handler deleted in base handle_close()\n")));
}
// signal handle_close() completed
g_semaphore.release ();
return 0;
}
};
struct My_Task : public ACE_Task_Base
{
int svc()
{
TEST_TRACE ("My_Task::svc");
ACE_Reactor *r = ACE_Reactor::instance ();
r->owner (ACE_OS::thr_self ());
int rv = r->run_reactor_event_loop ();
if (rv < 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Cannot run %p\n"),
ACE_TEXT ("reactor event loop")));
}
return 0;
}
};
// event handler used to signal when the reactor started
struct Timer_Handler : public ACE_Event_Handler
{
int handle_timeout (const ACE_Time_Value&, const void*)
{
g_semaphore.release (); // signal reactor started
return 0;
}
};
typedef ACE_Acceptor<My_Svc_Handler, ACE_SOCK_ACCEPTOR> My_Acceptor;
#endif
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Bug_2609_Regression_Test"));
int status = 0;
#if defined (ACE_HAS_THREADS)
My_Acceptor acceptor;
if (-1 == acceptor.open (ACE_sap_any_cast (const ACE_INET_Addr &)))
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("acceptor open")),
1);
ACE_INET_Addr listen_addr;
acceptor.acceptor ().get_local_addr (listen_addr);
#if defined (ACE_HAS_IPV6)
const ACE_TCHAR *me =
listen_addr.get_type () == PF_INET ? ACE_LOCALHOST : ACE_IPV6_LOCALHOST;
#else
const ACE_TCHAR *me = ACE_LOCALHOST;
#endif /* ACE_HAS_IPV6 */
ACE_INET_Addr connect_addr (listen_addr.get_port_number (),
me,
listen_addr.get_type ());
Timer_Handler timer_handler;
ACE_Reactor::instance()->schedule_timer (&timer_handler,
0,
ACE_Time_Value(0));
My_Task task;
int activated = task.activate ();
if (activated < 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Error activating task")),
1);
}
g_semaphore.acquire (); // wait for reactor to start
{
ACE_SOCK_Connector c1;
ACE_SOCK_Stream s1;
if (-1 == c1.connect (s1, connect_addr))
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("connect")));
status = 1;
}
else
{
g_semaphore.acquire (); // wait for open to complete
s1.close (); // will trip handle_input()
g_semaphore.acquire (); // wait for handle_close to complete
}
}
ACE_Reactor::instance ()->end_reactor_event_loop ();
task.wait ();
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("threads not supported on this platform\n")));
#endif
ACE_END_TEST;
return status;
}
|