Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Policy_Combinations / server.cpp
bloba4c66f8dcd4dcfdf86568624992466120f993dae
1 #include "ace/Get_Opt.h"
2 #include "testS.h"
3 #include "tao/ORB_Core.h"
4 #include "ace/Task.h"
5 #include "tao/RTPortableServer/RTPortableServer.h"
6 #include "../check_supported_priorities.cpp"
8 class test_i :
9 public POA_test
11 public:
12 test_i (CORBA::ORB_ptr orb,
13 PortableServer::POA_ptr poa,
14 CORBA::Short server_priority,
15 CORBA::Short client_priority);
17 CORBA::Short method ();
19 void prioritized_method ();
21 //FUZZ: disable check_for_lack_ACE_OS
22 void shutdown ();
23 //FUZZ: enable check_for_lack_ACE_OS
25 PortableServer::POA_ptr _default_POA ();
27 private:
28 CORBA::ORB_var orb_;
29 PortableServer::POA_var poa_;
30 CORBA::Short server_priority_;
31 CORBA::Short client_priority_;
34 test_i::test_i (CORBA::ORB_ptr orb,
35 PortableServer::POA_ptr poa,
36 CORBA::Short server_priority,
37 CORBA::Short client_priority)
38 : orb_ (CORBA::ORB::_duplicate (orb)),
39 poa_ (PortableServer::POA::_duplicate (poa)),
40 server_priority_ (server_priority),
41 client_priority_ (client_priority)
45 CORBA::Short
46 test_i::method ()
48 ACE_DEBUG ((LM_DEBUG,
49 "test_i::method\n"));
51 return this->client_priority_;
54 void
55 test_i::prioritized_method ()
57 CORBA::Object_var object =
58 this->orb_->resolve_initial_references ("RTCurrent");
60 RTCORBA::Current_var current =
61 RTCORBA::Current::_narrow (object.in ());
63 CORBA::Short priority =
64 current->the_priority ();
66 ACE_DEBUG ((LM_DEBUG,
67 "test_i::prioritized_method: client = %d server = %d (should be %d)\n",
68 this->client_priority_,
69 priority,
70 this->server_priority_));
72 ACE_ASSERT (this->server_priority_ == priority);
75 void
76 test_i::shutdown ()
78 ACE_DEBUG ((LM_DEBUG,
79 "test_i::shutdown\n"));
81 this->orb_->shutdown (false);
84 PortableServer::POA_ptr
85 test_i::_default_POA ()
87 return PortableServer::POA::_duplicate (this->poa_.in ());
90 static CORBA::Short server_priority;
91 static CORBA::Short client_priority;
92 static CORBA::ULong stacksize = 0;
93 static CORBA::ULong static_threads = 2;
94 static CORBA::ULong dynamic_threads = 0;
95 static RTCORBA::Priority default_thread_priority;
96 static CORBA::Boolean allow_request_buffering = 0;
97 static CORBA::ULong max_buffered_requests = 0;
98 static CORBA::ULong max_request_buffer_size = 0;
99 static CORBA::Boolean allow_borrowing = 0;
101 static int
102 parse_args (int argc, ACE_TCHAR **argv)
104 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("s:c:"));
105 int c;
107 while ((c = get_opts ()) != -1)
108 switch (c)
110 case 's':
111 ::server_priority = ACE_OS::atoi (get_opts.opt_arg ());
112 break;
114 case 'c':
115 ::client_priority = ACE_OS::atoi (get_opts.opt_arg ());
116 break;
118 case '?':
119 default:
120 ACE_ERROR_RETURN ((LM_ERROR,
121 "usage: %s "
122 "-s server priority "
123 "-c client priority "
124 "\n",
125 argv [0]),
126 -1);
129 return 0;
132 static void
133 write_iors_to_file (CORBA::Object_ptr object,
134 CORBA::ORB_ptr orb,
135 const char *filename)
137 FILE *file =
138 ACE_OS::fopen (filename, "w");
139 ACE_ASSERT (file != 0);
141 CORBA::String_var ior =
142 orb->object_to_string (object);
144 u_int result = 0;
145 result =
146 ACE_OS::fprintf (file,
147 "%s",
148 ior.in ());
150 ACE_ASSERT (result == ACE_OS::strlen (ior.in ()));
151 ACE_UNUSED_ARG (result);
153 ACE_OS::fclose (file);
156 class server
158 public:
159 server (CORBA::ORB_ptr orb,
160 RTCORBA::RTORB_ptr rt_orb,
161 PortableServer::POA_ptr root_poa,
162 PortableServer::POAManager_ptr poa_manager);
164 void test_root_poa ();
165 void test_child_poa ();
167 typedef void (server::*test_function) (CORBA::PolicyList &policies,
168 CORBA::Short server_priority,
169 CORBA::Short client_priority,
170 const char *test_name);
172 void test_default_pool_poa (CORBA::Short server_priority,
173 CORBA::Short client_priority,
174 server::test_function function,
175 const char *test_name);
177 void test_bands_poa (CORBA::PolicyList &policies,
178 CORBA::Short server_priority,
179 CORBA::Short client_priority,
180 server::test_function function,
181 const char *test_name);
183 void test_no_lanes_poa (CORBA::Short server_priority,
184 CORBA::Short client_priority,
185 server::test_function function,
186 const char *test_name);
188 void test_lanes_poa (CORBA::Short server_priority,
189 CORBA::Short client_priority,
190 server::test_function function,
191 const char *test_name);
193 void test_no_bands_client_propagated_poa (CORBA::PolicyList &policies,
194 CORBA::Short server_priority,
195 CORBA::Short client_priority,
196 const char *test_name);
198 void test_bands_client_propagated_poa (CORBA::PolicyList &policies,
199 CORBA::Short server_priority,
200 CORBA::Short client_priority,
201 const char *test_name);
203 void test_no_bands_server_declared_poa (CORBA::PolicyList &policies,
204 CORBA::Short server_priority,
205 CORBA::Short client_priority,
206 const char *test_name);
208 void test_bands_server_declared_poa (CORBA::PolicyList &policies,
209 CORBA::Short server_priority,
210 CORBA::Short client_priority,
211 const char *test_name);
213 void test_default_pool_no_bands_client_propagated_poa ();
215 void test_no_lanes_no_bands_client_propagated_poa ();
217 void test_lanes_no_bands_client_propagated_poa ();
219 void test_default_pool_bands_client_propagated_poa ();
221 void test_no_lanes_bands_client_propagated_poa ();
223 void test_lanes_bands_client_propagated_poa ();
225 void test_default_pool_no_bands_server_declared_poa ();
227 void test_no_lanes_no_bands_server_declared_poa ();
229 void test_lanes_no_bands_server_declared_poa ();
231 void test_default_pool_bands_server_declared_poa ();
233 void test_no_lanes_bands_server_declared_poa ();
235 void test_lanes_bands_server_declared_poa ();
237 private:
238 CORBA::ORB_var orb_;
239 RTCORBA::RTORB_var rt_orb_;
240 PortableServer::POA_var root_poa_;
241 PortableServer::POAManager_var poa_manager_;
244 server::server (CORBA::ORB_ptr orb,
245 RTCORBA::RTORB_ptr rt_orb,
246 PortableServer::POA_ptr root_poa,
247 PortableServer::POAManager_ptr poa_manager)
248 : orb_ (CORBA::ORB::_duplicate (orb)),
249 rt_orb_ (RTCORBA::RTORB::_duplicate (rt_orb)),
250 root_poa_ (PortableServer::POA::_duplicate (root_poa)),
251 poa_manager_ (PortableServer::POAManager::_duplicate (poa_manager))
255 void
256 server::test_root_poa ()
258 test_i *servant = 0;
259 ACE_NEW_THROW_EX (servant,
260 test_i (this->orb_.in (),
261 this->root_poa_.in (),
262 TAO_INVALID_PRIORITY,
263 TAO_INVALID_PRIORITY),
264 CORBA::NO_MEMORY ());
266 PortableServer::ServantBase_var safe_servant (servant);
268 PortableServer::ObjectId_var id =
269 this->root_poa_->activate_object (servant);
271 CORBA::Object_var object_act = this->root_poa_->id_to_reference (id.in ());
273 test_var test =
274 test::_narrow (object_act.in ());
276 write_iors_to_file (test.in (),
277 this->orb_.in (),
278 "root");
281 void
282 server::test_child_poa ()
284 CORBA::PolicyList policies;
285 policies.length (1);
286 policies[policies.length () - 1] =
287 this->root_poa_->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
289 PortableServer::POA_var poa =
290 this->root_poa_->create_POA ("child",
291 this->poa_manager_.in (),
292 policies);
294 test_i *servant = 0;
295 ACE_NEW_THROW_EX (servant,
296 test_i (this->orb_.in (),
297 poa.in (),
298 TAO_INVALID_PRIORITY,
299 TAO_INVALID_PRIORITY),
300 CORBA::NO_MEMORY ());
302 PortableServer::ServantBase_var safe_servant (servant);
304 PortableServer::ObjectId_var id =
305 poa->activate_object (servant);
307 CORBA::Object_var object_act = poa->id_to_reference (id.in ());
309 test_var test =
310 test::_narrow (object_act.in ());
312 write_iors_to_file (test.in (),
313 this->orb_.in (),
314 "child");
317 void
318 server::test_default_pool_poa (CORBA::Short server_priority,
319 CORBA::Short client_priority,
320 server::test_function function,
321 const char *test_name)
323 CORBA::PolicyList empty_policies;
324 (this->*function) (empty_policies,
325 server_priority,
326 client_priority,
327 test_name);
330 void
331 server::test_bands_poa (CORBA::PolicyList &policies,
332 CORBA::Short server_priority,
333 CORBA::Short client_priority,
334 server::test_function function,
335 const char *test_name)
337 RTCORBA::PriorityBands bands;
338 bands.length (3);
340 bands[0].low = default_thread_priority;
341 bands[0].high = default_thread_priority;
342 bands[1].low = ::server_priority - 1;
343 bands[1].high = ::server_priority + 1;
344 bands[2].low = ::client_priority - 1;
345 bands[2].high = ::client_priority + 1;
347 policies.length (policies.length () + 1);
348 policies[policies.length () - 1] =
349 this->rt_orb_->create_priority_banded_connection_policy (bands);
351 (this->*function) (policies,
352 server_priority,
353 client_priority,
354 test_name);
357 void
358 server::test_no_lanes_poa (CORBA::Short server_priority,
359 CORBA::Short client_priority,
360 server::test_function function,
361 const char *test_name)
363 RTCORBA::ThreadpoolId threadpool_id =
364 this->rt_orb_->create_threadpool (stacksize,
365 static_threads,
366 dynamic_threads,
367 default_thread_priority,
368 allow_request_buffering,
369 max_buffered_requests,
370 max_request_buffer_size);
372 CORBA::Policy_var threadpool_policy =
373 this->rt_orb_->create_threadpool_policy (threadpool_id);
375 CORBA::PolicyList policies;
376 policies.length (1);
377 policies[0] =
378 threadpool_policy;
380 (this->*function) (policies,
381 server_priority,
382 client_priority,
383 test_name);
386 void
387 server::test_lanes_poa (CORBA::Short server_priority,
388 CORBA::Short client_priority,
389 server::test_function function,
390 const char *test_name)
392 RTCORBA::ThreadpoolLanes lanes;
393 lanes.length (3);
395 lanes[0].lane_priority = ::client_priority;
396 lanes[0].static_threads = static_threads;
397 lanes[0].dynamic_threads = dynamic_threads;
399 lanes[1].lane_priority = ::server_priority;
400 lanes[1].static_threads = static_threads;
401 lanes[1].dynamic_threads = dynamic_threads;
403 lanes[2].lane_priority = default_thread_priority;
404 lanes[2].static_threads = static_threads;
405 lanes[2].dynamic_threads = dynamic_threads;
407 RTCORBA::ThreadpoolId threadpool_id =
408 this->rt_orb_->create_threadpool_with_lanes (stacksize,
409 lanes,
410 allow_borrowing,
411 allow_request_buffering,
412 max_buffered_requests,
413 max_request_buffer_size);
415 CORBA::Policy_var threadpool_policy =
416 this->rt_orb_->create_threadpool_policy (threadpool_id);
418 CORBA::PolicyList policies;
419 policies.length (1);
420 policies[0] =
421 threadpool_policy;
423 (this->*function) (policies,
424 server_priority,
425 client_priority,
426 test_name);
429 void
430 server::test_no_bands_client_propagated_poa (CORBA::PolicyList &policies,
431 CORBA::Short server_priority,
432 CORBA::Short client_priority,
433 const char *test_name)
435 policies.length (policies.length () + 1);
436 policies[policies.length () - 1] =
437 this->rt_orb_->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
438 default_thread_priority);
440 PortableServer::POA_var poa =
441 this->root_poa_->create_POA (test_name,
442 this->poa_manager_.in (),
443 policies);
445 RTPortableServer::POA_var rt_poa =
446 RTPortableServer::POA::_narrow (poa.in ());
448 test_i *servant = 0;
449 ACE_NEW_THROW_EX (servant,
450 test_i (this->orb_.in (),
451 poa.in (),
452 server_priority,
453 client_priority),
454 CORBA::NO_MEMORY ());
456 PortableServer::ServantBase_var safe_servant (servant);
458 PortableServer::ObjectId_var id =
459 rt_poa->activate_object (servant);
461 CORBA::Object_var object =
462 poa->id_to_reference (id.in ());
464 write_iors_to_file (object.in (),
465 this->orb_.in (),
466 test_name);
469 void
470 server::test_bands_client_propagated_poa (CORBA::PolicyList &policies,
471 CORBA::Short server_priority,
472 CORBA::Short client_priority,
473 const char *test_name)
475 this->test_bands_poa (policies,
476 server_priority,
477 client_priority,
478 &server::test_no_bands_client_propagated_poa,
479 test_name);
482 void
483 server::test_default_pool_no_bands_client_propagated_poa ()
485 this->test_default_pool_poa (::client_priority + 1,
486 ::client_priority + 1,
487 &server::test_no_bands_client_propagated_poa,
488 "default_pool_no_bands_client_propagated");
491 void
492 server::test_no_lanes_no_bands_client_propagated_poa ()
494 this->test_no_lanes_poa (::client_priority - 1,
495 ::client_priority - 1,
496 &server::test_no_bands_client_propagated_poa,
497 "no_lanes_no_bands_client_propagated");
500 void
501 server::test_lanes_no_bands_client_propagated_poa ()
503 this->test_lanes_poa (::client_priority,
504 ::client_priority,
505 &server::test_no_bands_client_propagated_poa,
506 "lanes_no_bands_client_propagated");
509 void
510 server::test_default_pool_bands_client_propagated_poa ()
512 this->test_default_pool_poa (::client_priority + 1,
513 ::client_priority + 1,
514 &server::test_bands_client_propagated_poa,
515 "default_pool_bands_client_propagated");
518 void
519 server::test_no_lanes_bands_client_propagated_poa ()
521 this->test_no_lanes_poa (::client_priority - 1,
522 ::client_priority - 1,
523 &server::test_bands_client_propagated_poa,
524 "no_lanes_bands_client_propagated");
527 void
528 server::test_lanes_bands_client_propagated_poa ()
530 this->test_lanes_poa (::client_priority,
531 ::client_priority + 1,
532 &server::test_bands_client_propagated_poa,
533 "lanes_bands_client_propagated");
536 void
537 server::test_no_bands_server_declared_poa (CORBA::PolicyList &policies,
538 CORBA::Short server_priority,
539 CORBA::Short client_priority,
540 const char *test_name)
542 policies.length (policies.length () + 1);
543 policies[policies.length () - 1] =
544 this->rt_orb_->create_priority_model_policy (RTCORBA::SERVER_DECLARED,
545 default_thread_priority);
547 PortableServer::POA_var poa =
548 this->root_poa_->create_POA (test_name,
549 this->poa_manager_.in (),
550 policies);
552 RTPortableServer::POA_var rt_poa =
553 RTPortableServer::POA::_narrow (poa.in ());
555 test_i *servant = 0;
556 ACE_NEW_THROW_EX (servant,
557 test_i (this->orb_.in (),
558 poa.in (),
559 server_priority,
560 client_priority),
561 CORBA::NO_MEMORY ());
563 PortableServer::ServantBase_var safe_servant (servant);
565 PortableServer::ObjectId_var id =
566 rt_poa->activate_object_with_priority (servant,
567 ::server_priority);
569 CORBA::Object_var object =
570 poa->id_to_reference (id.in ());
572 write_iors_to_file (object.in (),
573 this->orb_.in (),
574 test_name);
577 void
578 server::test_bands_server_declared_poa (CORBA::PolicyList &policies,
579 CORBA::Short server_priority,
580 CORBA::Short client_priority,
581 const char *test_name)
583 this->test_bands_poa (policies,
584 server_priority,
585 client_priority,
586 &server::test_no_bands_server_declared_poa,
587 test_name);
590 void
591 server::test_default_pool_no_bands_server_declared_poa ()
593 this->test_default_pool_poa (::server_priority,
594 ::client_priority + 1,
595 &server::test_no_bands_server_declared_poa,
596 "default_pool_no_bands_server_declared");
599 void
600 server::test_no_lanes_no_bands_server_declared_poa ()
602 this->test_no_lanes_poa (::server_priority,
603 ::client_priority - 1,
604 &server::test_no_bands_server_declared_poa,
605 "no_lanes_no_bands_server_declared");
608 void
609 server::test_lanes_no_bands_server_declared_poa ()
611 this->test_lanes_poa (::server_priority,
612 ::client_priority + 1,
613 &server::test_no_bands_server_declared_poa,
614 "lanes_no_bands_server_declared");
617 void
618 server::test_default_pool_bands_server_declared_poa ()
620 this->test_default_pool_poa (::server_priority,
621 ::client_priority - 1,
622 &server::test_bands_server_declared_poa,
623 "default_pool_bands_server_declared");
626 void
627 server::test_no_lanes_bands_server_declared_poa ()
629 this->test_no_lanes_poa (::server_priority,
630 ::client_priority + 1,
631 &server::test_bands_server_declared_poa,
632 "no_lanes_bands_server_declared");
635 void
636 server::test_lanes_bands_server_declared_poa ()
638 this->test_lanes_poa (::server_priority,
639 ::client_priority - 1,
640 &server::test_bands_server_declared_poa,
641 "lanes_bands_server_declared");
644 class Task : public ACE_Task_Base
646 public:
647 Task (ACE_Thread_Manager &thread_manager,
648 CORBA::ORB_ptr orb);
650 int svc ();
652 CORBA::ORB_var orb_;
655 Task::Task (ACE_Thread_Manager &thread_manager,
656 CORBA::ORB_ptr orb)
657 : ACE_Task_Base (&thread_manager),
658 orb_ (CORBA::ORB::_duplicate (orb))
663 Task::svc ()
667 CORBA::Object_var object =
668 this->orb_->resolve_initial_references ("RTORB");
670 RTCORBA::RTORB_var rt_orb =
671 RTCORBA::RTORB::_narrow (object.in ());
673 // The following sets the current thread to the lowest priority
674 // for this scheduling policy. This will give us the biggest
675 // range on NT since the default priority is 0 where as the
676 // lowest priority is -15.
677 ACE_hthread_t current_thread;
678 ACE_Thread::self (current_thread);
680 int sched_policy =
681 this->orb_->orb_core ()->orb_params ()->ace_sched_policy ();
683 int minimum_priority =
684 ACE_Sched_Params::priority_min (sched_policy);
686 int result =
687 ACE_Thread::setprio (current_thread,
688 minimum_priority);
689 if (result != 0)
690 return result;
692 object =
693 this->orb_->resolve_initial_references ("RTCurrent");
695 RTCORBA::Current_var current =
696 RTCORBA::Current::_narrow (object.in ());
698 default_thread_priority =
699 get_implicit_thread_CORBA_priority (this->orb_.in ());
701 client_priority =
702 default_thread_priority + 2;
704 server_priority =
705 default_thread_priority + 5;
707 object =
708 this->orb_->resolve_initial_references ("RootPOA");
710 PortableServer::POA_var root_poa =
711 PortableServer::POA::_narrow (object.in ());
713 PortableServer::POAManager_var poa_manager =
714 root_poa->the_POAManager ();
716 server server (this->orb_.in (),
717 rt_orb.in (),
718 root_poa.in (),
719 poa_manager.in ());
721 server.test_root_poa ();
723 server.test_child_poa ();
725 server.test_default_pool_no_bands_client_propagated_poa ();
727 server.test_default_pool_no_bands_server_declared_poa ();
729 server.test_no_lanes_no_bands_client_propagated_poa ();
731 server.test_no_lanes_no_bands_server_declared_poa ();
733 server.test_lanes_no_bands_client_propagated_poa ();
735 server.test_lanes_no_bands_server_declared_poa ();
737 server.test_default_pool_bands_client_propagated_poa ();
739 server.test_default_pool_bands_server_declared_poa ();
741 server.test_no_lanes_bands_client_propagated_poa ();
743 server.test_no_lanes_bands_server_declared_poa ();
745 server.test_lanes_bands_client_propagated_poa ();
747 server.test_lanes_bands_server_declared_poa ();
749 poa_manager->activate ();
751 this->orb_->run ();
753 this->orb_->destroy ();
755 catch (const CORBA::Exception& ex)
757 ex._tao_print_exception ("Exception caught");
758 return -1;
761 return 0;
765 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
769 CORBA::ORB_var orb =
770 CORBA::ORB_init (argc, argv);
772 int result =
773 parse_args (argc, argv);
774 if (result != 0)
775 return result;
777 // Make sure we can support multiple priorities that are required
778 // for this test.
779 if (!check_supported_priorities (orb.in ()))
780 return 2;
782 // Thread Manager for managing task.
783 ACE_Thread_Manager thread_manager;
785 // Create task.
786 Task task (thread_manager,
787 orb.in ());
789 // Task activation flags.
790 long flags =
791 THR_NEW_LWP |
792 THR_JOINABLE |
793 orb->orb_core ()->orb_params ()->thread_creation_flags ();
795 // Activate task.
796 result =
797 task.activate (flags);
798 if (result == -1)
800 if (errno == EPERM)
802 ACE_ERROR_RETURN ((LM_ERROR,
803 "Cannot create thread with scheduling policy %C\n"
804 "because the user does not have the appropriate privileges, terminating program....\n"
805 "Check svc.conf options and/or run as root\n",
806 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
809 else
810 // Unexpected error.
811 ACE_ASSERT (0);
814 // Wait for task to exit.
815 result =
816 thread_manager.wait ();
817 ACE_ASSERT (result != -1);
819 catch (const CORBA::Exception& ex)
821 ex._tao_print_exception ("Exception caught");
822 return -1;
825 return 0;