duandian2725 2017-04-18 09:31
浏览 35
已采纳

如何在goroutine中测试监视文件的功能

I have a function that watches certian file via fsnotify and calls a callback when the file changes. If the callback returns false, the watching is ended:

import (
    "github.com/golang/glog"
    "github.com/fsnotify/fsnotify"
)

type WatcherFunc func(err error) bool

func WatchFileChanges(filename string, watcherFunc WatcherFunc) {
    watcher, err := fsnotify.NewWatcher()

    if err != nil {
        glog.Errorf("Got error creating watcher %s", err)
    }

    defer watcher.Close()

    done := make(chan bool)

    go func() {
        for {
            select {
            case event := <-watcher.Events:
                glog.Infof("inotify event %s", event)

                if event.Op&fsnotify.Write == fsnotify.Write {
                    glog.Infof("modified file %s, calling watcher func", event.Name)

                    if !watcherFunc(nil) {
                        close(done)
                    }
                }

            case err := <-watcher.Errors:
                glog.Errorf("Got error watching %s, calling watcher func", err)

                if !watcherFunc(err) {
                    close(done)
                }
            }
        }
    }()

    glog.Infof("Start watching file %s", filename)

    err = watcher.Add(filename)

    if err != nil {
        glog.Errorf("Got error adding watcher %s", err)
    }
    <-done
}

Then I thought it would be nice to have a test for that, so I started out with a simple test case:

import (
    "io/ioutil"
    "os"
    "testing"
)

func TestStuff(t *testing.T) {
    tmpfile, err := ioutil.TempFile("", "test")

    if err != nil {
        t.Fatal("Failed to create tmp file")
    }

    defer os.Remove(tmpfile.Name())

    watcherFunc := func (err error) bool {
        return false
    }

    WatchFileChanges(tmpfile.Name(), watcherFunc)
}

What I wanted do to here is to do a few modifications to the file, collect the events in an array, return then false from the watcherFunc and then assert on the array. The thing is, of course the test just hangs and waits for events, as the goroutine was started.

Is there any way how I can test a function like this, like … starting a different thread (?) that updates/modifies the file?

  • 写回答

1条回答 默认 最新

  • dq1685513999 2017-04-18 09:36
    关注

    Is there any way how I can test a function like this, like … starting a different thread (?) that updates/modifies the file?

    Of course... start a goroutine that does the updates you want.

    func TestStuff(t *testing.T) {
        tmpfile, err := ioutil.TempFile("", "test")
    
        if err != nil {
            t.Fatal("Failed to create tmp file")
        }
    
        defer os.Remove(tmpfile.Name())
    
        watcherFunc := func (err error) bool {
            return false
        }
        go func() {
            // Do updates here
        }()
    
        WatchFileChanges(tmpfile.Name(), watcherFunc)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目