Fix etcdv3 client won't return error when no endpoint is available (#1009)
WayJam So authored 3 years ago
GitHub committed 3 years ago
7 | 7 | |
8 | 8 | "go.etcd.io/etcd/clientv3" |
9 | 9 | "go.etcd.io/etcd/pkg/transport" |
10 | "google.golang.org/grpc" | |
10 | 11 | ) |
11 | 12 | |
12 | 13 | var ( |
72 | 73 | CACert string |
73 | 74 | DialTimeout time.Duration |
74 | 75 | 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 | |
77 | 84 | } |
78 | 85 | |
79 | 86 | // NewClient returns Client with a connection to the named machines. It will |
106 | 113 | Endpoints: machines, |
107 | 114 | DialTimeout: options.DialTimeout, |
108 | 115 | DialKeepAliveTime: options.DialKeepAlive, |
116 | DialOptions: options.DialOptions, | |
109 | 117 | TLS: tlscfg, |
110 | 118 | Username: options.Username, |
111 | 119 | 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 | } |
8 | 8 | "github.com/go-kit/kit/log" |
9 | 9 | "github.com/go-kit/kit/sd" |
10 | 10 | "github.com/go-kit/kit/sd/lb" |
11 | "google.golang.org/grpc" | |
11 | 12 | ) |
12 | 13 | |
13 | 14 | func Example() { |
43 | 44 | |
44 | 45 | // If DialKeepAlive is 0, it defaults to 3s |
45 | 46 | DialKeepAlive: time.Second * 3, |
47 | ||
48 | // If passing `grpc.WithBlock`, dial connection will block until success. | |
49 | DialOptions: []grpc.DialOption{grpc.WithBlock()}, | |
46 | 50 | } |
47 | 51 | |
48 | 52 | // Build the client. |