summaryrefslogtreecommitdiff
path: root/ACE/tests/Object_Manager_Test.cpp
blob: 79cd94c8ccde569764abc7e400ebd693b01fce38 (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
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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Object_Manager_Test.cpp
//
// = DESCRIPTION
//    Tests the basic functions of the ACE_Object_Manager.
//
// = AUTHOR
//    David L. Levine <levine@cs.wustl.edu>
//
// ============================================================================

#include "test_config.h"
#include "ace/Object_Manager.h"
#include "ace/OS_Memory.h"
#include "ace/ACE.h"

ACE_RCSID(tests, Object_Manager_Test, "$Id$")

static u_int *ip;

extern "C"
void
hook1 (void)
{
  delete ip;
  ip = 0;
}

extern "C"
void
hook2 (void * /* object */, void *param)
{
  u_int *paramp = reinterpret_cast<u_int *> (param);

  // We can use ACE_Log_Msg in an ACE_Object_Manager cleanup hook.
  // But NOT in an ACE_OS::atexit () hook!  However, the ACE_END_TEST
  // invocation in main () will prevent this from being output to the
  // log stream.
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("hook2: %d\n"),
              *paramp));
  delete paramp;
}

int
run_main (int, ACE_TCHAR *[])
{
  ACE::init ();

  ACE_START_TEST (ACE_TEXT ("Object_Manager_Test"));

  u_int errors = 0;

  // If hook1 never gets called, this will show up as a memory leak.
  ACE_NEW_RETURN (ip,
                  u_int,
                  -1);

  const int starting_up =
    ACE_Object_Manager::instance ()->starting_up ();
  const int shutting_down =
    ACE_Object_Manager::instance ()->shutting_down ();

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("starting_up: %d, shutting_down: %d\n"),
              starting_up,
              shutting_down));

  if (starting_up || shutting_down)
    {
      ++errors;
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("%p\n"),
                  ACE_TEXT ("starting_up and shutting_down are supposed to ")
                  ACE_TEXT (" be 0!!!!")));
    }

  if (ACE_OS::atexit (hook1) != 0)
    {
      ++errors;
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("%p\n"),
                  ACE_TEXT ("ACE_OS::atexit () returned non-zero!!!!")));
    }

  for (u_int i = 0; i < 10; ++i)
    {
      u_int *paramp;
      ACE_NEW_RETURN (paramp,
                      u_int,
                      -1);
      *paramp = i;

      // The first paramp argument is only used to distinguish the
      // at_exit entries.  The ACE_Object_Manager only allows one
      // at_exit per object.  It's not used in the hook.
      if (ACE_Object_Manager::instance ()->at_exit (paramp,
                                                    hook2,
                                                    paramp))
        {
          ++errors;
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("ACE_Object_Manager::at_exit () ")
                      ACE_TEXT ("returned non-zero!!!!")));
        }
    }

  ACE_END_TEST;
  ACE::fini ();
  return errors == 0 ? 0 : 1;
}