mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-22 12:50:07 +08:00
add a test and fix broken logic.
This commit is contained in:
parent
3a4d8096cf
commit
4b2c1212e7
@ -223,7 +223,7 @@ func (mgr *dsAddrBook) dbInsert(keys []ds.Key, addrs []ma.Multiaddr, ttl time.Du
|
||||
err = ttltxn.SetTTL(key, ttl)
|
||||
case ttlExtend:
|
||||
var curr time.Time
|
||||
if curr, err = ttltxn.GetExpiration(key); err != nil && exp.After(curr) {
|
||||
if curr, err = ttltxn.GetExpiration(key); err == nil && exp.After(curr) {
|
||||
err = ttltxn.SetTTL(key, ttl)
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,20 @@ func testAddAddress(ab pstore.AddrBook) func(*testing.T) {
|
||||
|
||||
testHas(t, addrs, ab.Addrs(id))
|
||||
})
|
||||
|
||||
t.Run("adding an existing address with a later expiration extends its ttl", func(t *testing.T) {
|
||||
id := generatePeerIds(1)[0]
|
||||
addrs := generateAddrs(3)
|
||||
|
||||
ab.AddAddrs(id, addrs, time.Second)
|
||||
|
||||
// same address as before but with a higher TTL
|
||||
ab.AddAddrs(id, addrs[2:], time.Hour)
|
||||
|
||||
// after the initial TTL has expired, check that only the third address is present.
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
testHas(t, addrs[2:], ab.Addrs(id))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user