Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Queued_Message_Test / Queued_Message_Test.cpp
blob9580e893db98aacf671723effea3aa8dccd2d181
1 // ============================================================================
2 /**
3 * @brief Unit test for the TAO_Queued_Message class
5 * @author Carlos O'Ryan <coryan@uci.edu>
6 */
7 // ============================================================================
9 #include "tao/Asynch_Queued_Message.h"
10 #include "tao/ORB_Core.h"
11 #include "ace/Log_Msg.h"
12 #include "ace/Message_Block.h"
13 #include "ace/ACE.h"
14 #include "ace/OS_NS_stdio.h"
15 #include "ace/OS_NS_time.h"
16 #include "ace/OS_NS_stdlib.h"
18 /// Max number of bytes on each message block
19 const size_t max_block_length = 256;
21 static TAO_Queued_Message *
22 create_new_message ()
24 // First create a message block
25 size_t block_size =
26 64 + ACE_OS::rand () % (max_block_length - 64);
27 ACE_Message_Block mb (block_size);
28 mb.wr_ptr (block_size);
30 return new TAO_Asynch_Queued_Message (&mb, TAO_ORB_Core_instance (), 0, 0, 1);
33 /// Add a new message at the tail of the queue.
34 static void push_back_message (TAO_Queued_Message *&head,
35 TAO_Queued_Message *&tail)
37 TAO_Queued_Message *msg = create_new_message ();
38 msg->push_back (head, tail);
41 /// Add a new message at the head of the queue.
42 static void push_front_message (TAO_Queued_Message *&head,
43 TAO_Queued_Message *&tail)
45 TAO_Queued_Message *msg = create_new_message ();
46 msg->push_front (head, tail);
49 /// Remove the message at the head of the queue, and simulate the
50 /// behavior of the I/O subsystem when processing such messages.
51 static void del_message (TAO_Queued_Message *&head,
52 TAO_Queued_Message *&tail)
54 // ACE_DEBUG ((LM_DEBUG, "Removing message\n"));
55 TAO_Queued_Message *current = head;
56 current->remove_from_list (head, tail);
58 // Simulate message writing: each message is 'sent' using
59 // multiple write() calls, in this simulation, we call the
60 // bytes_transferred() method until all messages are removed.
62 size_t total_length = current->message_length ();
63 while (total_length > 0)
65 // select how many bytes we want to 'send' in this iteration.
66 size_t t = ACE_OS::rand () % 256 + 1;
68 if (t > total_length)
69 t = total_length;
71 current->bytes_transferred (t);
72 total_length -= t;
74 if (!current->all_data_sent ())
76 ACE_ERROR ((LM_DEBUG,
77 "ERROR: inconsistent state in Queued_Message\n"));
78 ACE_OS::exit (1);
80 current->destroy ();
83 int
84 ACE_TMAIN(int, ACE_TCHAR *[])
86 // Initialize a random seed to get better coverage.
87 // @@ The random seed and default values should be configurable
88 // using command line options.
90 ACE_hrtime_t current_hrtime = ACE_OS::gethrtime ();
91 ACE_UINT32 seed =
92 ACE_CU64_TO_CU32(current_hrtime);
93 ACE_OS::srand (seed);
95 ACE_DEBUG ((LM_DEBUG, "Running test SEED = %d\n", seed));
97 TAO_Queued_Message *head = 0;
98 TAO_Queued_Message *tail = 0;
100 int add_count = 0;
101 int del_count = 0;
103 const int iterations = 100;
104 int i;
106 for (i = 0; i != iterations; ++i)
108 push_back_message (head, tail);
109 add_count++;
111 if (ACE_OS::rand () % 100 > 90)
113 // every so often remove a message also.
114 if (head != 0)
116 del_message (head, tail);
117 del_count++;
122 // second phase, change the probabilities of removing a message.
123 for (i = 0; i != iterations; ++i)
125 if (ACE_OS::rand () % 100 > 90)
127 push_back_message (head, tail); add_count++;
129 if (ACE_OS::rand () % 100 > 90)
131 push_front_message (head, tail); add_count++;
133 if (head != 0)
135 del_message (head, tail);
136 del_count++;
140 // Go through a phase where all messages are removed.
141 while (head != 0)
143 del_message (head, tail);
144 del_count++;
147 if (tail != 0)
149 ACE_ERROR_RETURN ((LM_ERROR,
150 "ERROR: inconsistent state in message queue\n"),
154 if (add_count != del_count)
156 ACE_ERROR_RETURN ((LM_ERROR,
157 "ERROR: mismatched (%d != %d) add and del counts\n",
158 add_count, del_count),
163 return 0;