ACE+TAO-7_0_8
[ACE_TAO.git] / TAO / tests / RTCORBA / Collocation / Collocation.cpp
blobe164eda6df7b1b24ca1d469df881784566043840
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 (void);
36 void method (void);
38 PortableServer::POA_ptr _default_POA (void);
40 int client_propagated (void);
42 void client_propagated (int);
44 int server_declared (void);
46 void server_declared (int);
48 RTCORBA::Priority server_priority (void);
50 void server_priority (RTCORBA::Priority);
52 int in_lane (void);
54 void in_lane (int);
56 int serviced_by_rt_tp (void);
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 (void);
71 void invocation_priority (RTCORBA::Priority);
73 private:
75 CORBA::ORB_var orb_;
76 PortableServer::POA_var poa_;
77 Tests &tests_;
79 int client_propagated_;
80 int server_declared_;
81 RTCORBA::Priority server_priority_;
82 int in_lane_;
83 int serviced_by_rt_tp_;
85 CORBA::ULong pool_;
86 CORBA::ULong lane_;
87 RTCORBA::Priority invocation_priority_;
89 RTCORBA::Current_var current_;
92 test_i::test_i (CORBA::ORB_ptr orb,
93 PortableServer::POA_ptr poa,
94 Tests &tests)
95 : orb_ (CORBA::ORB::_duplicate (orb)),
96 poa_ (PortableServer::POA::_duplicate (poa)),
97 tests_ (tests),
98 client_propagated_ (0),
99 server_declared_ (0),
100 server_priority_ (0),
101 serviced_by_rt_tp_ (1),
102 invocation_priority_ (0)
104 CORBA::Object_var object =
105 this->orb_->resolve_initial_references ("RTCurrent");
107 this->current_ =
108 RTCORBA::Current::_narrow (object.in ());
112 test_i::client_propagated (void)
114 return this->client_propagated_;
117 void
118 test_i::client_propagated (int client_propagated)
120 this->client_propagated_ = client_propagated;
124 test_i::server_declared (void)
126 return this->server_declared_;
129 void
130 test_i::server_declared (int server_declared)
132 this->server_declared_ = server_declared;
135 RTCORBA::Priority
136 test_i::server_priority (void)
138 return this->server_priority_;
141 void
142 test_i::server_priority (RTCORBA::Priority server_priority)
144 this->server_priority_ = server_priority;
148 test_i::in_lane (void)
150 return this->in_lane_;
153 void
154 test_i::in_lane (int in_lane)
156 this->in_lane_ = in_lane;
160 test_i::serviced_by_rt_tp (void)
162 return this->serviced_by_rt_tp_;
165 void
166 test_i::serviced_by_rt_tp (int serviced_by_rt_tp)
168 this->serviced_by_rt_tp_ = serviced_by_rt_tp;
171 void
172 test_i::invocation_pool_and_lane (CORBA::ULong pool,
173 CORBA::ULong lane)
175 this->pool_ = pool;
176 this->lane_ = lane;
179 void
180 test_i::invocation_pool (CORBA::ULong pool)
182 this->pool_ = pool;
185 void
186 test_i::invocation_lane (CORBA::ULong lane)
188 this->lane_ = lane;
191 RTCORBA::Priority
192 test_i::invocation_priority (void)
194 return this->invocation_priority_;
197 void
198 test_i::invocation_priority (RTCORBA::Priority invocation_priority)
200 this->invocation_priority_ = invocation_priority;
203 void
204 test_i::start (void)
206 ACE_DEBUG ((LM_DEBUG,
207 "\n"));
209 this->thread_info ("test_i::start");
211 ACE_DEBUG ((LM_DEBUG,
212 "\n"));
214 Tests::ITERATOR iterator (this->tests_);
215 while (!iterator.done ())
217 Test_Object_And_Servant *test = 0;
218 iterator.next (test);
220 setup_test_parameters (test,
221 this->current_.in (),
222 this->orb_.in ());
224 test->object_->method ();
226 CORBA::String_var ior =
227 this->orb_->object_to_string (test->object_.in ());
229 CORBA::Object_var object =
230 this->orb_->string_to_object (ior.in ());
232 test_var test_from_string =
233 test::_narrow (object.in ());
235 test_from_string->method ();
237 iterator.advance ();
241 void
242 test_i::thread_info (const char *method_name)
244 // Get the ORB_Core's TSS resources.
245 TAO_ORB_Core_TSS_Resources *tss =
246 this->orb_->orb_core ()->get_tss_resources ();
248 /// Get the lane attribute in TSS.
249 TAO_Thread_Lane *lane =
250 (TAO_Thread_Lane *) tss->lane_;
252 if (lane)
254 ACE_DEBUG ((LM_DEBUG,
255 "(%t) %s (pool = %d; lane = %d); priority = %d (should be %d)\n",
256 method_name,
257 lane->pool ().id (),
258 lane->id (),
259 this->current_->the_priority (),
260 this->invocation_priority ()));
262 ACE_ASSERT (this->pool_ == lane->pool ().id ());
263 ACE_ASSERT (this->lane_ == lane->id ());
264 ACE_ASSERT (this->current_->the_priority () ==
265 this->invocation_priority ());
267 else
269 ACE_DEBUG ((LM_DEBUG,
270 "(%t) %s (default thread pool); priority = %d (should be %d)\n",
271 method_name,
272 this->current_->the_priority (),
273 this->invocation_priority ()));
275 ACE_ASSERT (this->pool_ == 0);
276 ACE_ASSERT (this->lane_ == 0);
277 ACE_ASSERT (this->current_->the_priority () ==
278 this->invocation_priority ());
282 void
283 test_i::method (void)
285 this->thread_info ("test_i::method");
288 PortableServer::POA_ptr
289 test_i::_default_POA (void)
291 return PortableServer::POA::_duplicate (this->poa_.in ());
294 void
295 setup_test_parameters (Test_Object_And_Servant *test,
296 RTCORBA::Current_ptr current,
297 CORBA::ORB_ptr orb)
299 CORBA::Short current_thread_priority;
303 current_thread_priority =
304 current->the_priority ();
306 catch (const CORBA::INITIALIZE&)
310 current_thread_priority = get_implicit_thread_CORBA_priority (orb);
312 catch (const CORBA::Exception &ex)
314 ex._tao_print_exception ("Exception in ::setup_test_parameters calling ::get_implicit_thread_CORBA_priority: ");
315 throw;
318 catch (const CORBA::Exception& dc)
320 dc._tao_print_exception ("Exception in ::setup_test_parameters calling current->the_priority(): ");
321 throw;
324 current->the_priority (current_thread_priority);
326 if (!test->servant_->serviced_by_rt_tp () ||
327 test->servant_->client_propagated ())
329 test->servant_->invocation_priority (current_thread_priority);
331 else if (test->servant_->server_declared ())
333 test->servant_->invocation_priority (test->servant_->server_priority ());
335 else
337 test->servant_->invocation_priority (default_thread_priority);
340 if (test->servant_->client_propagated () &&
341 test->servant_->in_lane ())
343 if (current_thread_priority == default_thread_priority)
344 test->servant_->invocation_lane (0);
345 else
346 test->servant_->invocation_lane (1);
349 if (!test->servant_->serviced_by_rt_tp ())
351 // Get the ORB_Core's TSS resources.
352 TAO_ORB_Core_TSS_Resources *tss =
353 orb->orb_core ()->get_tss_resources ();
355 /// Get the lane attribute in TSS.
356 TAO_Thread_Lane *lane =
357 (TAO_Thread_Lane *) tss->lane_;
359 if (lane)
361 test->servant_->invocation_pool_and_lane (lane->pool ().id (),
362 lane->id ());
364 else
366 test->servant_->invocation_pool_and_lane (0, 0);
371 class Server
373 public:
374 Server (CORBA::ORB_ptr orb);
376 void create_servant_in_root_poa (void);
377 void create_poa_and_servant_with_tp_policy (const char *poa_name,
378 int set_priority_model,
379 RTCORBA::PriorityModel priority_model);
380 void create_poa_and_servant_with_tp_with_lanes_policy (const char *poa_name,
381 RTCORBA::PriorityModel priority_model);
382 void test (void);
383 void start_testing (void);
385 //FUZZ: disable check_for_lack_ACE_OS
386 void shutdown (void);
387 //FUZZ: enable check_for_lack_ACE_OS
389 CORBA::ORB_var orb_;
390 RTCORBA::RTORB_var rt_orb_;
391 PortableServer::POA_var root_poa_;
392 PortableServer::POAManager_var poa_manager_;
393 RTCORBA::Current_var current_;
395 Tests tests_;
397 CORBA::ULong stacksize_;
398 CORBA::ULong static_threads_;
399 CORBA::ULong dynamic_threads_;
400 CORBA::Boolean allow_request_buffering_;
401 CORBA::Boolean allow_borrowing_;
402 CORBA::ULong max_buffered_requests_;
403 CORBA::ULong max_request_buffer_size_;
406 Server::Server (CORBA::ORB_ptr orb)
407 : orb_ (CORBA::ORB::_duplicate (orb)),
408 stacksize_ (0),
409 static_threads_ (1),
410 dynamic_threads_ (0),
411 allow_request_buffering_ (0),
412 allow_borrowing_ (0),
413 max_buffered_requests_ (0),
414 max_request_buffer_size_ (0)
416 CORBA::Object_var object =
417 this->orb_->resolve_initial_references ("RTORB");
419 this->rt_orb_ =
420 RTCORBA::RTORB::_narrow (object.in ());
422 object =
423 this->orb_->resolve_initial_references ("RTCurrent");
425 this->current_ =
426 RTCORBA::Current::_narrow (object.in ());
428 default_thread_priority =
429 get_implicit_thread_CORBA_priority (this->orb_.in ());
431 object =
432 this->orb_->resolve_initial_references ("RootPOA");
434 this->root_poa_ =
435 PortableServer::POA::_narrow (object.in ());
437 this->poa_manager_ =
438 this->root_poa_->the_POAManager ();
440 this->poa_manager_->activate ();
443 void
444 Server::create_servant_in_root_poa (void)
446 test_i *servant = 0;
447 ACE_NEW_THROW_EX (servant,
448 test_i (this->orb_.in (),
449 this->root_poa_.in (),
450 this->tests_),
451 CORBA::NO_MEMORY ());
453 servant->invocation_pool_and_lane (0, 0);
454 servant->in_lane (0);
455 servant->serviced_by_rt_tp (0);
457 PortableServer::ServantBase_var safe_servant (servant);
459 this->tests_.size (this->tests_.size () + 1);
460 this->tests_[this->tests_.size () - 1].servant_ =
461 servant;
462 this->tests_[this->tests_.size () - 1].object_ =
463 servant->_this ();
466 void
467 Server::create_poa_and_servant_with_tp_policy (const char *poa_name,
468 int set_priority_model,
469 RTCORBA::PriorityModel priority_model)
471 RTCORBA::ThreadpoolId threadpool_id =
472 this->rt_orb_->create_threadpool (this->stacksize_,
473 this->static_threads_,
474 this->dynamic_threads_,
475 default_thread_priority,
476 this->allow_request_buffering_,
477 this->max_buffered_requests_,
478 this->max_request_buffer_size_);
480 CORBA::Policy_var threadpool_policy =
481 this->rt_orb_->create_threadpool_policy (threadpool_id);
483 CORBA::Policy_var priority_model_policy =
484 this->rt_orb_->create_priority_model_policy (priority_model,
485 default_thread_priority);
487 CORBA::PolicyList policies;
489 policies.length (policies.length () + 1);
490 policies[policies.length () - 1] =
491 threadpool_policy;
493 if (set_priority_model)
495 policies.length (policies.length () + 1);
496 policies[policies.length () - 1] =
497 priority_model_policy;
500 PortableServer::POA_var poa =
501 this->root_poa_->create_POA (poa_name,
502 this->poa_manager_.in (),
503 policies);
505 RTPortableServer::POA_var rt_poa =
506 RTPortableServer::POA::_narrow (poa.in ());
508 test_i *servant1 = 0;
509 ACE_NEW_THROW_EX (servant1,
510 test_i (this->orb_.in (),
511 poa.in (),
512 this->tests_),
513 CORBA::NO_MEMORY ());
515 servant1->in_lane (0);
517 PortableServer::ObjectId_var id1;
519 if (set_priority_model)
521 if (priority_model == RTCORBA::CLIENT_PROPAGATED)
523 servant1->invocation_pool_and_lane (1, 0);
524 servant1->client_propagated (1);
526 id1 =
527 rt_poa->activate_object (servant1);
529 else
531 test_i *servant2 = 0;
533 ACE_NEW_THROW_EX (servant2,
534 test_i (this->orb_.in (),
535 poa.in (),
536 this->tests_),
537 CORBA::NO_MEMORY ());
539 servant2->in_lane (0);
541 PortableServer::ServantBase_var safe_servant2 (servant2);
543 PortableServer::ObjectId_var id2;
545 servant1->invocation_pool_and_lane (2, 0);
546 servant2->invocation_pool_and_lane (2, 0);
547 servant1->server_declared (1);
548 servant2->server_declared (1);
549 servant1->server_priority (default_thread_priority + 1);
550 servant2->server_priority (default_thread_priority);
552 id1 =
553 rt_poa->activate_object_with_priority (servant1,
554 default_thread_priority + 1);
556 id2 =
557 rt_poa->activate_object_with_priority (servant2,
558 default_thread_priority);
560 CORBA::Object_var object2 =
561 poa->id_to_reference (id2.in ());
563 this->tests_.size (this->tests_.size () + 1);
564 this->tests_[this->tests_.size () - 1].object_ =
565 test::_narrow (object2.in ());
566 this->tests_[this->tests_.size () - 1].servant_ =
567 servant2;
570 else
572 servant1->invocation_pool_and_lane (3, 0);
574 id1 =
575 rt_poa->activate_object (servant1);
578 PortableServer::ServantBase_var safe_servant (servant1);
580 CORBA::Object_var object1 =
581 poa->id_to_reference (id1.in ());
583 this->tests_.size (this->tests_.size () + 1);
584 this->tests_[this->tests_.size () - 1].object_ =
585 test::_narrow (object1.in ());
586 this->tests_[this->tests_.size () - 1].servant_ =
587 servant1;
590 void
591 Server::create_poa_and_servant_with_tp_with_lanes_policy (const char *poa_name,
592 RTCORBA::PriorityModel priority_model)
594 RTCORBA::ThreadpoolLanes lanes (2);
595 lanes.length (2);
597 lanes[0].lane_priority = default_thread_priority;
598 lanes[0].static_threads = this->static_threads_;
599 lanes[0].dynamic_threads = this->dynamic_threads_;
601 lanes[1].lane_priority = default_thread_priority + 1;
602 lanes[1].static_threads = this->static_threads_;
603 lanes[1].dynamic_threads = this->dynamic_threads_;
605 RTCORBA::ThreadpoolId threadpool_id =
606 this->rt_orb_->create_threadpool_with_lanes (this->stacksize_,
607 lanes,
608 this->allow_borrowing_,
609 this->allow_request_buffering_,
610 this->max_buffered_requests_,
611 this->max_request_buffer_size_);
613 CORBA::Policy_var threadpool_policy =
614 this->rt_orb_->create_threadpool_policy (threadpool_id);
616 CORBA::Policy_var priority_model_policy =
617 this->rt_orb_->create_priority_model_policy (priority_model,
618 default_thread_priority);
620 CORBA::PolicyList policies;
622 policies.length (policies.length () + 1);
623 policies[policies.length () - 1] =
624 threadpool_policy;
626 policies.length (policies.length () + 1);
627 policies[policies.length () - 1] =
628 priority_model_policy;
630 PortableServer::POA_var poa =
631 this->root_poa_->create_POA (poa_name,
632 this->poa_manager_.in (),
633 policies);
635 RTPortableServer::POA_var rt_poa =
636 RTPortableServer::POA::_narrow (poa.in ());
638 test_i *servant1 = 0;
639 ACE_NEW_THROW_EX (servant1,
640 test_i (this->orb_.in (),
641 poa.in (),
642 this->tests_),
643 CORBA::NO_MEMORY ());
645 servant1->in_lane (1);
647 PortableServer::ServantBase_var safe_servant1 (servant1);
649 PortableServer::ObjectId_var id1;
651 if (priority_model == RTCORBA::CLIENT_PROPAGATED)
653 servant1->invocation_pool (4);
654 servant1->client_propagated (1);
656 id1 =
657 rt_poa->activate_object (servant1);
659 else
661 test_i *servant2 = 0;
663 ACE_NEW_THROW_EX (servant2,
664 test_i (this->orb_.in (),
665 poa.in (),
666 this->tests_),
667 CORBA::NO_MEMORY ());
669 servant2->in_lane (1);
671 PortableServer::ServantBase_var safe_servant2 (servant2);
673 PortableServer::ObjectId_var id2;
675 servant1->invocation_pool_and_lane (5, 1);
676 servant2->invocation_pool_and_lane (5, 0);
677 servant1->server_declared (1);
678 servant2->server_declared (1);
679 servant1->server_priority (default_thread_priority + 1);
680 servant2->server_priority (default_thread_priority);
682 id1 =
683 rt_poa->activate_object_with_priority (servant1,
684 default_thread_priority + 1);
686 id2 =
687 rt_poa->activate_object_with_priority (servant2,
688 default_thread_priority);
690 CORBA::Object_var object2 =
691 poa->id_to_reference (id2.in ());
693 this->tests_.size (this->tests_.size () + 1);
694 this->tests_[this->tests_.size () - 1].object_ =
695 test::_narrow (object2.in ());
696 this->tests_[this->tests_.size () - 1].servant_ =
697 servant2;
700 CORBA::Object_var object1 =
701 poa->id_to_reference (id1.in ());
703 this->tests_.size (this->tests_.size () + 1);
704 this->tests_[this->tests_.size () - 1].object_ =
705 test::_narrow (object1.in ());
706 this->tests_[this->tests_.size () - 1].servant_ =
707 servant1;
710 void
711 Server::start_testing (void)
713 Tests::ITERATOR iterator (this->tests_);
714 while (!iterator.done ())
716 Test_Object_And_Servant *test = 0;
717 iterator.next (test);
719 setup_test_parameters (test,
720 this->current_.in (),
721 this->orb_.in ());
723 test->object_->start ();
725 iterator.advance ();
729 void
730 Server::test (void)
732 this->start_testing ();
734 ACE_DEBUG ((LM_DEBUG,
735 "\n\n*** Changing priority to be higher ***\n\n"));
737 this->current_->the_priority (default_thread_priority + 1);
739 this->start_testing ();
742 void
743 Server::shutdown (void)
745 this->orb_->shutdown (true);
747 this->orb_->destroy ();
750 class Task : public ACE_Task_Base
752 public:
754 Task (ACE_Thread_Manager &thread_manager,
755 CORBA::ORB_ptr orb);
757 int svc (void);
759 CORBA::ORB_var orb_;
763 Task::Task (ACE_Thread_Manager &thread_manager,
764 CORBA::ORB_ptr orb)
765 : ACE_Task_Base (&thread_manager),
766 orb_ (CORBA::ORB::_duplicate (orb))
771 Task::svc (void)
775 Server server (this->orb_.in ());
777 server.create_servant_in_root_poa ();
779 server.create_poa_and_servant_with_tp_policy ("tp_client_propagated_poa",
781 RTCORBA::CLIENT_PROPAGATED);
783 server.create_poa_and_servant_with_tp_policy ("tp_server_declared_poa",
785 RTCORBA::SERVER_DECLARED);
787 server.create_poa_and_servant_with_tp_policy ("tp_no_priority_model_poa",
789 RTCORBA::CLIENT_PROPAGATED);
791 server.create_poa_and_servant_with_tp_with_lanes_policy ("tp_with_lanes_client_propagated_poa",
792 RTCORBA::CLIENT_PROPAGATED);
794 server.create_poa_and_servant_with_tp_with_lanes_policy ("tp_with_lanes_server_declared_poa",
795 RTCORBA::SERVER_DECLARED);
797 server.test ();
799 server.shutdown ();
801 catch (const CORBA::Exception& ex)
803 ex._tao_print_exception ("Exception caught:");
804 return -1;
807 return 0;
811 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
815 CORBA::ORB_var orb =
816 CORBA::ORB_init (argc,
817 argv);
819 // Make sure we can support multiple priorities that are
820 // required for this test.
821 if (!check_supported_priorities (orb.in ()))
822 return 2;
824 // Thread Manager for managing task.
825 ACE_Thread_Manager thread_manager;
827 // Create task.
828 Task task (thread_manager,
829 orb.in ());
831 // Task activation flags.
832 long flags =
833 THR_NEW_LWP |
834 THR_JOINABLE |
835 orb->orb_core ()->orb_params ()->thread_creation_flags ();
837 // Activate task.
838 int result =
839 task.activate (flags);
840 if (result == -1)
842 if (errno == EPERM)
844 ACE_ERROR_RETURN ((LM_ERROR,
845 "Cannot create thread with scheduling policy %s\n"
846 "because the user does not have the appropriate privileges, terminating program....\n"
847 "Check svc.conf options and/or run as root\n",
848 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
851 else
852 // Unexpected error.
853 ACE_ASSERT (0);
856 // Wait for task to exit.
857 result =
858 thread_manager.wait ();
859 ACE_ASSERT (result != -1);
861 catch (const CORBA::Exception& ex)
863 ex._tao_print_exception ("Exception caught:");
864 return -1;
867 return 0;