Tests the scenario where there are multiple calls to New() with different names. Verifies that reference counts are tracked correctly for each client and that only when all references are released for a client, it is closed.
(t *testing.T)
| 129 | // names. Verifies that reference counts are tracked correctly for each client |
| 130 | // and that only when all references are released for a client, it is closed. |
| 131 | func (s) TestClientNew_Multiple(t *testing.T) { |
| 132 | // Create a bootstrap configuration, place it in a file in the temp |
| 133 | // directory, and set the bootstrap env vars to point to it. |
| 134 | nodeID := uuid.New().String() |
| 135 | contents := e2e.DefaultBootstrapContents(t, nodeID, "non-existent-server-address") |
| 136 | config, err := bootstrap.NewConfigFromContents(contents) |
| 137 | if err != nil { |
| 138 | t.Fatalf("Failed to parse bootstrap contents: %s, %v", contents, err) |
| 139 | } |
| 140 | pool := NewPool(config) |
| 141 | |
| 142 | // Override the client creation hook to get notified. |
| 143 | origClientImplCreateHook := xdsClientImplCreateHook |
| 144 | clientImplCreateCh := testutils.NewChannel() |
| 145 | xdsClientImplCreateHook = func(name string) { |
| 146 | clientImplCreateCh.Replace(name) |
| 147 | } |
| 148 | defer func() { xdsClientImplCreateHook = origClientImplCreateHook }() |
| 149 | |
| 150 | // Override the client close hook to get notified. |
| 151 | origClientImplCloseHook := xdsClientImplCloseHook |
| 152 | clientImplCloseCh := testutils.NewChannel() |
| 153 | xdsClientImplCloseHook = func(name string) { |
| 154 | clientImplCloseCh.Replace(name) |
| 155 | } |
| 156 | defer func() { xdsClientImplCloseHook = origClientImplCloseHook }() |
| 157 | |
| 158 | // Create two xDS clients. |
| 159 | client1Name := t.Name() + "-1" |
| 160 | _, closeFunc1, err := pool.NewClient(client1Name, &estats.UnimplementedMetricsRecorder{}) |
| 161 | if err != nil { |
| 162 | t.Fatalf("Failed to create xDS client: %v", err) |
| 163 | } |
| 164 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 165 | defer cancel() |
| 166 | name, err := clientImplCreateCh.Receive(ctx) |
| 167 | if err != nil { |
| 168 | t.Fatalf("Timeout when waiting for xDS client to be created: %v", err) |
| 169 | } |
| 170 | if name.(string) != client1Name { |
| 171 | t.Fatalf("xDS client created for name %q, want %q", name.(string), client1Name) |
| 172 | } |
| 173 | |
| 174 | client2Name := t.Name() + "-2" |
| 175 | _, closeFunc2, err := pool.NewClient(client2Name, &estats.UnimplementedMetricsRecorder{}) |
| 176 | if err != nil { |
| 177 | t.Fatalf("Failed to create xDS client: %v", err) |
| 178 | } |
| 179 | name, err = clientImplCreateCh.Receive(ctx) |
| 180 | if err != nil { |
| 181 | t.Fatalf("Timeout when waiting for xDS client to be created: %v", err) |
| 182 | } |
| 183 | if name.(string) != client2Name { |
| 184 | t.Fatalf("xDS client created for name %q, want %q", name.(string), client1Name) |
| 185 | } |
| 186 | |
| 187 | // Create N more references to each of these clients. |
| 188 | const count = 9 |
nothing calls this directly
no test coverage detected