...
1 package etcdv3
2
3 import (
4 "context"
5 "testing"
6 "time"
7
8 "google.golang.org/grpc"
9 )
10
11 const (
12
13 irrelevantEndpoint = "http://irrelevant:12345"
14 )
15
16 func TestNewClient(t *testing.T) {
17 client, err := NewClient(
18 context.Background(),
19 []string{irrelevantEndpoint},
20 ClientOptions{
21 DialTimeout: 3 * time.Second,
22 DialKeepAlive: 3 * time.Second,
23 },
24 )
25 if err != nil {
26 t.Fatalf("unexpected error creating client: %v", err)
27 }
28 if client == nil {
29 t.Fatal("expected new Client, got nil")
30 }
31 }
32
33 func TestClientOptions(t *testing.T) {
34 client, err := NewClient(
35 context.Background(),
36 []string{},
37 ClientOptions{
38 Cert: "",
39 Key: "",
40 CACert: "",
41 DialTimeout: 3 * time.Second,
42 DialKeepAlive: 3 * time.Second,
43 },
44 )
45 if err == nil {
46 t.Errorf("expected error: %v", err)
47 }
48 if client != nil {
49 t.Fatalf("expected client to be nil on failure")
50 }
51
52 _, err = NewClient(
53 context.Background(),
54 []string{irrelevantEndpoint},
55 ClientOptions{
56 Cert: "does-not-exist.crt",
57 Key: "does-not-exist.key",
58 CACert: "does-not-exist.CACert",
59 DialTimeout: 3 * time.Second,
60 DialKeepAlive: 3 * time.Second,
61 },
62 )
63 if err == nil {
64 t.Errorf("expected error: %v", err)
65 }
66
67 client, err = NewClient(
68 context.Background(),
69 []string{irrelevantEndpoint},
70 ClientOptions{
71 DialOptions: []grpc.DialOption{grpc.WithBlock()},
72 },
73 )
74 if err == nil {
75 t.Errorf("expected connection should fail")
76 }
77 if client != nil {
78 t.Errorf("expected client to be nil on failure")
79 }
80 }
81
View as plain text