summaryrefslogtreecommitdiff
path: root/TAO/tests/Storable/test.cpp
blob: 9ca675a754a2dfeed7f0c654bdd29fe015e15583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// $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.

#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;
    }

  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 have written to persistent store.
              // If not, it's too soon 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]);
                    }
                }
            }

          ACE_Time_Value sleep_time (0, 1000*sleep_msecs);

          // 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);
          ACE_OS::sleep (sleep_time);
          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]);
            }
        }

    }

  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;
}