-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher_test.go.untested
More file actions
123 lines (104 loc) · 3.14 KB
/
Copy pathwatcher_test.go.untested
File metadata and controls
123 lines (104 loc) · 3.14 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// testing for the filesystem watcher.
package main
import (
. "gopkg.in/check.v1"
"io/ioutil"
"os"
"strings"
"sync"
"testing"
"time"
)
// suite type for the watcher fixture
type HornetWatcherSuite struct {
cxt Context
cfg Config
}
// create all the necessary directories and what have you, and start the
// mover thread.
func (s *HornetWatcherSuite) SetUpSuite(c *C) {
// waitgroup for just the mover
var wg sync.WaitGroup
// temporary input and output directories
tempInDir, tidErr := ioutil.TempDir("", "hornet_test_in_dir")
tempOutDir, todErr := ioutil.TempDir("", "hornet_test_out_dir")
if (tidErr != nil) || (todErr != nil) {
panic("creation of temporary directories failed!\n")
}
s.cfg = Config{
WatchDirPath: tempInDir,
DestDirPath: tempOutDir,
}
s.cxt = Context{
Control: make(chan ControlMessage),
NewFileStream: make(chan string, 3),
Pool: &wg,
FinishedFileStream: make(chan string, 3),
}
s.cxt.Pool.Add(1)
go Watcher(s.cxt, s.cfg)
}
// tests
var _ = Suite(&HornetWatcherSuite{})
func TestCheckWatcher(t *testing.T) { TestingT(t) }
func (s *HornetWatcherSuite) TestWatcherNotifyOnSubdir(c *C) {
// create a subdirectory in the watched base directory. create a fake
// file there that should trigger the watcher to send the filename into
// the NewFileStream channel. Check that we receive the file on the other
// end. If we don't, that's failure.
tmpSubDir, tmpErr := ioutil.TempDir(s.cfg.WatchDirPath, "watch_subdir_test")
if tmpErr != nil {
c.Logf("couldn't create subdir for testing! [%v]", tmpErr)
c.Fail()
}
time.Sleep(10 * time.Millisecond)
tmpFileName := strings.Join([]string{tmpSubDir, "watch_file.MAT"}, "/")
tmpFile, tmpFileErr := os.Create(tmpFileName)
if tmpFileErr != nil {
c.Logf("couldn't create temporary file for testing! [%v]", tmpFileErr)
c.Fail()
}
tmpFile.WriteString("hornet watcher test file")
tmpFile.Close()
time.Sleep(10 * time.Millisecond)
select {
case fname := <-s.cxt.NewFileStream:
if fname != tmpFileName {
c.Fail()
}
default:
c.Log("no message available from watcher!")
c.Fail()
}
}
// Test that .Setup files are copied
func (s *HornetWatcherSuite) TestWatcherSetupTransfer(c *C) {
// create a subdirectory in the watched base directory. create a fake
// file there that should trigger the watcher to send the filename into
// the FinishedFileStream, i.e. is a setup file. make sure it comes down
// the right pipe.
tmpSubDir, tmpErr := ioutil.TempDir(s.cfg.WatchDirPath, "watch_subdir_test")
if tmpErr != nil {
c.Logf("couldn't create subdir for testing! [%v]", tmpErr)
c.Fail()
}
time.Sleep(10 * time.Millisecond)
tmpFileName := strings.Join([]string{tmpSubDir, "setup_file.Setup"}, "/")
tmpFile, tmpFileErr := os.Create(tmpFileName)
if tmpFileErr != nil {
c.Logf("couldn't create temporary file for testing! [%v]", tmpFileErr)
c.Fail()
}
tmpFile.WriteString("hornet watcher test setup file")
tmpFile.Close()
time.Sleep(10 * time.Millisecond)
select {
case fname := <-s.cxt.FinishedFileStream:
if fname != tmpFileName {
c.Fail()
}
default:
c.Log("no message available from watcher!")
c.Fail()
}
}