file_watcher_unittest.cpp 2.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

gejun's avatar
gejun committed
18
#include <gtest/gtest.h>
19 20
#include "butil/files/file_watcher.h"
#include "butil/logging.h"
gejun's avatar
gejun committed
21 22 23 24 25 26 27 28 29 30 31 32

namespace {
class FileWatcherTest : public ::testing::Test{
protected:
    FileWatcherTest(){};
    virtual ~FileWatcherTest(){};
    virtual void SetUp() {
    };
    virtual void TearDown() {
    };
};
 
33
//! gejun: check basic functions of butil::FileWatcher
gejun's avatar
gejun committed
34
TEST_F(FileWatcherTest, random_op) {
gejun's avatar
gejun committed
35 36
    srand (time(0));
    
37
    butil::FileWatcher fw;
gejun's avatar
gejun committed
38 39 40 41
    EXPECT_EQ (0, fw.init("dummy_file"));
    
    for (int i=0; i<30; ++i) {
        if (rand() % 2) {
42
            const butil::FileWatcher::Change ret = fw.check_and_consume();
gejun's avatar
gejun committed
43
            switch (ret) {
44
            case butil::FileWatcher::UPDATED:
gejun's avatar
gejun committed
45 46
                LOG(INFO) << fw.filepath() << " is updated";
                break;
47
            case butil::FileWatcher::CREATED:
gejun's avatar
gejun committed
48 49
                LOG(INFO) << fw.filepath() << " is created";
                break;
50
            case butil::FileWatcher::DELETED:
gejun's avatar
gejun committed
51 52
                LOG(INFO) << fw.filepath() << " is deleted";
                break;
53
            case butil::FileWatcher::UNCHANGED:
gejun's avatar
gejun committed
54 55 56 57 58 59 60
                LOG(INFO) << fw.filepath() << " does not change or still not exist";
                break;
            }
        }
        
        switch (rand() % 2) {
        case 0:
gejun's avatar
gejun committed
61
            ASSERT_EQ(0, system("touch dummy_file"));
gejun's avatar
gejun committed
62 63 64
            LOG(INFO) << "action: touch dummy_file";
            break;
        case 1:
gejun's avatar
gejun committed
65
            ASSERT_EQ(0, system("rm -f dummy_file"));
gejun's avatar
gejun committed
66 67 68 69 70 71 72 73 74
            LOG(INFO) << "action: rm -f dummy_file";
            break;
        case 2:
            LOG(INFO) << "action: (nothing)";
            break;
        }
        
        usleep (10000);
    }
gejun's avatar
gejun committed
75
    ASSERT_EQ(0, system("rm -f dummy_file"));
gejun's avatar
gejun committed
76
}
gejun's avatar
gejun committed
77 78

}  // namespace