Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / ZIOP / client.cpp
blobd30d9a56fe232656981f142a0fb987fd61a8c237
1 #include "TestC.h"
2 #include "ace/Get_Opt.h"
3 #include "tao/ZIOP/ZIOP.h"
4 #include "tao/Compression/zlib/ZlibCompressor_Factory.h"
5 #include "tao/Compression/bzip2/Bzip2Compressor_Factory.h"
6 #include "TestCompressor/TestCompressor_Factory.h"
8 #include "common.h"
9 static const ACE_TCHAR *ior = ACE_TEXT("file://") DEFAULT_IOR_FILENAME;
10 static ::Compression::CompressionManager_var compression_manager = 0;
11 CORBA::ULong big_msg_size = 40000;
13 int start_tests (Test::Hello_ptr hello, CORBA::ORB_ptr orb);
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:t:s:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'k':
25 ior = get_opts.opt_arg ();
26 break;
27 case 's':
28 big_msg_size = ACE_OS::strtoul (get_opts.opt_arg (),0,10);
29 break;
30 case 't':
31 test = ACE_OS::atoi (get_opts.opt_arg ());
32 break;
34 case '?':
35 default:
36 ACE_ERROR_RETURN ((LM_ERROR,
37 "usage: %s "
38 "-k <ior> "
39 "\n",
40 argv [0]),
41 -1);
43 // Indicates successful parsing of the command line
44 return 0;
47 int
48 register_factories (CORBA::ORB_ptr orb)
50 CORBA::Object_var compression_manager_obj =
51 orb->resolve_initial_references("CompressionManager");
53 compression_manager = ::Compression::CompressionManager::_narrow (
54 compression_manager_obj.in ());
56 if (CORBA::is_nil(compression_manager.in ()))
57 ACE_ERROR_RETURN ((LM_ERROR,
58 " (%P|%t) Panic: nil compression manager\n"),
59 1);
60 ::Compression::CompressorFactory_ptr compressor_factory;
61 ::Compression::CompressorFactory_var compr_fact;
63 //register Zlib compressor
64 ACE_NEW_RETURN (compressor_factory, TAO::Zlib_CompressorFactory (), 1);
65 compr_fact = compressor_factory;
66 compression_manager->register_factory(compr_fact.in ());
68 // register bzip2 compressor
69 ACE_NEW_RETURN (compressor_factory, TAO::Bzip2_CompressorFactory (), 1);
70 compr_fact = compressor_factory;
71 compression_manager->register_factory(compr_fact.in ());
73 // register test compressor
74 ACE_NEW_RETURN (compressor_factory, TAO::Test_CompressorFactory (), 1);
75 compr_fact = compressor_factory;
76 compression_manager->register_factory(compr_fact.in ());
77 return 0;
80 CORBA::Policy_ptr
81 create_compressor_id_level_list_policy (CORBA::ORB_ptr orb, bool add_zlib_for_test_1)
83 ::Compression::CompressorIdLevelList compressor_id_list;
85 switch (test)
87 case 1:
88 if (add_zlib_for_test_1)
89 compressor_id_list.length(2);
90 else
91 compressor_id_list.length(1);
92 compressor_id_list[0].compressor_id = ::Compression::COMPRESSORID_LZO;
93 compressor_id_list[0].compression_level = CLIENT_COMPRESSION_LEVEL;
94 if (add_zlib_for_test_1)
96 compressor_id_list[1].compressor_id = ::Compression::COMPRESSORID_ZLIB;
97 compressor_id_list[1].compression_level = CLIENT_COMPRESSION_LEVEL;
99 break;
100 case 2:
101 compressor_id_list.length(1);
102 compressor_id_list[0].compressor_id = COMPRESSORID_FOR_TESTING;
103 compressor_id_list[0].compression_level = CLIENT_COMPRESSION_LEVEL;
104 break;
105 case 3:
106 case 4:
107 default:
108 compressor_id_list.length(2);
109 compressor_id_list[0].compressor_id = ::Compression::COMPRESSORID_ZLIB;
110 compressor_id_list[0].compression_level = CLIENT_COMPRESSION_LEVEL;
111 compressor_id_list[1].compressor_id = ::Compression::COMPRESSORID_BZIP2;
112 compressor_id_list[1].compression_level = CLIENT_COMPRESSION_LEVEL;
113 break;
115 CORBA::Any compressor_id_any;
116 compressor_id_any <<= compressor_id_list;
118 return orb->create_policy (ZIOP::COMPRESSOR_ID_LEVEL_LIST_POLICY_ID, compressor_id_any);
121 CORBA::Policy_ptr
122 create_low_value_policy (CORBA::ORB_ptr orb)
124 // Setting policy for minimum amount of bytes that needs to be
125 // compressed. If a message is smaller than this, it doesn't get
126 // compressed.
127 // make sure everything gets compressed.
128 CORBA::ULong compression_low_value = 10;
129 if (test == 3)
130 compression_low_value = 5000000;
132 CORBA::Any low_value_any;
133 low_value_any <<= compression_low_value;
135 return orb->create_policy (ZIOP::COMPRESSION_LOW_VALUE_POLICY_ID, low_value_any);
138 CORBA::Policy_ptr
139 create_compression_enabled_policy (CORBA::ORB_ptr orb)
141 // Setting policy whether compression is used.
142 CORBA::Boolean compression_enabling = true;
143 CORBA::Any compression_enabling_any;
144 compression_enabling_any <<= CORBA::Any::from_boolean(compression_enabling);
146 return orb->create_policy (ZIOP::COMPRESSION_ENABLING_POLICY_ID, compression_enabling_any);
149 CORBA::Policy_ptr
150 create_min_ratio_policy (CORBA::ORB_ptr orb)
152 CORBA::Any min_compression_ratio_any;
153 Compression::CompressionRatio min_compression_ratio = 0.50;
154 min_compression_ratio_any <<= min_compression_ratio;
156 return orb->create_policy (ZIOP::COMPRESSION_MIN_RATIO_POLICY_ID, min_compression_ratio_any);
159 Test::Hello_var
160 create_policies (CORBA::ORB_ptr orb, bool add_zlib_compressor)
162 CORBA::PolicyList policies(4);
163 policies.length(4);
165 policies[0] = create_compressor_id_level_list_policy (orb, add_zlib_compressor);
166 policies[1] = create_low_value_policy (orb);
167 policies[2] = create_compression_enabled_policy (orb);
168 policies[3] = create_min_ratio_policy (orb);
170 CORBA::Object_var tmp = orb->string_to_object(ior);
171 CORBA::Object_var tmp2 = tmp->_set_policy_overrides (policies, CORBA::ADD_OVERRIDE);
172 Test::Hello_var hello = Test::Hello::_narrow(tmp2.in ());
173 return hello._retn ();
176 Test::Hello_var
177 prepare_tests (CORBA::ORB_ptr orb, bool create_factories=true)
179 #if defined TAO_HAS_ZIOP && TAO_HAS_ZIOP == 1
180 if (create_factories)
182 register_factories(orb);
185 return create_policies (orb, !create_factories);
186 #else
187 ACE_UNUSED_ARG (create_factories);
188 CORBA::Object_var tmp = orb->string_to_object(ior);
189 Test::Hello_var hello = Test::Hello::_narrow(tmp.in ());
190 return hello._retn ();
191 #endif
195 check_results (CORBA::ORB_ptr orb)
197 #if defined TAO_HAS_ZIOP && TAO_HAS_ZIOP == 1
198 switch (test)
200 case 1:
203 // should throw an exception
204 ::Compression::Compressor_var compressor (
205 compression_manager->get_compressor (
206 ::Compression::COMPRESSORID_LZO,
207 LEAST_COMPRESSION_LEVEL ));
208 ACE_ERROR_RETURN ((LM_ERROR,
209 ACE_TEXT ("ERROR : check_results, ")
210 ACE_TEXT ("no exception thrown when applying for ")
211 ACE_TEXT ("LZO Compressor\n")),
214 catch (const ::Compression::UnknownCompressorId&)
216 ACE_DEBUG ((LM_DEBUG,
217 ACE_TEXT ("check_results, expected exception caught, ")
218 ACE_TEXT ("(unknown factory)\n")));
219 Test::Hello_var hello = prepare_tests (orb, false);
220 test = -1;
221 return start_tests (hello.in (), orb);
223 break;
224 case 4:
225 case -1:
227 ::Compression::Compressor_var compressor (
228 compression_manager->get_compressor (
229 ::Compression::COMPRESSORID_ZLIB,
230 LEAST_COMPRESSION_LEVEL ));
231 if (!CORBA::is_nil (compressor))
233 if (compressor->compressed_bytes () == 0)
234 ACE_ERROR_RETURN ((LM_ERROR,
235 ACE_TEXT ("ERROR : check_results, no compression used ")
236 ACE_TEXT ("during test 1a\n")),
238 else
239 return 0;
241 else
243 ACE_ERROR_RETURN ((LM_ERROR,
244 ACE_TEXT ("ERROR : check_results, zlib compressor not found ")
245 ACE_TEXT ("during test 1a\n")),
249 break;
250 case 2:
251 return 0;
252 break;
253 case 3:
255 // low value policy test. No compression should be used.
256 ::Compression::Compressor_var compressor (
257 compression_manager->get_compressor (
258 ::Compression::COMPRESSORID_ZLIB,
259 LEAST_COMPRESSION_LEVEL ));
260 if (!CORBA::is_nil (compressor))
262 if (compressor->compressed_bytes () != 0)
263 ACE_ERROR_RETURN ((LM_ERROR,
264 ACE_TEXT ("ERROR : check_results, compression used ")
265 ACE_TEXT ("during test %d\n"), test),
267 else
268 return 0;
270 else
272 ACE_ERROR_RETURN ((LM_ERROR,
273 ACE_TEXT ("ERROR : check_results, zlib compressor not found ")
274 ACE_TEXT ("during test %d\n"), test),
278 break;
279 default:
280 ACE_ERROR_RETURN ((LM_ERROR,
281 ACE_TEXT ("ERROR : check_results, unknown test ID\n")),
284 ACE_ERROR_RETURN ((LM_ERROR,
285 ACE_TEXT ("ERROR : check_results, unexpected\n")),
287 #else
288 ACE_UNUSED_ARG (orb);
289 return 0;
290 #endif
294 run_string_test (Test::Hello_ptr hello)
296 ACE_DEBUG((LM_DEBUG,
297 ACE_TEXT("run_string_test, start\n")));
299 if (test == 2)
301 ACE_DEBUG((LM_DEBUG,
302 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
305 CORBA::String_var the_string = hello->get_string ("This is a test string"
306 "This is a test string"
307 "This is a test string"
308 "This is a test string"
309 "This is a test string"
310 "This is a test string"
311 "This is a test string"
312 "This is a test string"
313 "This is a test string");
314 ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>\n",
315 the_string.in ()));
316 if (ACE_OS::strstr (the_string.in (), "Hello there") == 0)
318 ACE_ERROR_RETURN ((LM_ERROR,
319 ACE_TEXT ("ERROR : run_string_test, unexpected string received\n")),
323 return 0;
327 run_big_reply_test (Test::Hello_ptr hello)
329 ACE_DEBUG((LM_DEBUG,
330 ACE_TEXT("start get_big_reply\n")));
332 if (test == 2)
334 ACE_DEBUG((LM_DEBUG,
335 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
338 //Prepare to send a large number of bytes. Should be compressed
339 Test::Octet_Seq_var dummy = hello->get_big_reply (big_msg_size);
340 if (dummy->length () > 0)
342 ACE_DEBUG ((LM_DEBUG,
343 ACE_TEXT("get_big_reply, received = %d bytes\n"),
344 dummy->length ()));
346 else
348 ACE_ERROR_RETURN ((LM_ERROR,
349 ACE_TEXT ("ERROR : get_big_reply, ")
350 ACE_TEXT ("error receiving client side blob\n")),
353 return 0;
357 run_big_request_test (Test::Hello_ptr hello)
359 Test::Octet_Seq send_msg(big_msg_size);
360 send_msg.length (big_msg_size);
362 for (CORBA::ULong i = 0; i < big_msg_size; ++i)
364 send_msg[i]= static_cast<CORBA::Octet> (i & 0xff);
367 ACE_DEBUG((LM_DEBUG,
368 ACE_TEXT("run_big_request_test, send = %d bytes\n"), big_msg_size));
370 if (test == 2)
372 ACE_DEBUG((LM_DEBUG,
373 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
376 hello->big_request(send_msg);
377 return 0;
381 start_tests (Test::Hello_ptr hello, CORBA::ORB_ptr orb)
383 int result = 0;
384 if (test != 4)
386 result += run_string_test (hello);
387 result += run_big_request_test (hello);
389 result += run_big_reply_test (hello);
391 result += check_results (orb);
392 return result;
396 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
398 int result = 0;
401 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
403 if (parse_args (argc, argv) != 0)
404 return 1;
406 Test::Hello_var hello = prepare_tests (orb.in ());
408 if (CORBA::is_nil (hello.in ()))
410 ACE_ERROR_RETURN ((LM_DEBUG,
411 "ERROR : Nil Test::Hello reference <%C>\n",
412 ior),
418 result += start_tests(hello.in (), orb.in ());
420 catch (const CORBA::Exception& ex)
422 ex._tao_print_exception ("Exception caught:");
423 ++result;
426 if (test == 2)
428 ACE_DEBUG((LM_DEBUG,
429 ACE_TEXT("*** NOTE TestCompressor is EXPECTED to throw IDL:omg.org/Compression/CompressionException ***\n")));
432 hello->shutdown ();
434 orb->destroy ();
436 catch (const CORBA::Exception& ex)
438 ex._tao_print_exception ("Exception caught:");
439 ++result;
442 return result;