...
1
18
19 package xdsclient
20
21 import (
22 "context"
23 "testing"
24
25 "github.com/google/uuid"
26 "google.golang.org/grpc/internal/testutils"
27 "google.golang.org/grpc/internal/testutils/xds/bootstrap"
28 )
29
30
31
32 func (s) TestClientNewSingleton(t *testing.T) {
33
34
35 nodeID := uuid.New().String()
36 cleanup, err := bootstrap.CreateFile(bootstrap.Options{
37 NodeID: nodeID,
38 ServerURI: "non-existent-server-address",
39 })
40 if err != nil {
41 t.Fatal(err)
42 }
43 defer cleanup()
44
45
46 origSingletonClientImplCreateHook := singletonClientImplCreateHook
47 singletonCreationCh := testutils.NewChannel()
48 singletonClientImplCreateHook = func() {
49 singletonCreationCh.Replace(nil)
50 }
51 defer func() { singletonClientImplCreateHook = origSingletonClientImplCreateHook }()
52
53
54 origSingletonClientImplCloseHook := singletonClientImplCloseHook
55 singletonCloseCh := testutils.NewChannel()
56 singletonClientImplCloseHook = func() {
57 singletonCloseCh.Replace(nil)
58 }
59 defer func() { singletonClientImplCloseHook = origSingletonClientImplCloseHook }()
60
61
62 _, closeFunc, err := New()
63 if err != nil {
64 t.Fatalf("failed to create xDS client: %v", err)
65 }
66
67 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
68 defer cancel()
69 if _, err := singletonCreationCh.Receive(ctx); err != nil {
70 t.Fatalf("Timeout when waiting for singleton xDS client to be created: %v", err)
71 }
72
73
74 const count = 9
75 closeFuncs := make([]func(), 9)
76 for i := 0; i < count; i++ {
77 func() {
78 _, closeFuncs[i], err = New()
79 if err != nil {
80 t.Fatalf("%d-th call to New() failed with error: %v", i, err)
81 }
82
83 sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
84 defer sCancel()
85 if _, err := singletonCreationCh.Receive(sCtx); err == nil {
86 t.Fatalf("%d-th call to New() created a new singleton client", i)
87 }
88 }()
89 }
90
91
92
93
94
95 for i := 0; i < count; i++ {
96 func() {
97 closeFuncs[i]()
98 closeFuncs[i]()
99
100 sCtx, sCancel := context.WithTimeout(ctx, defaultTestShortTimeout)
101 defer sCancel()
102 if _, err := singletonCloseCh.Receive(sCtx); err == nil {
103 t.Fatal("singleton client implementation closed before all references are released")
104 }
105 }()
106 }
107
108
109 closeFunc()
110 if _, err := singletonCloseCh.Receive(ctx); err != nil {
111 t.Fatalf("Timeout waiting for singleton client implementation to be closed: %v", err)
112 }
113
114
115
116 _, closeFunc, err = New()
117 if err != nil {
118 t.Fatalf("failed to create client: %v", err)
119 }
120 defer closeFunc()
121 if _, err := singletonCreationCh.Receive(ctx); err != nil {
122 t.Fatalf("Timeout when waiting for singleton xDS client to be created: %v", err)
123 }
124 }
125
View as plain text