forked from l-vitaly/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
87 lines (81 loc) · 2.06 KB
/
client_test.go
File metadata and controls
87 lines (81 loc) · 2.06 KB
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
package consul
import (
"fmt"
"os"
"testing"
"time"
"github.com/go-kit/kit/log"
)
func TestMain(t *testing.M) {
t.Run()
}
func ExampleNewClient() {
type testStruct struct {
Name string `consul:"default:name"`
Email string `consul:"default:email"`
Offset int `consul:"default:1"`
Int64 int64 `consul:"default:164"`
Uint64 uint64 `consul:"default:1644"`
Time time.Time `consul:"default:2006-01-02T15:04:05Z"`
Timeout time.Duration `consul:"default:25h"`
}
c, err := NewClient(SetLogger(log.NewJSONLogger(os.Stdout)))
if err != nil {
panic(err)
}
config := testStruct{}
if err := c.PullOrPush("some", &config); err != nil {
panic(err)
}
fmt.Println(config.Name)
fmt.Println(config.Email)
fmt.Println(config.Offset)
fmt.Println(config.Int64)
fmt.Println(config.Uint64)
fmt.Println(config.Time)
fmt.Println(config.Timeout)
// Output: name
// email
// 1
// 164
// 1644
// 2006-01-02 15:04:05 +0000 UTC
// 25h0m0s
}
func ExampleNewClient_watch() {
type testStruct struct {
Name string `consul:"default:name"`
Timeout time.Duration `consul:"default:25h"`
Int int `consul:"default:1"`
WatchableName String `consul:"default:some-name"`
WatchableTimeout Duration `consul:"default:5s"`
WatchableInt Int `consul:"default:5"`
}
c, err := NewClient(RefreshPeriod(time.Second))
if err != nil {
panic(err)
}
config := testStruct{}
if err := c.PullOrPush("some", &config); err != nil {
panic(err)
}
fmt.Println(config.Name)
fmt.Println(config.Timeout)
fmt.Println(config.Int)
fmt.Println(config.WatchableName.String())
fmt.Println(config.WatchableTimeout.Duration())
fmt.Println(config.WatchableInt.Int())
time.Sleep(time.Second * 20) // here you can change your variables in consul
fmt.Println(config.WatchableName.String())
fmt.Println(config.WatchableTimeout.Duration())
fmt.Println(config.WatchableInt.Int())
// Output: name
// 25h0m0s
// 1
// some-name
// 5s
// 5
// some-name
// 5s
// 5
}