Codebase list golang-github-go-kit-kit / 4133f7f
Fix etcdv3 client won't return error when no endpoint is available (#1009) WayJam So authored 3 years ago GitHub committed 3 years ago
3 changed file(s) with 94 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
77
88 "go.etcd.io/etcd/clientv3"
99 "go.etcd.io/etcd/pkg/transport"
10 "google.golang.org/grpc"
1011 )
1112
1213 var (
7273 CACert string
7374 DialTimeout time.Duration
7475 DialKeepAlive time.Duration
75 Username string
76 Password string
76
77 // DialOptions is a list of dial options for the gRPC client (e.g., for interceptors).
78 // For example, pass grpc.WithBlock() to block until the underlying connection is up.
79 // Without this, Dial returns immediately and connecting the server happens in background.
80 DialOptions []grpc.DialOption
81
82 Username string
83 Password string
7784 }
7885
7986 // NewClient returns Client with a connection to the named machines. It will
106113 Endpoints: machines,
107114 DialTimeout: options.DialTimeout,
108115 DialKeepAliveTime: options.DialKeepAlive,
116 DialOptions: options.DialOptions,
109117 TLS: tlscfg,
110118 Username: options.Username,
111119 Password: options.Password,
0 package etcdv3
1
2 import (
3 "context"
4 "testing"
5 "time"
6
7 "google.golang.org/grpc"
8 )
9
10 const (
11 // irrelevantEndpoint is an address which does not exists.
12 irrelevantEndpoint = "http://irrelevant:12345"
13 )
14
15 func TestNewClient(t *testing.T) {
16 client, err := NewClient(
17 context.Background(),
18 []string{irrelevantEndpoint},
19 ClientOptions{
20 DialTimeout: 3 * time.Second,
21 DialKeepAlive: 3 * time.Second,
22 },
23 )
24 if err != nil {
25 t.Fatalf("unexpected error creating client: %v", err)
26 }
27 if client == nil {
28 t.Fatal("expected new Client, got nil")
29 }
30 }
31
32 func TestClientOptions(t *testing.T) {
33 client, err := NewClient(
34 context.Background(),
35 []string{},
36 ClientOptions{
37 Cert: "",
38 Key: "",
39 CACert: "",
40 DialTimeout: 3 * time.Second,
41 DialKeepAlive: 3 * time.Second,
42 },
43 )
44 if err == nil {
45 t.Errorf("expected error: %v", err)
46 }
47 if client != nil {
48 t.Fatalf("expected client to be nil on failure")
49 }
50
51 _, err = NewClient(
52 context.Background(),
53 []string{irrelevantEndpoint},
54 ClientOptions{
55 Cert: "does-not-exist.crt",
56 Key: "does-not-exist.key",
57 CACert: "does-not-exist.CACert",
58 DialTimeout: 3 * time.Second,
59 DialKeepAlive: 3 * time.Second,
60 },
61 )
62 if err == nil {
63 t.Errorf("expected error: %v", err)
64 }
65
66 client, err = NewClient(
67 context.Background(),
68 []string{irrelevantEndpoint},
69 ClientOptions{
70 DialOptions: []grpc.DialOption{grpc.WithBlock()},
71 },
72 )
73 if err == nil {
74 t.Errorf("expected connection should fail")
75 }
76 if client != nil {
77 t.Errorf("expected client to be nil on failure")
78 }
79 }
88 "github.com/go-kit/kit/log"
99 "github.com/go-kit/kit/sd"
1010 "github.com/go-kit/kit/sd/lb"
11 "google.golang.org/grpc"
1112 )
1213
1314 func Example() {
4344
4445 // If DialKeepAlive is 0, it defaults to 3s
4546 DialKeepAlive: time.Second * 3,
47
48 // If passing `grpc.WithBlock`, dial connection will block until success.
49 DialOptions: []grpc.DialOption{grpc.WithBlock()},
4650 }
4751
4852 // Build the client.