-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
63 lines (52 loc) · 1.43 KB
/
example_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
package cfg_test
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/wiggin77/cfg"
)
func sampleMap() map[string]string {
return map[string]string{
"fps": "30",
"retryDelay": "1 minute",
"logRotate": "1 day",
"ratio": "1.85",
}
}
type listener struct {
// empty
}
func (l *listener) ConfigChanged(cfg *cfg.Config, src cfg.SourceMonitored) {
fmt.Println("Config changed!")
}
func Example() {
// create a Config instance
config := &cfg.Config{}
// shutdown will stop monitoring the sources for changes
defer config.Shutdown()
// for this sample use a source backed by a simple map
m := sampleMap()
src := cfg.NewSrcMapFromMap(m)
// add the source to the end of the searched sources
config.AppendSource(src)
// add a source to the beginning of the searched sources,
// providing defaults for missing properties.
config.PrependSource(cfg.NewSrcMapFromMap(map[string]string{"maxRetries": "10"}))
// listen for changes (why not use a func type here intead of interface? Because we
// need to be able to remove listeners and cannot do that with funcs).
config.AddChangedListener(&listener{})
// change a property every 1 seconds for 5 seconds.
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
done := time.After(5 * time.Second)
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
for {
select {
case <-ticker.C:
m["fps"] = strconv.Itoa(rnd.Intn(30))
case <-done:
return
}
}
}