file_watcher_unittest.cpp 1.7 KB
Newer Older
gejun's avatar
gejun committed
1
#include <gtest/gtest.h>
2 3
#include "butil/files/file_watcher.h"
#include "butil/logging.h"
gejun's avatar
gejun committed
4 5 6 7 8 9 10 11 12 13 14 15

namespace {
class FileWatcherTest : public ::testing::Test{
protected:
    FileWatcherTest(){};
    virtual ~FileWatcherTest(){};
    virtual void SetUp() {
    };
    virtual void TearDown() {
    };
};
 
16
//! gejun: check basic functions of butil::FileWatcher
gejun's avatar
gejun committed
17
TEST_F(FileWatcherTest, random_op) {
gejun's avatar
gejun committed
18 19
    srand (time(0));
    
20
    butil::FileWatcher fw;
gejun's avatar
gejun committed
21 22 23 24
    EXPECT_EQ (0, fw.init("dummy_file"));
    
    for (int i=0; i<30; ++i) {
        if (rand() % 2) {
25
            const butil::FileWatcher::Change ret = fw.check_and_consume();
gejun's avatar
gejun committed
26
            switch (ret) {
27
            case butil::FileWatcher::UPDATED:
gejun's avatar
gejun committed
28 29
                LOG(INFO) << fw.filepath() << " is updated";
                break;
30
            case butil::FileWatcher::CREATED:
gejun's avatar
gejun committed
31 32
                LOG(INFO) << fw.filepath() << " is created";
                break;
33
            case butil::FileWatcher::DELETED:
gejun's avatar
gejun committed
34 35
                LOG(INFO) << fw.filepath() << " is deleted";
                break;
36
            case butil::FileWatcher::UNCHANGED:
gejun's avatar
gejun committed
37 38 39 40 41 42 43
                LOG(INFO) << fw.filepath() << " does not change or still not exist";
                break;
            }
        }
        
        switch (rand() % 2) {
        case 0:
gejun's avatar
gejun committed
44
            ASSERT_EQ(0, system("touch dummy_file"));
gejun's avatar
gejun committed
45 46 47
            LOG(INFO) << "action: touch dummy_file";
            break;
        case 1:
gejun's avatar
gejun committed
48
            ASSERT_EQ(0, system("rm -f dummy_file"));
gejun's avatar
gejun committed
49 50 51 52 53 54 55 56 57
            LOG(INFO) << "action: rm -f dummy_file";
            break;
        case 2:
            LOG(INFO) << "action: (nothing)";
            break;
        }
        
        usleep (10000);
    }
gejun's avatar
gejun committed
58
    ASSERT_EQ(0, system("rm -f dummy_file"));
gejun's avatar
gejun committed
59
}
gejun's avatar
gejun committed
60 61

}  // namespace