proxygen
FilePollerTest.cpp File Reference
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <boost/filesystem.hpp>
#include <folly/portability/GTest.h>
#include <folly/synchronization/Baton.h>
#include <folly/File.h>
#include <folly/FileUtil.h>
#include <folly/experimental/TestUtil.h>
#include <wangle/util/FilePoller.h>
#include <folly/Singleton.h>

Go to the source code of this file.

Classes

class  FilePollerTest
 
struct  UpdateSyncState
 
class  TestFile
 
class  NoDiskPoller
 
struct  PollerWithState
 

Functions

void updateModifiedTime (const std::string &fileName, bool forward=true)
 
 TEST_F (FilePollerTest, TestUpdateFile)
 
 TEST_F (FilePollerTest, TestUpdateFileBackwards)
 
 TEST_F (FilePollerTest, TestCreateFile)
 
 TEST_F (FilePollerTest, TestDeleteFile)
 
 TEST_F (FilePollerTest, TestTwoUpdatesAndDelete)
 
 TEST_F (FilePollerTest, TestFileCreatedLate)
 
 TEST_F (FilePollerTest, TestMultiplePollers)
 
 TEST (FilePoller, TestFork)
 

Function Documentation

TEST ( FilePoller  ,
TestFork   
)

Definition at line 233 of file FilePollerTest.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::SingletonVault::destroyInstances(), folly::SingletonVault::singleton(), TestFile::update(), and PollerWithState::waitForUpdate().

233  {
234  TestFile testFile(true, 1);
235  PollerWithState p1(testFile);
236  testFile.update(true, 2);
237  ASSERT_NO_FATAL_FAILURE(p1.waitForUpdate());
238  // nuke singleton
240  testFile.update(true, 3);
241  ASSERT_NO_FATAL_FAILURE(p1.waitForUpdate());
242 }
static SingletonVault * singleton()
Definition: Singleton.h:495
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( FilePollerTest  ,
TestUpdateFile   
)

Definition at line 58 of file FilePollerTest.cpp.

References wangle::FilePoller::addFileToTrack(), ASSERT_TRUE, folly::Baton< MayBlock, Atom >::post(), folly::Baton< MayBlock, Atom >::try_wait_for(), and updateModifiedTime().

58  {
59  createFile();
60  Baton<> baton;
61  bool updated = false;
62  FilePoller poller(std::chrono::milliseconds(1));
63  poller.addFileToTrack(tmpFile, [&]() {
64  updated = true;
65  baton.post();
66  });
67  updateModifiedTime(tmpFile);
68  ASSERT_TRUE(baton.try_wait_for(std::chrono::seconds(5)));
69  ASSERT_TRUE(updated);
70 }
void updateModifiedTime(const std::string &fileName, bool forward=true)
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
void post() noexcept
Definition: Baton.h:123
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TEST_F ( FilePollerTest  ,
TestUpdateFileBackwards   
)

Definition at line 72 of file FilePollerTest.cpp.

References wangle::FilePoller::addFileToTrack(), ASSERT_TRUE, folly::Baton< MayBlock, Atom >::post(), folly::Baton< MayBlock, Atom >::try_wait_for(), and updateModifiedTime().

72  {
73  createFile();
74  Baton<> baton;
75  bool updated = false;
76  FilePoller poller(std::chrono::milliseconds(1));
77  poller.addFileToTrack(tmpFile, [&]() {
78  updated = true;
79  baton.post();
80  });
81  updateModifiedTime(tmpFile, false);
82  ASSERT_TRUE(baton.try_wait_for(std::chrono::seconds(5)));
83  ASSERT_TRUE(updated);
84 }
void updateModifiedTime(const std::string &fileName, bool forward=true)
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
void post() noexcept
Definition: Baton.h:123
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TEST_F ( FilePollerTest  ,
TestCreateFile   
)

Definition at line 86 of file FilePollerTest.cpp.

References wangle::FilePoller::addFileToTrack(), ASSERT_TRUE, folly::Baton< MayBlock, Atom >::post(), and folly::Baton< MayBlock, Atom >::try_wait_for().

86  {
87  Baton<> baton;
88  bool updated = false;
89  createFile();
90  remove(tmpFile.c_str());
91  FilePoller poller(std::chrono::milliseconds(1));
92  poller.addFileToTrack(tmpFile, [&]() {
93  updated = true;
94  baton.post();
95  });
96  File(creat(tmpFile.c_str(), O_RDONLY));
97  ASSERT_TRUE(baton.try_wait_for(std::chrono::seconds(5)));
98  ASSERT_TRUE(updated);
99 }
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
void post() noexcept
Definition: Baton.h:123
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TEST_F ( FilePollerTest  ,
TestDeleteFile   
)

Definition at line 101 of file FilePollerTest.cpp.

References wangle::FilePoller::addFileToTrack(), ASSERT_FALSE, folly::Baton< MayBlock, Atom >::post(), and folly::Baton< MayBlock, Atom >::try_wait_for().

101  {
102  Baton<> baton;
103  bool updated = false;
104  createFile();
105  FilePoller poller(std::chrono::milliseconds(1));
106  poller.addFileToTrack(tmpFile, [&]() {
107  updated = true;
108  baton.post();
109  });
110  remove(tmpFile.c_str());
111  ASSERT_FALSE(baton.try_wait_for(std::chrono::seconds(1)));
112  ASSERT_FALSE(updated);
113 }
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
void post() noexcept
Definition: Baton.h:123
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
TEST_F ( FilePollerTest  ,
TestTwoUpdatesAndDelete   
)

Definition at line 189 of file FilePollerTest.cpp.

References ASSERT_NO_FATAL_FAILURE, TestFile::update(), and PollerWithState::waitForUpdate().

189  {
190  TestFile testFile(true, 1);
191  PollerWithState poller(testFile);
192 
193  testFile.update(true, 2);
194  ASSERT_NO_FATAL_FAILURE(poller.waitForUpdate());
195 
196  testFile.update(true, 3);
197  ASSERT_NO_FATAL_FAILURE(poller.waitForUpdate());
198 
199  testFile.update(false, 0);
200  ASSERT_NO_FATAL_FAILURE(poller.waitForUpdate(false));
201 }
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( FilePollerTest  ,
TestFileCreatedLate   
)

Definition at line 203 of file FilePollerTest.cpp.

References ASSERT_NO_FATAL_FAILURE, TestFile::update(), and PollerWithState::waitForUpdate().

203  {
204  TestFile testFile(false, 0); // not created yet
205  PollerWithState poller(testFile);
206  ASSERT_NO_FATAL_FAILURE(poller.waitForUpdate(false));
207 
208  testFile.update(true, 1);
209  ASSERT_NO_FATAL_FAILURE(poller.waitForUpdate());
210 }
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( FilePollerTest  ,
TestMultiplePollers   
)

Definition at line 212 of file FilePollerTest.cpp.

References ASSERT_NO_FATAL_FAILURE, PollerWithState::poller, TestFile::update(), and PollerWithState::waitForUpdate().

212  {
213  TestFile testFile(true, 1);
214  PollerWithState p1(testFile);
215  PollerWithState p2(testFile);
216 
217  testFile.update(true, 2);
218  ASSERT_NO_FATAL_FAILURE(p1.waitForUpdate());
219  ASSERT_NO_FATAL_FAILURE(p2.waitForUpdate());
220 
221  testFile.update(true, 1);
222  ASSERT_NO_FATAL_FAILURE(p1.waitForUpdate());
223  ASSERT_NO_FATAL_FAILURE(p2.waitForUpdate());
224 
225  // clear one of the pollers and make sure the other is still
226  // getting them
227  p2.poller.reset();
228  testFile.update(true, 3);
229  ASSERT_NO_FATAL_FAILURE(p1.waitForUpdate());
230  ASSERT_NO_FATAL_FAILURE(p2.waitForUpdate(false));
231 }
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
void updateModifiedTime ( const std::string fileName,
bool  forward = true 
)

Definition at line 43 of file FilePollerTest.cpp.

References diff().

Referenced by TEST_F().

43  {
44  auto previous = std::chrono::system_clock::from_time_t(
45  boost::filesystem::last_write_time(fileName));
46  auto diff = std::chrono::seconds(10);
47  std::chrono::system_clock::time_point newTime;
48  if (forward) {
49  newTime = previous + diff;
50  } else {
51  newTime = previous - diff;
52  }
53 
54  time_t newTimet = std::chrono::system_clock::to_time_t(newTime);
55  boost::filesystem::last_write_time(fileName, newTimet);
56 }
uint64_t diff(uint64_t a, uint64_t b)
Definition: FutexTest.cpp:135