Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Storable / test.cpp
blob0748cb9b4093c8c88d95c9549610c4bbabf47d47
1 // Assumed to be one of two processes that
2 // writes/reads from persistent store.
3 // Each process writes one group of attributes
4 // and reads from the group written by the
5 // other process. After reading a check is
6 // made that expected values are read.
7 // A sleep is done at the end of the loop
8 // to account for limitation in resolution
9 // of timestamp on file used to determine
10 // if the file is stale.
12 #include "Savable.h"
14 #include "tao/Storable_FlatFileStream.h"
15 #include "tao/SystemException.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/OS_NS_unistd.h"
20 #include <iostream>
22 const ACE_TCHAR *persistence_file = ACE_TEXT("test.dat");
24 int num_loops = 1;
25 int sleep_msecs = 100;
26 int write_index = 0;
27 bool use_backup = false;
29 int
30 parse_args (int argc, ACE_TCHAR *argv[])
32 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:s:i:b"));
33 int c;
35 while ((c = get_opts ()) != -1)
36 switch (c)
38 case 'n':
39 num_loops = ACE_OS::atoi (get_opts.opt_arg ());
40 break;
42 case 's':
43 sleep_msecs = ACE_OS::atoi (get_opts.opt_arg ());
44 break;
46 case 'i':
47 write_index = ACE_OS::atoi (get_opts.opt_arg ());
48 break;
50 case 'b':
51 use_backup = true;
52 break;
54 case '?':
55 default:
56 ACE_ERROR_RETURN ((LM_ERROR,
57 ACE_TEXT("usage: %s ")
58 ACE_TEXT("-n <number-of-loops> ")
59 ACE_TEXT("-s <milliseconds-to-sleep-in-loop> ")
60 ACE_TEXT("-i <index-used-for-writing> ")
61 ACE_TEXT("-b (use backup) ")
62 ACE_TEXT("\n"),
63 argv [0]),
64 -1);
67 if (write_index != 0 && write_index != 1)
69 ACE_ERROR_RETURN ((LM_ERROR,
70 ACE_TEXT("Error: Value passed to -i should be ")
71 ACE_TEXT("0 or 1.")),
72 -1);
75 // Indicates successful parsing of the command line
76 return 0;
79 int
80 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
82 int exit_status = 0;
84 if (parse_args (argc, argv) != 0)
85 return 1;
87 int read_index = write_index ? 0 : 1;
89 TAO::Storable_Base::use_backup_default = use_backup;
91 TAO::Storable_FlatFileFactory factory ("./");
93 ACE_CString str_write_value = "test_string";
94 int int_write_value = -100;
95 unsigned int unsigned_int_write_value = 100;
97 const int bytes_size = 8;
98 char bytes_write_value[bytes_size];
99 char bytes_read[bytes_size];
100 // Set 1st byte to 32 (ASCII space) to
101 // make sure fscanf doesn't consume it
102 // when reading the size.
103 bytes_write_value[0] = 32;
104 for (int i = 1; i < bytes_size; ++i)
106 bytes_write_value[i] = i;
109 ACE_Time_Value sleep_time (0, 1000*sleep_msecs);
113 for (int j = 0; j < num_loops; ++j)
116 // Constructor called num_loops times.
117 // Each time state read from persistent store.
118 Savable savable(factory);
120 // If the file was read, verify what was
121 // written from other process is correct.
122 if (savable.is_loaded_from_stream ())
125 int int_read = savable.int_get(read_index);
126 // If value read is not 0 then the other
127 // process has written to persistent store.
128 // If not, it's too soon to test.
129 if (int_read != 0)
131 ACE_ASSERT (int_read == int_write_value);
133 const ACE_CString & str_read = savable.string_get(read_index);
134 if (str_read != str_write_value)
135 ACE_ERROR ((LM_ERROR,
136 ACE_TEXT ("String read != num written.\n")));
138 unsigned int unsigned_int_read =
139 savable.unsigned_int_get (read_index);
140 if (unsigned_int_read != unsigned_int_write_value)
141 ACE_ERROR ((LM_ERROR,
142 ACE_TEXT ("Int read != num written.\n")));
144 int bytes_read_size =
145 savable.bytes_get (read_index, bytes_read);
146 if (bytes_read_size != bytes_size)
147 ACE_ERROR ((LM_ERROR,
148 ACE_TEXT ("Bytes read != num written.\n")));
150 for (int k = 0; k < bytes_size; ++k)
152 ACE_ASSERT (bytes_read[k] == bytes_write_value[k]);
157 // Write out state
158 savable.string_set(write_index, str_write_value);
159 savable.int_set(write_index, int_write_value);
160 savable.unsigned_int_set(write_index, unsigned_int_write_value);
161 savable.bytes_set(write_index, bytes_size, bytes_write_value);
162 int bytes_size = savable.bytes_get (write_index, bytes_read);
163 for (int k = 0; k < bytes_size; ++k)
165 ACE_ASSERT (bytes_read[k] == bytes_write_value[k]);
168 ACE_OS::sleep (sleep_time);
172 catch (Savable_Exception &)
174 std::cout << "Savable_Exception thrown" << std::endl;
175 exit_status = 1;
178 catch (TAO::Storable_Read_Exception &ex)
180 std::cout << "TAO::Storable_Read_Exception thrown with state " <<
181 ex.get_state () << std::endl;
182 exit_status = 1;
185 catch (TAO::Storable_Write_Exception &ex)
187 std::cout << "TAO::Storable_Write_Exception thrown with state " <<
188 ex.get_state () << std::endl;
189 exit_status = 1;
192 catch (const CORBA::PERSIST_STORE &)
194 std::cout << "CORBA::PERSIST_STORE thrown" << std::endl;
195 exit_status = 1;
198 return exit_status;