periodic_file_deleter_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 #include <fstream>
16 #include <string>
17 #include <vector>
18 
19 #include <gtest/gtest.h>
20 
23 
24 using namespace Aws::Rosbag::Utils;
25 
26 class PeriodicFileDeleterTest : public ::testing::Test
27 {
28 public:
29  std::vector<std::string> GetFiles() {
30  get_files_count += 1;
31  return delete_files;
32  };
33 protected:
34  std::vector<std::string> files;
35  std::vector<std::string> delete_files;
36  std::unique_ptr<PeriodicFileDeleter> deleter;
38 
39  std::string CreateTempFile() {
40  char file_path[] = "/tmp/PeriodicFileDeleterTestXXXXXX";
41  int fd = mkstemp(file_path);
42  close(fd);
43  auto file = std::string(file_path);
44  // Track files so that they can be cleaned up after the test
45  files.push_back(file);
46  return file;
47  }
48 
49  void CreateDeleteFiles(int num_files) {
50  for (int i = 0; i < num_files; ++i) {
51  delete_files.push_back(CreateTempFile());
52  }
53  }
54 
55  bool FileExists(std::string file_path) {
56  std::ifstream file(file_path);
57  return file.good();
58  }
59 
60  void SetUp() override
61  {
62  get_files_count = 0;
63  deleter = std::make_unique<PeriodicFileDeleter>(boost::bind(&PeriodicFileDeleterTest::GetFiles, this));
64  }
65 
66  void TearDown() override
67  {
68  // In case a test fails, delete all files
69  for (const auto& file: files) {
70  Aws::Rosbag::Utils::DeleteFile(file.c_str());
71  }
72  }
73 };
74 
76 {
77  ASSERT_FALSE(deleter->IsActive());
78  deleter->Start();
79  ASSERT_TRUE(deleter->IsActive());
80  // Shouldn't do anything
81  deleter->Start();
82  ASSERT_TRUE(deleter->IsActive());
83  deleter->Stop();
84  ASSERT_FALSE(deleter->IsActive());
85 }
86 
87 TEST_F(PeriodicFileDeleterTest, TestDeletesFilesSucceeds)
88 {
89  int num_files = 3;
90  CreateDeleteFiles(num_files);
91 
92  // Sanity check that files were created
93  ASSERT_EQ(num_files, delete_files.size());
94  for (const auto& file: delete_files) {
95  ASSERT_TRUE(FileExists(file));
96  }
97 
98  deleter->Start();
99 
100  // Deleter won't call GetFiles again until it's completed the first batch
101  while (get_files_count < 2) {
102  sleep(1);
103  }
104  for (const auto& file: delete_files) {
105  EXPECT_FALSE(FileExists(file));
106  }
107 }
108 
109 TEST_F(PeriodicFileDeleterTest, TestIgnoresFailedFileDeletion)
110 {
111  int num_files = 3;
112  CreateDeleteFiles(num_files);
113  // Sanity check that files were created
114  ASSERT_EQ(num_files, delete_files.size());
115  for (const auto& file: delete_files) {
116  ASSERT_TRUE(FileExists(file));
117  }
118 
119  // Delete a file before the deleter can
120  DeleteFile(delete_files.at(1));
121 
122  deleter->Start();
123 
124  // Deleter won't call GetFiles again until it's completed the first batch
125  while (get_files_count < 2) {
126  sleep(1.0);
127  }
128  // All files should still be succesfully deleted
129  for (const auto& file: delete_files) {
130  EXPECT_FALSE(FileExists(file));
131  }
132 }
std::vector< std::string > GetFiles()
std::vector< std::string > files
std::vector< std::string > delete_files
Aws::Rosbag::RecorderErrorCode DeleteFile(const std::string &file_path)
delete a file
Definition: file_utils.cpp:93
std::unique_ptr< PeriodicFileDeleter > deleter
TEST_F(PeriodicFileDeleterTest, TestIsActive)
bool FileExists(std::string file_path)


rosbag_cloud_recorders
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:27