log_batch_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <aws/core/utils/logging/AWSLogging.h>
17 #include <aws/core/utils/logging/ConsoleLogSystem.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
23 using namespace Aws::CloudWatchLogs;
24 using namespace Aws::FileManagement;
25 
27 public:
29  options_ = options;
30  }
31 
33  return !logs.empty() && !data_tokens.empty();
34  }
35 
37  return options_.delete_stale_data;
38  }
39 
40  DataToken read(std::string &data) override{
41  if(isDataAvailable()){
42  it++;
43  data = logs[it-1];
44  return data_tokens[it-1];
45  }
46 
47  return 0;
48  }
49 
50  void write(const std::string &data){
51  logs.push_back(data);
52  data_tokens.push_back(data_token);
53  data_token++;
54  }
55 
56  void resolve(const DataToken &token, bool is_success){
57  if(is_success){
58  for(int i = 0; i < (int)data_tokens.size(); i++){
59  if(data_tokens[i] == token){
60  data_tokens.erase(data_tokens.begin()+i);
61  logs.erase(logs.begin()+i);
62  return;
63  }
64  }
65  }
66  return;
67  }
68 
69  std::vector<std::string> logs;
70  std::vector<uint64_t> data_tokens;
71  uint64_t data_token = 1;
72  int it = 0;
74 };
75 
76 class LogBatchTest : public ::testing::Test{
77 public:
78  void SetUp() override {
79  test_strategy = std::make_shared<TestStrategy>(options);
80  file_manager = std::make_unique<LogFileManager>(test_strategy);
81  }
82 
83  void TearDown() override {
84  log_data.clear();
85  timestamps.clear();
86  }
87 
88  void createLogs(const std::vector<long> & timestamps){
89  for (auto ts : timestamps){
90  input_event.SetTimestamp(ts);
91  input_event.SetMessage("Testing readBatch");
92  log_data.push_back(input_event);
93  }
94  }
95 
96  void readLogs(){
97  while(test_strategy->isDataAvailable()){
98  test_strategy->it = 0;
99  batch = file_manager->readBatch(test_strategy->logs.size());
100  resolveBatch();
101  file_manager->deleteStaleData();
102  ASSERT_TRUE(validateBatch());
103  }
104  }
105 
106  void resolveBatch(){
107  for (auto dt : batch.data_tokens){
108  test_strategy->resolve(dt, true);
109  }
110  }
111 
112  //validate that the batch produced matches with the user's expectation
114  for(auto bd : batch.batch_data){
115  if(expectedTimeStamps.empty())
116  return false;
117 
118  long expectedTimestamp = expectedTimeStamps.front().back();
119  expectedTimeStamps.front().pop_back();
120 
121  if(expectedTimeStamps.front().empty())
122  expectedTimeStamps.erase(expectedTimeStamps.begin());
123 
124  if(bd.GetTimestamp() != expectedTimestamp){
125  return false;
126  }
127  }
128 
129  return true;
130  }
131 
132  std::shared_ptr<TestStrategy> test_strategy;
133  std::unique_ptr<LogFileManager> file_manager;
135  Aws::CloudWatchLogs::Model::InputLogEvent input_event;
136  std::vector<long> timestamps;
138 
139  //FileManagerStrategyOptions includes a new option "delete_stale_data" which we will
140  //set to true for testing
141  FileManagerStrategyOptions options{"test", "log_tests/", ".log", 1024*1024, 1024*1024, true};
142 
143  //the expecteTimeStamps will hold the log batch results we expect from completion of
144  //the readBatch function
145  std::vector<std::vector<long>> expectedTimeStamps;
146 };
147 
152 TEST_F(LogBatchTest, test_readBatch_1_batch) {
153  timestamps = {1, 3, 0, ONE_DAY_IN_MILLISEC-1, 4, 2};
154  expectedTimeStamps = {{ONE_DAY_IN_MILLISEC-1, 4, 3, 2, 1, 0}};
155  createLogs(timestamps);
156  file_manager->write(log_data);
157  readLogs();
158 }
163 TEST_F(LogBatchTest, test_readBatch_2_batches) {
164  timestamps = {ONE_DAY_IN_MILLISEC+1, 2, ONE_DAY_IN_MILLISEC+2, 1, 0, ONE_DAY_IN_MILLISEC};
165  expectedTimeStamps = {{ONE_DAY_IN_MILLISEC+2, ONE_DAY_IN_MILLISEC+1, ONE_DAY_IN_MILLISEC}, {2, 1, 0}};
166  createLogs(timestamps);
167  file_manager->write(log_data);
168  readLogs();
169 }
174 TEST_F(LogBatchTest, test_readBatch_3_batches) {
175  timestamps = {1, ONE_DAY_IN_MILLISEC+5, 4, 2, 0, ONE_DAY_IN_MILLISEC*2+10};
176  expectedTimeStamps = {{ONE_DAY_IN_MILLISEC*2+10}, {ONE_DAY_IN_MILLISEC+5},{4, 2, 1, 0}};
177  createLogs(timestamps);
178  file_manager->write(log_data);
179  readLogs();
180 }
187 TEST_F(LogBatchTest, test_2_week_delete_1_of_6) {
188  timestamps = {15, ONE_DAY_IN_MILLISEC, 0, TWO_WEEK_IN_MILLISEC+5, TWO_WEEK_IN_MILLISEC+1, 10};
189  expectedTimeStamps = {{TWO_WEEK_IN_MILLISEC+5, TWO_WEEK_IN_MILLISEC+1}, {ONE_DAY_IN_MILLISEC, 15, 10}};
190  createLogs(timestamps);
191  file_manager->write(log_data);
192  readLogs();
193 }
200 TEST_F(LogBatchTest, test_2_week_delete_4_of_6) {
201  timestamps = {1, ONE_DAY_IN_MILLISEC, 4, TWO_WEEK_IN_MILLISEC+5, 0, 3};
202  expectedTimeStamps = {{TWO_WEEK_IN_MILLISEC+5}, {ONE_DAY_IN_MILLISEC}};
203  createLogs(timestamps);
204  file_manager->write(log_data);
205  readLogs();
206 }
213 TEST_F(LogBatchTest, test_2_week_delete_5_of_6) {
214  timestamps = {1, ONE_DAY_IN_MILLISEC, 4, TWO_WEEK_IN_MILLISEC+ONE_DAY_IN_MILLISEC+5, 0, 3};
215  expectedTimeStamps = {{TWO_WEEK_IN_MILLISEC+ONE_DAY_IN_MILLISEC+5}};
216  createLogs(timestamps);
217  file_manager->write(log_data);
218  readLogs();
219 }
226 TEST_F(LogBatchTest, test_2_week_no_delete) {
227  test_strategy->options_.delete_stale_data = false;
228  timestamps = {1, ONE_DAY_IN_MILLISEC, 4, TWO_WEEK_IN_MILLISEC+5, 0, 3};
229  expectedTimeStamps = {{TWO_WEEK_IN_MILLISEC+5},{ONE_DAY_IN_MILLISEC, 4, 3, 1}, {0}};
230  createLogs(timestamps);
231  file_manager->write(log_data);
232  readLogs();
233 }
238 TEST(DeleteOptionTest, file_manager_delete_true) {
239  FileManagerStrategyOptions options{"test", "log_tests/", ".log", 1024*1024, 1024*1024, true};
240  std::shared_ptr<FileManagerStrategy> file_manager_strategy = std::make_shared<FileManagerStrategy>(options);
241  LogFileManager file_manager(file_manager_strategy);
242  ASSERT_TRUE(file_manager_strategy->isDeleteStaleData());
243 }
248 TEST(DeleteOptionTest, file_manager_delete_false) {
249  FileManagerStrategyOptions options{"test", "log_tests/", ".log", 1024*1024, 1024*1024, false};
250  std::shared_ptr<FileManagerStrategy> file_manager_strategy = std::make_shared<FileManagerStrategy>(options);
251  LogFileManager file_manager(file_manager_strategy);
252  ASSERT_FALSE(file_manager_strategy->isDeleteStaleData());
253 }
254 
255 int main(int argc, char** argv)
256 {
257  Aws::Utils::Logging::InitializeAWSLogging(
258  Aws::MakeShared<Aws::Utils::Logging::ConsoleLogSystem>(
259  "RunUnitTests", Aws::Utils::Logging::LogLevel::Trace));
260  ::testing::InitGoogleMock(&argc, argv);
261  int exitCode = RUN_ALL_TESTS();
262  Aws::Utils::Logging::ShutdownAWSLogging();
263  return exitCode;
264 }
265 
bool isDataAvailable()
std::vector< std::vector< long > > expectedTimeStamps
const long TWO_WEEK_IN_MILLISEC
Aws::CloudWatchLogs::Model::InputLogEvent input_event
void createLogs(const std::vector< long > &timestamps)
TestStrategy(const FileManagerStrategyOptions &options)
std::vector< uint64_t > data_tokens
FileObject< LogCollection > batch
FileManagerStrategyOptions options_
LogCollection log_data
TEST(DeleteOptionTest, file_manager_delete_true)
std::vector< std::string > logs
bool isDeleteStaleData()
TEST_F(LogBatchTest, test_readBatch_1_batch)
std::list< LogType > LogCollection
Definition: definitions.h:29
DataToken read(std::string &data) override
int main(int argc, char **argv)
std::shared_ptr< TestStrategy > test_strategy
void SetUp() override
void write(const std::string &data)
const long ONE_DAY_IN_MILLISEC
std::unique_ptr< LogFileManager > file_manager
uint64_t DataToken
void TearDown() override
std::vector< long > timestamps
void resolve(const DataToken &token, bool is_success)


cloudwatch_logs_common
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:24