-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc_test.go
91 lines (80 loc) · 1.49 KB
/
rc_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
90
91
package rc
import (
"context"
"fmt"
"io"
"os"
"testing"
"time"
"github.com/fsnotify/fsnotify"
)
type tt struct{}
func (tt) Unmarshall() (interface{}, error) {
b, err := os.ReadFile("./test.cfg")
if len(b) == 0 {
return nil, io.EOF
}
return string(b), err
}
func TestRc(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
rc := NewReloadableConfig(ctx, NewFsNotifyPoller("./test.cfg", fsnotify.Write), &tt{})
i := 0
for {
c, ok := <-rc.Notify
if i > 10 {
cancel()
}
if !ok {
fmt.Println("ffff")
cancel()
return
}
fmt.Println(c.(string))
i++
}
}
type TestYamlConfig struct {
A string
B struct {
Ba string
Bas []string
}
}
func TestRcYaml(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
rc := NewReloadableConfig(ctx, NewFsNotifyPoller("./test.yml", fsnotify.Write), NewYamlConfig("./test.yml", new(TestYamlConfig)))
i := 0
for {
c, ok := <-rc.Notify
if i > 10 {
cancel()
}
if !ok {
fmt.Println("ffff")
cancel()
return
}
fmt.Printf("%v \n", c.(*TestYamlConfig))
i++
}
}
func TestRcTimedYaml(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
timer := time.NewTicker(1 * time.Second)
rc := NewReloadableConfig(ctx, (*TimedPoller)(timer), NewYamlConfig("./test.yml", new(TestYamlConfig)))
i := 0
for {
c, ok := <-rc.Notify
if i > 10 {
cancel()
}
if !ok {
fmt.Println("ffff")
cancel()
return
}
fmt.Printf("%v \n", c.(*TestYamlConfig))
i++
}
}