summaryrefslogtreecommitdiff
path: root/TAO/tests/Storable/test.cpp
diff options
context:
space:
mode:
authorstanleyk <stanleyk@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2013-02-05 21:11:03 +0000
committerstanleyk <stanleyk@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2013-02-05 21:11:03 +0000
commit5e030faf84086ab02059fcbcc3faed224bd57b95 (patch)
tree3a62df45ac6ccf599fb07cf6a03d672456ce2e3d /TAO/tests/Storable/test.cpp
parent9d296f7fa51116ff7040ecb2ad18612cd94b5fd1 (diff)
downloadATCD-5e030faf84086ab02059fcbcc3faed224bd57b95.tar.gz
Merge in OCI_Reliability_Enhancements branch.
Diffstat (limited to 'TAO/tests/Storable/test.cpp')
-rw-r--r--TAO/tests/Storable/test.cpp188
1 files changed, 188 insertions, 0 deletions
diff --git a/TAO/tests/Storable/test.cpp b/TAO/tests/Storable/test.cpp
new file mode 100644
index 00000000000..d9f74cb7763
--- /dev/null
+++ b/TAO/tests/Storable/test.cpp
@@ -0,0 +1,188 @@
+// $Id$
+
+// Assumed to be one of two processes that
+// writes/reads from persistent store.
+// Each process writes one group of attributes
+// and reads from the group written by the
+// other process. After reading a check is
+// made that expected values are read.
+// A sleep is done at the end of the loop
+// to account for limitation in resolution
+// of timestamp on file used to determine
+// if the file is stale.
+
+#include "Savable.h"
+
+#include "tao/Storable_FlatFileStream.h"
+
+#include "ace/Get_Opt.h"
+
+#include "ace/OS_NS_unistd.h"
+
+#include <iostream>
+
+const ACE_TCHAR *persistence_file = ACE_TEXT("test.dat");
+
+int num_loops = 1;
+int sleep_msecs = 100;
+int write_index = 0;
+bool use_backup = false;
+
+int
+parse_args (int argc, ACE_TCHAR *argv[])
+{
+ ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:s:i:b"));
+ int c;
+
+ while ((c = get_opts ()) != -1)
+ switch (c)
+ {
+ case 'n':
+ num_loops = ACE_OS::atoi (get_opts.opt_arg ());
+ break;
+
+ case 's':
+ sleep_msecs = ACE_OS::atoi (get_opts.opt_arg ());
+ break;
+
+ case 'i':
+ write_index = ACE_OS::atoi (get_opts.opt_arg ());
+ break;
+
+ case 'b':
+ use_backup = true;
+ break;
+
+ case '?':
+ default:
+ ACE_ERROR_RETURN ((LM_ERROR,
+ ACE_TEXT("usage: %s ")
+ ACE_TEXT("-n <number-of-loops> ")
+ ACE_TEXT("-s <milliseconds-to-sleep-in-loop> ")
+ ACE_TEXT("-i <index-used-for-writing> ")
+ ACE_TEXT("-b (use backup) ")
+ ACE_TEXT("\n"),
+ argv [0]),
+ -1);
+ }
+
+ if (write_index != 0 && write_index != 1)
+ {
+ ACE_ERROR_RETURN ((LM_ERROR,
+ ACE_TEXT("Error: Value passed to -i should be ")
+ ACE_TEXT("0 or 1.")),
+ -1);
+ }
+
+ // Indicates successful parsing of the command line
+ return 0;
+}
+
+int
+ACE_TMAIN(int argc, ACE_TCHAR *argv[])
+{
+ int exit_status = 0;
+
+ if (parse_args (argc, argv) != 0)
+ return 1;
+
+ int read_index = write_index ? 0 : 1;
+
+ TAO::Storable_Base::use_backup_default = use_backup;
+
+ TAO::Storable_FlatFileFactory factory ("./");
+
+ ACE_CString str_write_value = "test_string";
+ int int_write_value = -100;
+ unsigned int unsigned_int_write_value = 100;
+
+ const int bytes_size = 8;
+ char bytes_write_value[bytes_size];
+ char bytes_read[bytes_size];
+ // Set 1st byte to 32 (ASCII space) to
+ // make sure fscanf doesn't consume it
+ // when reading the size.
+ bytes_write_value[0] = 32;
+ for (int i = 1; i < bytes_size; ++i)
+ {
+ bytes_write_value[i] = i;
+ }
+
+ ACE_Time_Value sleep_time (0, 1000*sleep_msecs);
+
+ try
+ {
+ for (int j = 0; j < num_loops; ++j)
+ {
+
+ // Constructor called num_loops times.
+ // Each time state read from persistent store.
+ Savable savable(factory);
+
+ // If the file was read, verify what was
+ // written from other process is correct.
+ if (savable.is_loaded_from_stream ())
+ {
+
+ int int_read = savable.int_get(read_index);
+ // If value read is not 0 then the other
+ // process has written to persistent store.
+ // If not, it's too soon to test.
+ if (int_read != 0)
+ {
+ ACE_ASSERT (int_read == int_write_value);
+
+ const ACE_CString & str_read = savable.string_get(read_index);
+ ACE_ASSERT (str_read == str_write_value);
+
+ unsigned int unsigned_int_read =
+ savable.unsigned_int_get (read_index);
+ ACE_ASSERT (unsigned_int_read == unsigned_int_write_value);
+
+ int bytes_read_size =
+ savable.bytes_get (read_index, bytes_read);
+ ACE_ASSERT (bytes_read_size == bytes_size);
+ for (int k = 0; k < bytes_size; ++k)
+ {
+ ACE_ASSERT (bytes_read[k] == bytes_write_value[k]);
+ }
+ }
+ }
+
+ // Write out state
+ savable.string_set(write_index, str_write_value);
+ savable.int_set(write_index, int_write_value);
+ savable.unsigned_int_set(write_index, unsigned_int_write_value);
+ savable.bytes_set(write_index, bytes_size, bytes_write_value);
+ int bytes_size = savable.bytes_get (write_index, bytes_read);
+ for (int k = 0; k < bytes_size; ++k)
+ {
+ ACE_ASSERT (bytes_read[k] == bytes_write_value[k]);
+ }
+
+ ACE_OS::sleep (sleep_time);
+ }
+ }
+
+ catch (Savable_Exception &)
+ {
+ std::cout << "Savable_Exception thrown" << std::endl;
+ exit_status = 1;
+ }
+
+ catch (TAO::Storable_Read_Exception &ex)
+ {
+ std::cout << "TAO::Storable_Read_Exception thrown with state " <<
+ ex.get_state () << std::endl;
+ exit_status = 1;
+ }
+
+ catch (TAO::Storable_Write_Exception &ex)
+ {
+ std::cout << "TAO::Storable_Write_Exception thrown with state " <<
+ ex.get_state () << std::endl;
+ exit_status = 1;
+ }
+
+ return exit_status;
+}