add a test and fix broken logic.

This commit is contained in:
Raúl Kripalani 2018-09-14 18:27:23 +01:00
parent 3a4d8096cf
commit 4b2c1212e7
2 changed files with 15 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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))
})
}
}