Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Collocation / Collocation.cpp
blob98494b81b1d2b9e25dad045bc2991650b5ec4ae1
1 #include "ace/Get_Opt.h"
2 #include "ace/Array_Base.h"
3 #include "ace/Task.h"
4 #include "tao/ORB_Core.h"
5 #include "tao/RTCORBA/Thread_Pool.h"
6 #include "testS.h"
7 #include "tao/RTPortableServer/RTPortableServer.h"
8 #include "../check_supported_priorities.cpp"
10 RTCORBA::Priority default_thread_priority;
12 class test_i;
14 struct Test_Object_And_Servant
16 test_var object_;
17 test_i *servant_;
20 typedef ACE_Array_Base<Test_Object_And_Servant> Tests;
22 void
23 setup_test_parameters (Test_Object_And_Servant *test,
24 RTCORBA::Current_ptr current,
25 CORBA::ORB_ptr orb);
26 class test_i :
27 public POA_test
29 public:
30 test_i (CORBA::ORB_ptr orb,
31 PortableServer::POA_ptr poa,
32 Tests &tests);
34 void start ();
36 void method ();
38 PortableServer::POA_ptr _default_POA ();
40 int client_propagated ();
42 void client_propagated (int);
44 int server_declared ();
46 void server_declared (int);
48 RTCORBA::Priority server_priority ();
50 void server_priority (RTCORBA::Priority);
52 int in_lane ();
54 void in_lane (int);
56 int serviced_by_rt_tp ();
58 void serviced_by_rt_tp (int);
60 void thread_info (const char *method_name);
62 void invocation_pool_and_lane (CORBA::ULong pool,
63 CORBA::ULong lane);
65 void invocation_pool (CORBA::ULong pool);
67 void invocation_lane (CORBA::ULong lane);
69 RTCORBA::Priority invocation_priority ();
71 void invocation_priority (RTCORBA::Priority);
73 private:
74 CORBA::ORB_var orb_;
75 PortableServer::POA_var poa_;
76 Tests &tests_;
78 int client_propagated_;
79 int server_declared_;
80 RTCORBA::Priority server_priority_;
81 int in_lane_;
82 int serviced_by_rt_tp_;
84 CORBA::ULong pool_;
85 CORBA::ULong lane_;
86 RTCORBA::Priority invocation_priority_;
88 RTCORBA::Current_var current_;
91 test_i::test_i (CORBA::ORB_ptr orb,
92 PortableServer::POA_ptr poa,
93 Tests &tests)
94 : orb_ (CORBA::ORB::_duplicate (orb)),
95 poa_ (PortableServer::POA::_duplicate (poa)),
96 tests_ (tests),
97 client_propagated_ (0),
98 server_declared_ (0),
99 server_priority_ (0),
100 serviced_by_rt_tp_ (1),
101 invocation_priority_ (0)
103 CORBA::Object_var object =
104 this->orb_->resolve_initial_references ("RTCurrent");
106 this->current_ =
107 RTCORBA::Current::_narrow (object.in ());
111 test_i::client_propagated ()
113 return this->client_propagated_;
116 void
117 test_i::client_propagated (int client_propagated)
119 this->client_propagated_ = client_propagated;
123 test_i::server_declared ()
125 return this->server_declared_;
128 void
129 test_i::server_declared (int server_declared)
131 this->server_declared_ = server_declared;
134 RTCORBA::Priority
135 test_i::server_priority ()
137 return this->server_priority_;
140 void
141 test_i::server_priority (RTCORBA::Priority server_priority)
143 this->server_priority_ = server_priority;
147 test_i::in_lane ()
149 return this->in_lane_;
152 void
153 test_i::in_lane (int in_lane)
155 this->in_lane_ = in_lane;
159 test_i::serviced_by_rt_tp ()
161 return this->serviced_by_rt_tp_;
164 void
165 test_i::serviced_by_rt_tp (int serviced_by_rt_tp)
167 this->serviced_by_rt_tp_ = serviced_by_rt_tp;
170 void
171 test_i::invocation_pool_and_lane (CORBA::ULong pool,
172 CORBA::ULong lane)
174 this->pool_ = pool;
175 this->lane_ = lane;
178 void
179 test_i::invocation_pool (CORBA::ULong pool)
181 this->pool_ = pool;
184 void
185 test_i::invocation_lane (CORBA::ULong lane)
187 this->lane_ = lane;
190 RTCORBA::Priority
191 test_i::invocation_priority ()
193 return this->invocation_priority_;
196 void
197 test_i::invocation_priority (RTCORBA::Priority invocation_priority)
199 this->invocation_priority_ = invocation_priority;
202 void
203 test_i::start ()
205 ACE_DEBUG ((LM_DEBUG,
206 "\n"));
208 this->thread_info ("test_i::start");
210 ACE_DEBUG ((LM_DEBUG,
211 "\n"));
213 Tests::ITERATOR iterator (this->tests_);
214 while (!iterator.done ())
216 Test_Object_And_Servant *test = 0;
217 iterator.next (test);
219 setup_test_parameters (test,
220 this->current_.in (),
221 this->orb_.in ());
223 test->object_->method ();
225 CORBA::String_var ior =
226 this->orb_->object_to_string (test->object_.in ());
228 CORBA::Object_var object =
229 this->orb_->string_to_object (ior.in ());
231 test_var test_from_string =
232 test::_narrow (object.in ());
234 test_from_string->method ();
236 iterator.advance ();
240 void
241 test_i::thread_info (const char *method_name)
243 // Get the ORB_Core's TSS resources.
244 TAO_ORB_Core_TSS_Resources *tss =
245 this->orb_->orb_core ()->get_tss_resources ();
247 /// Get the lane attribute in TSS.
248 TAO_Thread_Lane *lane =
249 (TAO_Thread_Lane *) tss->lane_;
251 if (lane)
253 ACE_DEBUG ((LM_DEBUG,
254 "(%t) %s (pool = %d; lane = %d); priority = %d (should be %d)\n",
255 method_name,
256 lane->pool ().id (),
257 lane->id (),
258 this->current_->the_priority (),
259 this->invocation_priority ()));
261 ACE_ASSERT (this->pool_ == lane->pool ().id ());
262 ACE_ASSERT (this->lane_ == lane->id ());
263 ACE_ASSERT (this->current_->the_priority () ==
264 this->invocation_priority ());
266 else
268 ACE_DEBUG ((LM_DEBUG,
269 "(%t) %s (default thread pool); priority = %d (should be %d)\n",
270 method_name,
271 this->current_->the_priority (),
272 this->invocation_priority ()));
274 ACE_ASSERT (this->pool_ == 0);
275 ACE_ASSERT (this->lane_ == 0);
276 ACE_ASSERT (this->current_->the_priority () ==
277 this->invocation_priority ());
281 void
282 test_i::method ()
284 this->thread_info ("test_i::method");
287 PortableServer::POA_ptr
288 test_i::_default_POA ()
290 return PortableServer::POA::_duplicate (this->poa_.in ());
293 void
294 setup_test_parameters (Test_Object_And_Servant *test,
295 RTCORBA::Current_ptr current,
296 CORBA::ORB_ptr orb)
298 CORBA::Short current_thread_priority;
302 current_thread_priority =
303 current->the_priority ();
305 catch (const CORBA::INITIALIZE&)
309 current_thread_priority = get_implicit_thread_CORBA_priority (orb);
311 catch (const CORBA::Exception &ex)
313 ex._tao_print_exception ("Exception in ::setup_test_parameters calling ::get_implicit_thread_CORBA_priority: ");
314 throw;
317 catch (const CORBA::Exception& dc)
319 dc._tao_print_exception ("Exception in ::setup_test_parameters calling current->the_priority(): ");
320 throw;
323 current->the_priority (current_thread_priority);
325 if (!test->servant_->serviced_by_rt_tp () ||
326 test->servant_->client_propagated ())
328 test->servant_->invocation_priority (current_thread_priority);
330 else if (test->servant_->server_declared ())
332 test->servant_->invocation_priority (test->servant_->server_priority ());
334 else
336 test->servant_->invocation_priority (default_thread_priority);
339 if (test->servant_->client_propagated () &&
340 test->servant_->in_lane ())
342 if (current_thread_priority == default_thread_priority)
343 test->servant_->invocation_lane (0);
344 else
345 test->servant_->invocation_lane (1);
348 if (!test->servant_->serviced_by_rt_tp ())
350 // Get the ORB_Core's TSS resources.
351 TAO_ORB_Core_TSS_Resources *tss =
352 orb->orb_core ()->get_tss_resources ();
354 /// Get the lane attribute in TSS.
355 TAO_Thread_Lane *lane =
356 (TAO_Thread_Lane *) tss->lane_;
358 if (lane)
360 test->servant_->invocation_pool_and_lane (lane->pool ().id (),
361 lane->id ());
363 else
365 test->servant_->invocation_pool_and_lane (0, 0);
370 class Server
372 public:
373 Server (CORBA::ORB_ptr orb);
375 void create_servant_in_root_poa ();
376 void create_poa_and_servant_with_tp_policy (const char *poa_name,
377 int set_priority_model,
378 RTCORBA::PriorityModel priority_model);
379 void create_poa_and_servant_with_tp_with_lanes_policy (const char *poa_name,
380 RTCORBA::PriorityModel priority_model);
381 void test ();
382 void start_testing ();
384 //FUZZ: disable check_for_lack_ACE_OS
385 void shutdown ();
386 //FUZZ: enable check_for_lack_ACE_OS
388 CORBA::ORB_var orb_;
389 RTCORBA::RTORB_var rt_orb_;
390 PortableServer::POA_var root_poa_;
391 PortableServer::POAManager_var poa_manager_;
392 RTCORBA::Current_var current_;
394 Tests tests_;
396 CORBA::ULong stacksize_;
397 CORBA::ULong static_threads_;
398 CORBA::ULong dynamic_threads_;
399 CORBA::Boolean allow_request_buffering_;
400 CORBA::Boolean allow_borrowing_;
401 CORBA::ULong max_buffered_requests_;
402 CORBA::ULong max_request_buffer_size_;
405 Server::Server (CORBA::ORB_ptr orb)
406 : orb_ (CORBA::ORB::_duplicate (orb)),
407 stacksize_ (0),
408 static_threads_ (1),
409 dynamic_threads_ (0),
410 allow_request_buffering_ (0),
411 allow_borrowing_ (0),
412 max_buffered_requests_ (0),
413 max_request_buffer_size_ (0)
415 CORBA::Object_var object =
416 this->orb_->resolve_initial_references ("RTORB");
418 this->rt_orb_ =
419 RTCORBA::RTORB::_narrow (object.in ());
421 object =
422 this->orb_->resolve_initial_references ("RTCurrent");
424 this->current_ =
425 RTCORBA::Current::_narrow (object.in ());
427 default_thread_priority =
428 get_implicit_thread_CORBA_priority (this->orb_.in ());
430 object =
431 this->orb_->resolve_initial_references ("RootPOA");
433 this->root_poa_ =
434 PortableServer::POA::_narrow (object.in ());
436 this->poa_manager_ =
437 this->root_poa_->the_POAManager ();
439 this->poa_manager_->activate ();
442 void
443 Server::create_servant_in_root_poa ()
445 test_i *servant = 0;
446 ACE_NEW_THROW_EX (servant,
447 test_i (this->orb_.in (),
448 this->root_poa_.in (),
449 this->tests_),
450 CORBA::NO_MEMORY ());
452 servant->invocation_pool_and_lane (0, 0);
453 servant->in_lane (0);
454 servant->serviced_by_rt_tp (0);
456 PortableServer::ServantBase_var safe_servant (servant);
458 this->tests_.size (this->tests_.size () + 1);
459 this->tests_[this->tests_.size () - 1].servant_ =
460 servant;
461 this->tests_[this->tests_.size () - 1].object_ =
462 servant->_this ();
465 void
466 Server::create_poa_and_servant_with_tp_policy (const char *poa_name,
467 int set_priority_model,
468 RTCORBA::PriorityModel priority_model)
470 RTCORBA::ThreadpoolId threadpool_id =
471 this->rt_orb_->create_threadpool (this->stacksize_,
472 this->static_threads_,
473 this->dynamic_threads_,
474 default_thread_priority,
475 this->allow_request_buffering_,
476 this->max_buffered_requests_,
477 this->max_request_buffer_size_);
479 CORBA::Policy_var threadpool_policy =
480 this->rt_orb_->create_threadpool_policy (threadpool_id);
482 CORBA::Policy_var priority_model_policy =
483 this->rt_orb_->create_priority_model_policy (priority_model,
484 default_thread_priority);
486 CORBA::PolicyList policies;
488 policies.length (policies.length () + 1);
489 policies[policies.length () - 1] =
490 threadpool_policy;
492 if (set_priority_model)
494 policies.length (policies.length () + 1);
495 policies[policies.length () - 1] =
496 priority_model_policy;
499 PortableServer::POA_var poa =
500 this->root_poa_->create_POA (poa_name,
501 this->poa_manager_.in (),
502 policies);
504 RTPortableServer::POA_var rt_poa =
505 RTPortableServer::POA::_narrow (poa.in ());
507 test_i *servant1 = 0;
508 ACE_NEW_THROW_EX (servant1,
509 test_i (this->orb_.in (),
510 poa.in (),
511 this->tests_),
512 CORBA::NO_MEMORY ());
514 servant1->in_lane (0);
516 PortableServer::ObjectId_var id1;
518 if (set_priority_model)
520 if (priority_model == RTCORBA::CLIENT_PROPAGATED)
522 servant1->invocation_pool_and_lane (1, 0);
523 servant1->client_propagated (1);
525 id1 =
526 rt_poa->activate_object (servant1);
528 else
530 test_i *servant2 = 0;
532 ACE_NEW_THROW_EX (servant2,
533 test_i (this->orb_.in (),
534 poa.in (),
535 this->tests_),
536 CORBA::NO_MEMORY ());
538 servant2->in_lane (0);
540 PortableServer::ServantBase_var safe_servant2 (servant2);
542 PortableServer::ObjectId_var id2;
544 servant1->invocation_pool_and_lane (2, 0);
545 servant2->invocation_pool_and_lane (2, 0);
546 servant1->server_declared (1);
547 servant2->server_declared (1);
548 servant1->server_priority (default_thread_priority + 1);
549 servant2->server_priority (default_thread_priority);
551 id1 =
552 rt_poa->activate_object_with_priority (servant1,
553 default_thread_priority + 1);
555 id2 =
556 rt_poa->activate_object_with_priority (servant2,
557 default_thread_priority);
559 CORBA::Object_var object2 =
560 poa->id_to_reference (id2.in ());
562 this->tests_.size (this->tests_.size () + 1);
563 this->tests_[this->tests_.size () - 1].object_ =
564 test::_narrow (object2.in ());
565 this->tests_[this->tests_.size () - 1].servant_ =
566 servant2;
569 else
571 servant1->invocation_pool_and_lane (3, 0);
573 id1 =
574 rt_poa->activate_object (servant1);
577 PortableServer::ServantBase_var safe_servant (servant1);
579 CORBA::Object_var object1 =
580 poa->id_to_reference (id1.in ());
582 this->tests_.size (this->tests_.size () + 1);
583 this->tests_[this->tests_.size () - 1].object_ =
584 test::_narrow (object1.in ());
585 this->tests_[this->tests_.size () - 1].servant_ =
586 servant1;
589 void
590 Server::create_poa_and_servant_with_tp_with_lanes_policy (const char *poa_name,
591 RTCORBA::PriorityModel priority_model)
593 RTCORBA::ThreadpoolLanes lanes (2);
594 lanes.length (2);
596 lanes[0].lane_priority = default_thread_priority;
597 lanes[0].static_threads = this->static_threads_;
598 lanes[0].dynamic_threads = this->dynamic_threads_;
600 lanes[1].lane_priority = default_thread_priority + 1;
601 lanes[1].static_threads = this->static_threads_;
602 lanes[1].dynamic_threads = this->dynamic_threads_;
604 RTCORBA::ThreadpoolId threadpool_id =
605 this->rt_orb_->create_threadpool_with_lanes (this->stacksize_,
606 lanes,
607 this->allow_borrowing_,
608 this->allow_request_buffering_,
609 this->max_buffered_requests_,
610 this->max_request_buffer_size_);
612 CORBA::Policy_var threadpool_policy =
613 this->rt_orb_->create_threadpool_policy (threadpool_id);
615 CORBA::Policy_var priority_model_policy =
616 this->rt_orb_->create_priority_model_policy (priority_model,
617 default_thread_priority);
619 CORBA::PolicyList policies;
621 policies.length (policies.length () + 1);
622 policies[policies.length () - 1] =
623 threadpool_policy;
625 policies.length (policies.length () + 1);
626 policies[policies.length () - 1] =
627 priority_model_policy;
629 PortableServer::POA_var poa =
630 this->root_poa_->create_POA (poa_name,
631 this->poa_manager_.in (),
632 policies);
634 RTPortableServer::POA_var rt_poa =
635 RTPortableServer::POA::_narrow (poa.in ());
637 test_i *servant1 = 0;
638 ACE_NEW_THROW_EX (servant1,
639 test_i (this->orb_.in (),
640 poa.in (),
641 this->tests_),
642 CORBA::NO_MEMORY ());
644 servant1->in_lane (1);
646 PortableServer::ServantBase_var safe_servant1 (servant1);
648 PortableServer::ObjectId_var id1;
650 if (priority_model == RTCORBA::CLIENT_PROPAGATED)
652 servant1->invocation_pool (4);
653 servant1->client_propagated (1);
655 id1 =
656 rt_poa->activate_object (servant1);
658 else
660 test_i *servant2 = 0;
662 ACE_NEW_THROW_EX (servant2,
663 test_i (this->orb_.in (),
664 poa.in (),
665 this->tests_),
666 CORBA::NO_MEMORY ());
668 servant2->in_lane (1);
670 PortableServer::ServantBase_var safe_servant2 (servant2);
672 PortableServer::ObjectId_var id2;
674 servant1->invocation_pool_and_lane (5, 1);
675 servant2->invocation_pool_and_lane (5, 0);
676 servant1->server_declared (1);
677 servant2->server_declared (1);
678 servant1->server_priority (default_thread_priority + 1);
679 servant2->server_priority (default_thread_priority);
681 id1 =
682 rt_poa->activate_object_with_priority (servant1,
683 default_thread_priority + 1);
685 id2 =
686 rt_poa->activate_object_with_priority (servant2,
687 default_thread_priority);
689 CORBA::Object_var object2 =
690 poa->id_to_reference (id2.in ());
692 this->tests_.size (this->tests_.size () + 1);
693 this->tests_[this->tests_.size () - 1].object_ =
694 test::_narrow (object2.in ());
695 this->tests_[this->tests_.size () - 1].servant_ =
696 servant2;
699 CORBA::Object_var object1 =
700 poa->id_to_reference (id1.in ());
702 this->tests_.size (this->tests_.size () + 1);
703 this->tests_[this->tests_.size () - 1].object_ =
704 test::_narrow (object1.in ());
705 this->tests_[this->tests_.size () - 1].servant_ =
706 servant1;
709 void
710 Server::start_testing ()
712 Tests::ITERATOR iterator (this->tests_);
713 while (!iterator.done ())
715 Test_Object_And_Servant *test = 0;
716 iterator.next (test);
718 setup_test_parameters (test,
719 this->current_.in (),
720 this->orb_.in ());
722 test->object_->start ();
724 iterator.advance ();
728 void
729 Server::test ()
731 this->start_testing ();
733 ACE_DEBUG ((LM_DEBUG,
734 "\n\n*** Changing priority to be higher ***\n\n"));
736 this->current_->the_priority (default_thread_priority + 1);
738 this->start_testing ();
741 void
742 Server::shutdown ()
744 this->orb_->shutdown (true);
746 this->orb_->destroy ();
749 class Task : public ACE_Task_Base
751 public:
752 Task (ACE_Thread_Manager &thread_manager,
753 CORBA::ORB_ptr orb);
755 int svc ();
757 CORBA::ORB_var orb_;
760 Task::Task (ACE_Thread_Manager &thread_manager,
761 CORBA::ORB_ptr orb)
762 : ACE_Task_Base (&thread_manager),
763 orb_ (CORBA::ORB::_duplicate (orb))
768 Task::svc ()
772 Server server (this->orb_.in ());
774 server.create_servant_in_root_poa ();
776 server.create_poa_and_servant_with_tp_policy ("tp_client_propagated_poa",
778 RTCORBA::CLIENT_PROPAGATED);
780 server.create_poa_and_servant_with_tp_policy ("tp_server_declared_poa",
782 RTCORBA::SERVER_DECLARED);
784 server.create_poa_and_servant_with_tp_policy ("tp_no_priority_model_poa",
786 RTCORBA::CLIENT_PROPAGATED);
788 server.create_poa_and_servant_with_tp_with_lanes_policy ("tp_with_lanes_client_propagated_poa",
789 RTCORBA::CLIENT_PROPAGATED);
791 server.create_poa_and_servant_with_tp_with_lanes_policy ("tp_with_lanes_server_declared_poa",
792 RTCORBA::SERVER_DECLARED);
794 server.test ();
796 server.shutdown ();
798 catch (const CORBA::Exception& ex)
800 ex._tao_print_exception ("Exception caught:");
801 return -1;
804 return 0;
808 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
812 CORBA::ORB_var orb =
813 CORBA::ORB_init (argc,
814 argv);
816 // Make sure we can support multiple priorities that are
817 // required for this test.
818 if (!check_supported_priorities (orb.in ()))
819 return 2;
821 // Thread Manager for managing task.
822 ACE_Thread_Manager thread_manager;
824 // Create task.
825 Task task (thread_manager,
826 orb.in ());
828 // Task activation flags.
829 long flags =
830 THR_NEW_LWP |
831 THR_JOINABLE |
832 orb->orb_core ()->orb_params ()->thread_creation_flags ();
834 // Activate task.
835 int result =
836 task.activate (flags);
837 if (result == -1)
839 if (errno == EPERM)
841 ACE_ERROR_RETURN ((LM_ERROR,
842 "Cannot create thread with scheduling policy %s\n"
843 "because the user does not have the appropriate privileges, terminating program....\n"
844 "Check svc.conf options and/or run as root\n",
845 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
848 else
849 // Unexpected error.
850 ACE_ASSERT (0);
853 // Wait for task to exit.
854 result =
855 thread_manager.wait ();
856 ACE_ASSERT (result != -1);
858 catch (const CORBA::Exception& ex)
860 ex._tao_print_exception ("Exception caught:");
861 return -1;
864 return 0;