@@ -2,14 +2,20 @@ package main
2
2
3
3
import (
4
4
"flag"
5
- "github.com/adobe-apiplatform/api-gateway-config-supervisor/sync"
6
5
"io/ioutil"
6
+ "log"
7
7
"os"
8
8
"strings"
9
+ Sync "sync"
9
10
"testing"
10
11
"time"
12
+
13
+ "github.com/adobe-apiplatform/api-gateway-config-supervisor/sync"
14
+ "github.com/koyachi/go-term-ansicolor/ansicolor"
11
15
)
12
16
17
+ var once Sync.Once
18
+
13
19
func TestMain (m * testing.M ) {
14
20
flag .Parse ()
15
21
os .Exit (m .Run ())
@@ -29,6 +35,12 @@ func setup(t *testing.T) (tempdir string) {
29
35
// setup extra debug information
30
36
debugOn := true
31
37
debug = & debugOn
38
+
39
+ once .Do (func () {
40
+ log .Println (ansicolor .IntenseBlack ("Starting the main() program" ))
41
+ go main ()
42
+ })
43
+
32
44
return tmpDir
33
45
}
34
46
@@ -52,9 +64,6 @@ func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
52
64
tmpDir := setup (t )
53
65
defer os .RemoveAll (tmpDir )
54
66
55
- // start the utility in background
56
- go main ()
57
-
58
67
status = sync .GetStatusInstance ()
59
68
if time .Since (status .LastSync ).Seconds () < 2 {
60
69
t .Fatal ("sync should not happen immediately" + time .Since (status .LastSync ).String ())
@@ -106,7 +115,6 @@ func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
106
115
107
116
//reset the reload command
108
117
reload_cmd = "echo reload-cmd not defined"
109
- reloadCmd = & reload_cmd
110
118
}
111
119
112
120
func TestThatSyncCommandExecutes (t * testing.T ) {
@@ -118,17 +126,61 @@ func TestThatSyncCommandExecutes(t *testing.T) {
118
126
syncCmd = & sync_cmd_test
119
127
120
128
// 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
+ }
122
135
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
+ }
126
138
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
128
151
time .Sleep (500 * time .Millisecond )
129
152
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" )
133
182
}
183
+
184
+ //reset the reload command
185
+ reload_cmd_test = "echo reload-cmd not defined"
134
186
}
0 commit comments