1 // ============================================================================
3 * @brief Unit test for the TAO_Queued_Message class
5 * @author Carlos O'Ryan <coryan@uci.edu>
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"
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
*
24 // First create a message block
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;
71 current
->bytes_transferred (t
);
74 if (!current
->all_data_sent ())
77 "ERROR: inconsistent state in Queued_Message\n"));
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 ();
92 ACE_CU64_TO_CU32(current_hrtime
);
95 ACE_DEBUG ((LM_DEBUG
, "Running test SEED = %d\n", seed
));
97 TAO_Queued_Message
*head
= 0;
98 TAO_Queued_Message
*tail
= 0;
103 const int iterations
= 100;
106 for (i
= 0; i
!= iterations
; ++i
)
108 push_back_message (head
, tail
);
111 if (ACE_OS::rand () % 100 > 90)
113 // every so often remove a message also.
116 del_message (head
, tail
);
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
++;
135 del_message (head
, tail
);
140 // Go through a phase where all messages are removed.
143 del_message (head
, tail
);
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
),