Skip to content

Commit ba0cbd5

Browse files
jeet1995Copilot
andcommitted
Update unit tests for background refresh fix in multi-writer accounts
- backgroundRefreshForMultiMaster: changed assertion from assertFalse to assertTrue - the timer must keep running even when shouldRefreshEndpoints returns false - backgroundRefreshDetectsTopologyChangeForMultiMaster: new test that proves the fix works by simulating a multi-write to single-write transition and verifying the background refresh detects the topology change All 6 GlobalEndPointManagerTest tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 76c4bed commit ba0cbd5

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/directconnectivity/GlobalEndPointManagerTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,57 @@ public void backgroundRefreshForMultiMaster() throws Exception {
233233
GlobalEndpointManager globalEndPointManager = new GlobalEndpointManager(databaseAccountManagerInternal, connectionPolicy, new Configs());
234234
globalEndPointManager.init();
235235

236+
// Background refresh timer must keep running even for multi-master accounts where
237+
// shouldRefreshEndpoints() returns false. This ensures topology changes (e.g.,
238+
// multi-write <-> single-write transitions) are detected.
236239
AtomicBoolean isRefreshInBackground = getRefreshInBackground(globalEndPointManager);
237-
Assert.assertFalse(isRefreshInBackground.get());
240+
Assert.assertTrue(isRefreshInBackground.get());
241+
LifeCycleUtils.closeQuietly(globalEndPointManager);
242+
}
243+
244+
/**
245+
* Validates that a multi-master account's background refresh timer detects a topology
246+
* change from multi-write to single-write. Without the fix in refreshLocationPrivateAsync,
247+
* the timer stops after init and the transition is never detected.
248+
*/
249+
@Test(groups = {"unit"}, timeOut = TIMEOUT)
250+
public void backgroundRefreshDetectsTopologyChangeForMultiMaster() throws Exception {
251+
// Start with a multi-writer account (dbAccountJson4: MW, East US + East Asia)
252+
ConnectionPolicy connectionPolicy = new ConnectionPolicy(DirectConnectionConfig.getDefaultConfig());
253+
connectionPolicy.setEndpointDiscoveryEnabled(true);
254+
connectionPolicy.setMultipleWriteRegionsEnabled(true);
255+
DatabaseAccount multiWriterAccount = new DatabaseAccount(dbAccountJson4);
256+
Mockito.when(databaseAccountManagerInternal.getDatabaseAccountFromEndpoint(ArgumentMatchers.any()))
257+
.thenReturn(Flux.just(multiWriterAccount));
258+
Mockito.when(databaseAccountManagerInternal.getServiceEndpoint())
259+
.thenReturn(new URI("https://testaccount.documents.azure.com:443"));
260+
261+
GlobalEndpointManager globalEndPointManager = new GlobalEndpointManager(
262+
databaseAccountManagerInternal, connectionPolicy, new Configs());
263+
setBackgroundRefreshLocationTimeIntervalInMS(globalEndPointManager, 500);
264+
globalEndPointManager.init();
265+
266+
// Verify multi-writer state: 2 write regions available
267+
LocationCache locationCache = this.getLocationCache(globalEndPointManager);
268+
Map<String, URI> availableWriteEndpoints = this.getAvailableWriteEndpointByLocation(locationCache);
269+
Assert.assertEquals(availableWriteEndpoints.size(), 2, "Expected 2 write regions for multi-writer account");
270+
Assert.assertTrue(availableWriteEndpoints.containsKey("East US"));
271+
Assert.assertTrue(availableWriteEndpoints.containsKey("East Asia"));
272+
273+
// Transition to single-writer account (dbAccountJson1: SW, East US only for writes)
274+
DatabaseAccount singleWriterAccount = new DatabaseAccount(dbAccountJson1);
275+
Mockito.when(databaseAccountManagerInternal.getDatabaseAccountFromEndpoint(ArgumentMatchers.any()))
276+
.thenReturn(Flux.just(singleWriterAccount));
277+
278+
// Wait for background refresh to detect the topology change
279+
Thread.sleep(2000);
280+
281+
// Verify single-writer state: write endpoints updated to reflect single-writer topology
282+
locationCache = this.getLocationCache(globalEndPointManager);
283+
availableWriteEndpoints = this.getAvailableWriteEndpointByLocation(locationCache);
284+
Assert.assertEquals(availableWriteEndpoints.size(), 1, "Expected 1 write region after transition to single-writer");
285+
Assert.assertTrue(availableWriteEndpoints.containsKey("East US"));
286+
238287
LifeCycleUtils.closeQuietly(globalEndPointManager);
239288
}
240289

0 commit comments

Comments
 (0)