-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify_test.go
68 lines (54 loc) · 1.34 KB
/
notify_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
package config
import (
"context"
"io/ioutil"
"os"
"testing"
"time"
)
func TestNotify(t *testing.T) {
var cfg struct {
Key string `toml:"key"`
}
dir, _ := ioutil.TempDir("", "")
tmp, _ := ioutil.TempFile(dir, "")
defer os.RemoveAll(dir)
if _, err := tmp.WriteString(`key = "hey"`); err != nil {
t.Fatalf("unexpected error: %v", err)
}
tmp.Close()
ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFunc()
if err := Load(tmp.Name(), &cfg); err != nil {
t.Fatalf("unexpected error %v", err)
}
if cfg.Key != "hey" {
t.Fatalf("got: %v, expected: %v", cfg.Key, "hey")
}
watch, err := Watch(ctx, tmp.Name())
if err != nil {
t.Fatalf("unexpected error while watching the configuration file: %v", err)
}
go func() {
t.Log("Will update the file after a couple of seconds...")
time.Sleep(time.Second)
t.Logf("Updating file %q", tmp.Name())
if err := ioutil.WriteFile(tmp.Name(), []byte(`key = "ho"`), 0644); err != nil {
t.Log(err)
cancelFunc()
}
}()
t.Logf("Waiting for notification...")
select {
case <-ctx.Done():
t.Fatalf("context canceled: %v", err)
case <-watch:
}
t.Logf("Got update on the file...")
if err := Load(tmp.Name(), &cfg); err != nil {
t.Fatalf("unexpected error %v", err)
}
if cfg.Key != "ho" {
t.Fatalf("got: %v, expected: %v", cfg.Key, "ho")
}
}