Skip to content

Commit 0372321

Browse files
Copilotjongalloway
andcommitted
Use configured endpoint identifiers instead of generic backend names
Co-authored-by: jongalloway <[email protected]>
1 parent a0019a2 commit 0372321

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/NLWebNet/Services/BackendManager.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,27 @@ public BackendManager(
3030
// based on the configuration
3131
_backendsByName = new Dictionary<string, IDataBackend>();
3232

33-
// For now, assign backends simple numeric names since we don't have
34-
// the backend factory implementation yet
33+
// Assign backends names based on configured endpoints if available,
34+
// otherwise fall back to generic names for backward compatibility
3535
var backendArray = _backends.ToArray();
36-
for (int i = 0; i < backendArray.Length; i++)
36+
var configuredEndpoints = _options.Endpoints?.Where(e => e.Value.Enabled).ToList() ?? new List<KeyValuePair<string, BackendEndpointOptions>>();
37+
38+
if (configuredEndpoints.Count > 0 && configuredEndpoints.Count == backendArray.Length)
3739
{
38-
_backendsByName[$"backend_{i}"] = backendArray[i];
40+
// Use configured endpoint identifiers
41+
for (int i = 0; i < backendArray.Length; i++)
42+
{
43+
var endpointName = configuredEndpoints[i].Key;
44+
_backendsByName[endpointName] = backendArray[i];
45+
}
46+
}
47+
else
48+
{
49+
// Fall back to generic names for backward compatibility
50+
for (int i = 0; i < backendArray.Length; i++)
51+
{
52+
_backendsByName[$"backend_{i}"] = backendArray[i];
53+
}
3954
}
4055

4156
// Set write backend - for now use the first backend if writeEndpoint is configured

tests/NLWebNet.Tests/Services/BackendManagerTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,67 @@ public async Task SearchAsync_WithParallelQueryingDisabled_StillWorks()
185185
Assert.IsNotNull(results);
186186
Assert.IsTrue(results.Any(), "Should return results even with sequential querying");
187187
}
188+
189+
[TestMethod]
190+
public void GetBackendInfo_UsesConfiguredEndpointNames_WhenEndpointsProvided()
191+
{
192+
// Arrange
193+
var optionsWithEndpoints = new MultiBackendOptions
194+
{
195+
Enabled = true,
196+
EnableParallelQuerying = true,
197+
EnableResultDeduplication = true,
198+
MaxConcurrentQueries = 5,
199+
BackendTimeoutSeconds = 30,
200+
WriteEndpoint = "primary_backend",
201+
Endpoints = new Dictionary<string, BackendEndpointOptions>
202+
{
203+
["primary_backend"] = new() { Enabled = true, BackendType = "mock", Priority = 10 },
204+
["secondary_backend"] = new() { Enabled = true, BackendType = "mock", Priority = 5 }
205+
}
206+
};
207+
208+
var backends = new[] { _backend1, _backend2 };
209+
var optionsWrapper = Options.Create(optionsWithEndpoints);
210+
var manager = new BackendManager(backends, optionsWrapper, _logger);
211+
212+
// Act
213+
var backendInfo = manager.GetBackendInfo();
214+
215+
// Assert
216+
Assert.IsNotNull(backendInfo);
217+
var infoList = backendInfo.ToList();
218+
Assert.AreEqual(2, infoList.Count, "Should return info for all backends");
219+
220+
// Verify that configured endpoint names are used instead of generic backend_0, backend_1
221+
var backendIds = infoList.Select(info => info.Id).OrderBy(id => id).ToList();
222+
CollectionAssert.AreEqual(new[] { "primary_backend", "secondary_backend" }, backendIds,
223+
"Backend IDs should use configured endpoint names");
224+
225+
// Verify write endpoint identification works with configured names
226+
var writeBackend = infoList.Single(info => info.IsWriteEndpoint);
227+
Assert.AreEqual("primary_backend", writeBackend.Id, "Primary backend should be identified as write endpoint");
228+
}
229+
230+
[TestMethod]
231+
public void GetBackendInfo_FallsBackToGenericNames_WhenNoEndpointsConfigured()
232+
{
233+
// Arrange - use original options without configured endpoints
234+
var backends = new[] { _backend1, _backend2 };
235+
var optionsWrapper = Options.Create(_options);
236+
var manager = new BackendManager(backends, optionsWrapper, _logger);
237+
238+
// Act
239+
var backendInfo = manager.GetBackendInfo();
240+
241+
// Assert
242+
Assert.IsNotNull(backendInfo);
243+
var infoList = backendInfo.ToList();
244+
Assert.AreEqual(2, infoList.Count, "Should return info for all backends");
245+
246+
// Verify that generic names are used as fallback
247+
var backendIds = infoList.Select(info => info.Id).OrderBy(id => id).ToList();
248+
CollectionAssert.AreEqual(new[] { "backend_0", "backend_1" }, backendIds,
249+
"Backend IDs should fall back to generic names when no endpoints configured");
250+
}
188251
}

0 commit comments

Comments
 (0)