summaryrefslogtreecommitdiff
path: root/ACE/tests/Bug_4189_Regression_Test.cpp
blob: 1ccd0e1a76872c72d9f3fcf5110bb35bfb79778a (plain)
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
//=============================================================================
/**
 *  @file  Bug_4189_Regression_Test.cpp
 *
 * This test shows the reuse address issue on broadcast sockets.
 * Start two process instances of this test on the same host within x sec.
 * For broadcast sockets it should be possible to open the same port more than
 * once at the time on a host.
 */
//=============================================================================

#include "test_config.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/Log_Msg.h"
#include "ace/SOCK_Dgram_Bcast.h"
#include "ace/Thread_Manager.h"

int result = 0;

const int UDP_PORT = 3000;
const int SEC_TO_KEEP_SOCKET_OPEN = 2;

#if defined (ACE_HAS_THREADS)

int reuseAddr_test ()
{
# if defined ACE_WIN32 || !defined ACE_LACKS_IOCTL
  ACE_SOCK_Dgram_Bcast sock1;
  if (sock1.open(ACE_INET_Addr(UDP_PORT),PF_INET,0,1) != 0)
  {
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could not open socket for broadcast on port %d\n"), UDP_PORT ));
    ++result;
  }
  else
  {
    // Keep the socket open for some time
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Opened socket on port %d and keep it open for %d sec\n"), UDP_PORT,  SEC_TO_KEEP_SOCKET_OPEN));

    ACE_OS::sleep(ACE_Time_Value(SEC_TO_KEEP_SOCKET_OPEN,0));

    sock1.close();
  }
# endif
  return result;
}

/* \brief Thread main function to run run_receiver function
   \note run_receiver return valu is stored in receiver_exit_code global variable
*/
static ACE_THR_FUNC_RETURN run_thread_receiver (void *)
{
  reuseAddr_test ();
  return 0;
}
#endif /* defined (ACE_HAS_THREADS) */

int run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Bug_4189_Regression_Test"));

#if defined (ACE_HAS_THREADS)
  if (ACE_Thread_Manager::instance ()->spawn_n (2, run_thread_receiver) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("spawn_n ()")), -1);

  ACE_Thread_Manager::instance ()->wait ();
#endif

  ACE_END_TEST;
  return result;
}