Skip to content

Commit 6a128a6

Browse files
committed
Execute reload cmd when a new folder is created
1 parent e25d451 commit 6a128a6

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ setup:
88
99
install:
1010
@go version
11-
export GO15VENDOREXPERIMENT=1
11+
# export GO15VENDOREXPERIMENT=1
1212
# GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install -v ./...
1313
GOPATH=$(GOPATH) GOBIN=$(GOBIN) $(GOPATH)/bin/godep go install -v ./...
1414

config-supervisor_test.go

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ package main
22

33
import (
44
"flag"
5-
"github.com/adobe-apiplatform/api-gateway-config-supervisor/sync"
65
"io/ioutil"
6+
"log"
77
"os"
88
"strings"
9+
Sync "sync"
910
"testing"
1011
"time"
12+
13+
"github.com/adobe-apiplatform/api-gateway-config-supervisor/sync"
14+
"github.com/koyachi/go-term-ansicolor/ansicolor"
1115
)
1216

17+
var once Sync.Once
18+
1319
func TestMain(m *testing.M) {
1420
flag.Parse()
1521
os.Exit(m.Run())
@@ -29,6 +35,12 @@ func setup(t *testing.T) (tempdir string) {
2935
// setup extra debug information
3036
debugOn := true
3137
debug = &debugOn
38+
39+
once.Do(func() {
40+
log.Println(ansicolor.IntenseBlack("Starting the main() program"))
41+
go main()
42+
})
43+
3244
return tmpDir
3345
}
3446

@@ -52,9 +64,6 @@ func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
5264
tmpDir := setup(t)
5365
defer os.RemoveAll(tmpDir)
5466

55-
// start the utility in background
56-
go main()
57-
5867
status = sync.GetStatusInstance()
5968
if time.Since(status.LastSync).Seconds() < 2 {
6069
t.Fatal("sync should not happen immediately" + time.Since(status.LastSync).String())
@@ -106,7 +115,6 @@ func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
106115

107116
//reset the reload command
108117
reload_cmd = "echo reload-cmd not defined"
109-
reloadCmd = &reload_cmd
110118
}
111119

112120
func TestThatSyncCommandExecutes(t *testing.T) {
@@ -118,17 +126,61 @@ func TestThatSyncCommandExecutes(t *testing.T) {
118126
syncCmd = &sync_cmd_test
119127

120128
// wait for some time to init
121-
time.Sleep(500 * time.Millisecond)
129+
time.Sleep(1100 * time.Millisecond)
130+
131+
// check that the sync_cmd.txt file was created when the sync command executes
132+
if _, err := os.Stat(tmpDir + "/sync_cmd.txt"); err != nil {
133+
t.Fatal("Expected to find the file created by the sync command " + tmpDir + "/sync_cmd.txt")
134+
}
122135

123-
// main is already started by the previous test
124-
// and we can't start it here again due to: https://github.com/golang/go/issues/4674
125-
//go main()
136+
sync_cmd_test = "echo sync-cmd not defined"
137+
}
126138

127-
// wait for some time to init
139+
func TestThatReloadCommandExecutesWhenNewFolderIsAdded(t *testing.T) {
140+
tmpDir := setup(t)
141+
defer os.RemoveAll(tmpDir)
142+
143+
// in order to test that the reload command executed, we create a file and later verify that it exists on the disk
144+
reload_cmd_test := "touch " + tmpDir + "/reload_cmd.txt"
145+
reloadCmd = &reload_cmd_test
146+
147+
syncFolder = &tmpDir
148+
go watchForFSChanges()
149+
150+
// wait for some time
128151
time.Sleep(500 * time.Millisecond)
129152

130-
// check that the sync_cmd.txt file exists
131-
if _, err := os.Stat(tmpDir + "/sync_cmd.txt"); err != nil {
132-
t.Fatal("Expected to find the file created by the sync command " + tmpDir + "/sync_cmd.txt")
153+
//modifyFS: create a new directory and file
154+
dir, err := ioutil.TempDir(tmpDir, "new-folder-")
155+
if err != nil {
156+
t.Fatal(err)
157+
}
158+
f2, err := createFile(t, dir, "new-file-")
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
defer os.Remove(f2.Name()) // clean up
163+
164+
// wait for some time to track the changes
165+
time.Sleep(1000 * time.Millisecond)
166+
167+
status = sync.GetStatusInstance()
168+
// check that reload cmd has been executed
169+
if time.Since(status.LastSync).Seconds() > 1 {
170+
t.Fatal("sync should have executed earlier than 1.5 but was executed " + time.Since(status.LastSync).String())
171+
}
172+
if time.Since(status.LastReload).Seconds() > 1 {
173+
t.Fatal("reload should have executed earlier than 1.5s but was executed " + time.Since(status.LastReload).String())
174+
}
175+
if time.Since(status.LastFSChangeDetected).Seconds() > 1 {
176+
t.Fatal("FS changes should have been detected earlier than 1.5s but was detected " + time.Since(status.LastFSChangeDetected).String())
177+
}
178+
179+
// check that the reload_cmd.txt file was created when the reload command executed
180+
if _, err := os.Stat(tmpDir + "/reload_cmd.txt"); err != nil {
181+
t.Fatal("Expected to find the file created by the reload command " + tmpDir + "/reload_cmd.txt")
133182
}
183+
184+
//reset the reload command
185+
reload_cmd_test = "echo reload-cmd not defined"
134186
}

sync/watcher.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package sync
22

33
import (
4-
"github.com/koyachi/go-term-ansicolor/ansicolor"
54
"log"
5+
6+
"github.com/koyachi/go-term-ansicolor/ansicolor"
67
)
78

89
// Watches the specific folder for changes
@@ -20,10 +21,11 @@ func WatchFolderRecursive(folder string, debug bool) <-chan string {
2021
for {
2122
select {
2223
case file := <-watcher.Files:
23-
done <- file
2424
log.Println(ansicolor.IntenseBlack("FS Changed"), ansicolor.Underline(file))
25+
done <- file
2526
case folder := <-watcher.Folders:
2627
log.Println(ansicolor.Yellow("Watching path"), ansicolor.Yellow(folder))
28+
done <- folder
2729
}
2830
}
2931
}()

0 commit comments

Comments
 (0)