Skip to content

Commit 72eff29

Browse files
authored
test: Fix flaky MonitoreCacheTest case that needs to wait for a background task. (#2133)
1 parent 071085c commit 72eff29

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

core/src/test/java/com/google/cloud/sql/core/MonitoredCacheTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ public void testMonitoredCachePurgesClosedSockets() throws InterruptedException
7171
cache.addSocket(socket);
7272
Assert.assertEquals("1 socket in cache", 1, cache.getOpenSocketCount());
7373
socket.close();
74-
Thread.sleep(20);
74+
75+
// Wait for the MonitoredCache task to clean up the socket, up to 500ms.
76+
for (int i = 0; cache.getOpenSocketCount() > 0 && i < 10; i++) {
77+
Thread.sleep(50);
78+
}
79+
80+
// Ensure that the socket was removed from the cache.
7581
Assert.assertEquals("0 socket in cache", 0, cache.getOpenSocketCount());
7682
}
7783

0 commit comments

Comments
 (0)