Skip to content

Commit 79d3dcf

Browse files
committed
wtclient: fix race conditions locking tests
This commit fixes two flaky test scenarios: 1. testRemoveLockedAddr: Add synchronization to wait for the dial to start before asserting that the address is locked. Previously, the test could race and check the lock state before session negotiation began. 2. testTowerSwitch: Use wait.Predicate for RemoveTower since the address may still be locked by an active session, causing intermittent failures.
1 parent 7b8c69e commit 79d3dcf

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

watchtower/wtclient/client_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,8 +1811,10 @@ var clientTests = []clientTest{
18111811
require.NoError(h.t, err)
18121812

18131813
cancel := make(chan struct{})
1814+
dialStarted := make(chan struct{})
18141815
h.net.registerConnCallback(
18151816
h.server.addr, func(peer wtserver.Peer) {
1817+
close(dialStarted)
18161818
select {
18171819
case <-h.quit:
18181820
case <-cancel:
@@ -1847,6 +1849,15 @@ var clientTests = []clientTest{
18471849
err = h.clientMgr.AddTower(towerAddr)
18481850
require.NoError(h.t, err)
18491851

1852+
// Wait for the dial to start so that we know the
1853+
// session negotiation has begun and the address is
1854+
// locked.
1855+
select {
1856+
case <-dialStarted:
1857+
case <-time.After(waitTime):
1858+
h.t.Fatal("timeout waiting for dial to start")
1859+
}
1860+
18501861
// Assert that if the client attempts to remove the
18511862
// tower's first address, then it will error due to
18521863
// address currently being locked for session
@@ -2354,10 +2365,16 @@ var clientTests = []clientTest{
23542365
}, waitTime)
23552366
require.NoError(h.t, err)
23562367

2357-
// Now remove the tower.
2358-
err = h.clientMgr.RemoveTower(
2359-
h.server.addr.IdentityKey, nil,
2360-
)
2368+
// Now remove the tower. We use wait.Predicate here
2369+
// because the address may still be locked by an active
2370+
// session.
2371+
err = wait.Predicate(func() bool {
2372+
err := h.clientMgr.RemoveTower(
2373+
h.server.addr.IdentityKey, nil,
2374+
)
2375+
2376+
return err == nil
2377+
}, waitTime)
23612378
require.NoError(h.t, err)
23622379

23632380
// Add a new tower.

0 commit comments

Comments
 (0)