file_watcher_unittest.cpp 1.64 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 18 19 20
TEST_F(FileWatcherTest, random_op)
{
    srand (time(0));
    
21
    butil::FileWatcher fw;
gejun's avatar
gejun committed
22 23 24 25
    EXPECT_EQ (0, fw.init("dummy_file"));
    
    for (int i=0; i<30; ++i) {
        if (rand() % 2) {
26
            const butil::FileWatcher::Change ret = fw.check_and_consume();
gejun's avatar
gejun committed
27
            switch (ret) {
28
            case butil::FileWatcher::UPDATED:
gejun's avatar
gejun committed
29 30
                LOG(INFO) << fw.filepath() << " is updated";
                break;
31
            case butil::FileWatcher::CREATED:
gejun's avatar
gejun committed
32 33
                LOG(INFO) << fw.filepath() << " is created";
                break;
34
            case butil::FileWatcher::DELETED:
gejun's avatar
gejun committed
35 36
                LOG(INFO) << fw.filepath() << " is deleted";
                break;
37
            case butil::FileWatcher::UNCHANGED:
gejun's avatar
gejun committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
                LOG(INFO) << fw.filepath() << " does not change or still not exist";
                break;
            }
        }
        
        switch (rand() % 2) {
        case 0:
            system ("touch dummy_file");
            LOG(INFO) << "action: touch dummy_file";
            break;
        case 1:
            system ("rm -f dummy_file");
            LOG(INFO) << "action: rm -f dummy_file";
            break;
        case 2:
            LOG(INFO) << "action: (nothing)";
            break;
        }
        
        usleep (10000);
    }
    system ("rm -f dummy_file");
}
}