Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Storable / test.cpp
blob568e6aa98484b30b61d663d939a136f7c8bb73d2
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)
115 // Constructor called num_loops times.
116 // Each time state read from persistent store.
117 Savable savable(factory);
119 // If the file was read, verify what was
120 // written from other process is correct.
121 if (savable.is_loaded_from_stream ())
123 int int_read = savable.int_get(read_index);
124 // If value read is not 0 then the other
125 // process has written to persistent store.
126 // If not, it's too soon to test.
127 if (int_read != 0)
129 ACE_ASSERT (int_read == int_write_value);
131 const ACE_CString & str_read = savable.string_get(read_index);
132 if (str_read != str_write_value)
133 ACE_ERROR ((LM_ERROR,
134 ACE_TEXT ("String read != num written.\n")));
136 unsigned int unsigned_int_read =
137 savable.unsigned_int_get (read_index);
138 if (unsigned_int_read != unsigned_int_write_value)
139 ACE_ERROR ((LM_ERROR,
140 ACE_TEXT ("Int read != num written.\n")));
142 int bytes_read_size =
143 savable.bytes_get (read_index, bytes_read);
144 if (bytes_read_size != bytes_size)
145 ACE_ERROR ((LM_ERROR,
146 ACE_TEXT ("Bytes read != num written.\n")));
148 for (int k = 0; k < bytes_size; ++k)
150 ACE_ASSERT (bytes_read[k] == bytes_write_value[k]);
155 // Write out state
156 savable.string_set(write_index, str_write_value);
157 savable.int_set(write_index, int_write_value);
158 savable.unsigned_int_set(write_index, unsigned_int_write_value);
159 savable.bytes_set(write_index, bytes_size, bytes_write_value);
160 int bytes_size = savable.bytes_get (write_index, bytes_read);
161 for (int k = 0; k < bytes_size; ++k)
163 ACE_ASSERT (bytes_read[k] == bytes_write_value[k]);
166 ACE_OS::sleep (sleep_time);
170 catch (Savable_Exception &)
172 std::cout << "Savable_Exception thrown" << std::endl;
173 exit_status = 1;
176 catch (TAO::Storable_Read_Exception &ex)
178 std::cout << "TAO::Storable_Read_Exception thrown with state " <<
179 ex.get_state () << std::endl;
180 exit_status = 1;
183 catch (TAO::Storable_Write_Exception &ex)
185 std::cout << "TAO::Storable_Write_Exception thrown with state " <<
186 ex.get_state () << std::endl;
187 exit_status = 1;
190 catch (const CORBA::PERSIST_STORE &)
192 std::cout << "CORBA::PERSIST_STORE thrown" << std::endl;
193 exit_status = 1;
196 return exit_status;