file_watcher_unittest.cpp 1.7 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <gtest/gtest.h>
#include "base/files/file_watcher.h"
#include "base/logging.h"

namespace {
class FileWatcherTest : public ::testing::Test{
protected:
    FileWatcherTest(){};
    virtual ~FileWatcherTest(){};
    virtual void SetUp() {
    };
    virtual void TearDown() {
    };
};
 
//! gejun: check basic functions of base::FileWatcher
TEST_F(FileWatcherTest, random_op)
{
    srand (time(0));
    
    base::FileWatcher fw;
    EXPECT_EQ (0, fw.init("dummy_file"));
    
    for (int i=0; i<30; ++i) {
        if (rand() % 2) {
            LOG(INFO) << "watch: " << noflush;
            const base::FileWatcher::Change ret = fw.check_and_consume();
            switch (ret) {
            case base::FileWatcher::UPDATED:
                LOG(INFO) << fw.filepath() << " is updated";
                break;
            case base::FileWatcher::CREATED:
                LOG(INFO) << fw.filepath() << " is created";
                break;
            case base::FileWatcher::DELETED:
                LOG(INFO) << fw.filepath() << " is deleted";
                break;
            case base::FileWatcher::UNCHANGED:
                LOG(INFO) << fw.filepath() << " does not change or still not exist";
                break;
            }
            LOG(INFO);
        }
        
        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");
}
}