-
Notifications
You must be signed in to change notification settings - Fork 8
/
path_monitor_test.go
89 lines (68 loc) · 1.76 KB
/
path_monitor_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestGitRepoMonitor_StartMonitoring(t *testing.T) {
var gitRepoMonitor = GitRepoMonitor{
scheduledUpdateInterval: time.Minute,
}
var watcher = MockWatcher{}
var git = MockGit{}
gitRepoMonitor.StartMonitoring("some-path", &watcher, &git)
assert.Equal(t, "some-path", watcher.repoPath)
assert.Equal(t, 1, git.Count)
watcher.channel <- watcher.repoPath
time.Sleep(1 * time.Second)
assert.Equal(t, 2, git.Count)
}
func TestGitRepoMonitor_StartMonitoringAutomaticScheduleUpdate(t *testing.T) {
var gitRepoMonitor = GitRepoMonitor{
scheduledUpdateInterval: 100 * time.Millisecond,
}
var watcher = MockWatcher{}
var git = MockGit{}
gitRepoMonitor.StartMonitoring("some-path", &watcher, &git)
assert.Eventually(t, func() bool {
return git.Count >= 2
}, 1 * time.Second, 10 * time.Millisecond)
}
func TestGitRepoMonitor_ScheduleUpdate(t *testing.T) {
var gitRepoMonitor = GitRepoMonitor{
scheduledUpdateInterval: 100 * time.Millisecond,
}
var channel = make(chan string)
var path string
go func() {
path = <-channel
}()
gitRepoMonitor.scheduleUpdate("some-path", channel)
assert.Eventually(t, func() bool {
return path == "some-path"
}, 1 * time.Second, 10 * time.Millisecond)
}
type MockWatcher struct {
repoPath string
channel chan string
}
func (m *MockWatcher) Watch(path string, channel chan string) {
m.repoPath = path
m.channel = channel
}
type MockGit struct {
Count int
}
func (m *MockGit) IsDirty(path string) (bool, error) {
return false, nil
}
func (m *MockGit) Sync(path string) error {
m.Count++
return nil
}
func (m *MockGit) Update(path string) error {
return nil
}
func (m *MockGit) GetState(path string) (State, error) {
return Sync, nil
}