Fix for negative TTLs by using a different lookups set depending on the value stored in the cache.
This commit is contained in:
1 parent
e66225ca9b
commit
6f545a4296
3 files changed
+53
-149
No files matched your search
+12
-64
@@ -34,7 +34,7 @@ func TestCacheLength(t *testing.T) {
|
||||
reply := makeAddressReply(questionMsg, question, ips)
|
||||
reply.Answer[0].Header().Ttl = uint32(i)
|
||||
|
||||
l.Put(questionMsg, reply, 0, insTime)
|
||||
l.Put(questionMsg, reply, 0, 0, insTime)
|
||||
}
|
||||
|
||||
wt.AssertEqualInt(t, l.Len(), cacheLen, "cache length")
|
||||
@@ -69,20 +69,20 @@ func TestCacheEntries(t *testing.T) {
|
||||
resp, err := l.Get(questionMsg, minUDPSize, time.Now())
|
||||
wt.AssertNoErr(t, err)
|
||||
if resp != nil {
|
||||
t.Logf("Got '%s'", resp)
|
||||
t.Logf("Got\n%s", resp)
|
||||
t.Fatalf("ERROR: Did not expect a reponse from Get() yet")
|
||||
}
|
||||
t.Logf("Trying to get it again")
|
||||
resp, err = l.Get(questionMsg, minUDPSize, time.Now())
|
||||
wt.AssertNoErr(t, err)
|
||||
if resp != nil {
|
||||
t.Logf("Got '%s'", resp)
|
||||
t.Logf("Got\n%s", resp)
|
||||
t.Fatalf("ERROR: Did not expect a reponse from Get() yet")
|
||||
}
|
||||
|
||||
t.Logf("Inserting the reply")
|
||||
reply1 := makeAddressReply(questionMsg, question, []net.IP{net.ParseIP("10.0.1.1")})
|
||||
l.Put(questionMsg, reply1, 0, time.Now())
|
||||
l.Put(questionMsg, reply1, nullTTL, 0, time.Now())
|
||||
|
||||
timeGet1 := time.Now()
|
||||
t.Logf("Checking we can Get() the reply now")
|
||||
@@ -93,13 +93,6 @@ func TestCacheEntries(t *testing.T) {
|
||||
wt.AssertType(t, resp.Answer[0], (*dns.A)(nil), "DNS record")
|
||||
ttlGet1 := resp.Answer[0].Header().Ttl
|
||||
|
||||
t.Logf("Checking a Wait() with timeout=0 gets the same result")
|
||||
resp, err = l.Wait(questionMsg, time.Duration(0)*time.Second, minUDPSize, time.Now())
|
||||
wt.AssertNoErr(t, err)
|
||||
wt.AssertTrue(t, resp != nil, "reponse from a Wait(timeout=0)")
|
||||
t.Logf("Received '%s'", resp.Answer[0])
|
||||
wt.AssertType(t, resp.Answer[0], (*dns.A)(nil), "DNS record")
|
||||
|
||||
timeGet2 := timeGet1.Add(time.Duration(1) * time.Second)
|
||||
t.Logf("Checking that a second Get(), after 1 second, gets the same result, but with reduced TTL")
|
||||
resp, err = l.Get(questionMsg, minUDPSize, timeGet2)
|
||||
@@ -115,13 +108,13 @@ func TestCacheEntries(t *testing.T) {
|
||||
resp, err = l.Get(questionMsg, minUDPSize, timeGet3)
|
||||
wt.AssertNoErr(t, err)
|
||||
if resp != nil {
|
||||
t.Logf("Got '%s'", resp)
|
||||
t.Logf("Got\n%s", resp)
|
||||
t.Fatalf("ERROR: Did NOT expect a reponse from the second Get()")
|
||||
}
|
||||
|
||||
t.Logf("Checking that an Remove() results in Get() returning nothing")
|
||||
replyTemp := makeAddressReply(questionMsg, question, []net.IP{net.ParseIP("10.0.9.9")})
|
||||
l.Put(questionMsg, replyTemp, 0, time.Now())
|
||||
l.Put(questionMsg, replyTemp, nullTTL, 0, time.Now())
|
||||
lenBefore := l.Len()
|
||||
l.Remove(question)
|
||||
wt.AssertEqualInt(t, l.Len(), lenBefore-1, "cache length")
|
||||
@@ -135,10 +128,10 @@ func TestCacheEntries(t *testing.T) {
|
||||
t.Logf("Inserting a two replies for the same query")
|
||||
timePut2 := time.Now()
|
||||
reply2 := makeAddressReply(questionMsg, question, []net.IP{net.ParseIP("10.0.1.2")})
|
||||
l.Put(questionMsg, reply2, 0, timePut2)
|
||||
l.Put(questionMsg, reply2, nullTTL, 0, timePut2)
|
||||
timePut3 := timePut2.Add(time.Duration(1) * time.Second)
|
||||
reply3 := makeAddressReply(questionMsg, question, []net.IP{net.ParseIP("10.0.1.3")})
|
||||
l.Put(questionMsg, reply3, 0, timePut3)
|
||||
l.Put(questionMsg, reply3, nullTTL, 0, timePut3)
|
||||
|
||||
t.Logf("Checking we get the last one...")
|
||||
resp, err = l.Get(questionMsg, minUDPSize, timePut3)
|
||||
@@ -162,7 +155,7 @@ func TestCacheEntries(t *testing.T) {
|
||||
resp, err = l.Get(questionMsg, minUDPSize, timePut3.Add(time.Duration(localTTL)*time.Second))
|
||||
wt.AssertNoErr(t, err)
|
||||
if resp != nil {
|
||||
t.Logf("Received '%s'", resp.Answer[0])
|
||||
t.Logf("Got\n%s", resp.Answer[0])
|
||||
t.Fatalf("ERROR: Did NOT expect a reponse from the Get()")
|
||||
}
|
||||
wt.AssertEqualInt(t, l.Len(), lenBefore-1, "cache length (after getting an expired entry)")
|
||||
@@ -180,13 +173,10 @@ func TestCacheEntries(t *testing.T) {
|
||||
t.Logf("Checking that an Remove() between Get() and Put() does not break things")
|
||||
replyTemp2 := makeAddressReply(questionMsg2, question2, []net.IP{net.ParseIP("10.0.9.9")})
|
||||
l.Remove(question2)
|
||||
l.Put(questionMsg2, replyTemp2, 0, time.Now())
|
||||
l.Put(questionMsg2, replyTemp2, nullTTL, 0, time.Now())
|
||||
resp, err = l.Get(questionMsg2, minUDPSize, time.Now())
|
||||
wt.AssertNoErr(t, err)
|
||||
wt.AssertNotNil(t, resp, "reponse from Get()")
|
||||
resp, err = l.Wait(questionMsg2, time.Duration(0)*time.Second, minUDPSize, time.Now())
|
||||
wt.AssertNoErr(t, err)
|
||||
wt.AssertNotNil(t, resp, "reponse from Get()")
|
||||
|
||||
questionMsg3 := new(dns.Msg)
|
||||
questionMsg3.SetQuestion("some.other.name", dns.TypeA)
|
||||
@@ -195,7 +185,7 @@ func TestCacheEntries(t *testing.T) {
|
||||
|
||||
t.Logf("Checking that a entry with CacheNoLocalReplies return an error")
|
||||
timePut3 = time.Now()
|
||||
l.Put(questionMsg3, nil, CacheNoLocalReplies, timePut3)
|
||||
l.Put(questionMsg3, nil, nullTTL, CacheNoLocalReplies, timePut3)
|
||||
resp, err = l.Get(questionMsg3, minUDPSize, timePut3)
|
||||
wt.AssertNil(t, resp, "Get() response with CacheNoLocalReplies")
|
||||
wt.AssertNotNil(t, err, "Get() error with CacheNoLocalReplies")
|
||||
@@ -208,51 +198,9 @@ func TestCacheEntries(t *testing.T) {
|
||||
|
||||
l.Remove(question3)
|
||||
t.Logf("Checking that Put&Get with CacheNoLocalReplies with a Remove in the middle returns nothing")
|
||||
l.Put(questionMsg3, nil, CacheNoLocalReplies, time.Now())
|
||||
l.Put(questionMsg3, nil, nullTTL, CacheNoLocalReplies, time.Now())
|
||||
l.Remove(question3)
|
||||
resp, err = l.Get(questionMsg3, minUDPSize, time.Now())
|
||||
wt.AssertNil(t, resp, "Get() reponse with CacheNoLocalReplies")
|
||||
wt.AssertNil(t, err, "Get() error with CacheNoLocalReplies")
|
||||
}
|
||||
|
||||
// Check that waiters are unblocked when the name they are waiting for is inserted
|
||||
func TestCacheBlockingOps(t *testing.T) {
|
||||
InitDefaultLogging(true)
|
||||
|
||||
const cacheLen = 256
|
||||
|
||||
l, err := NewCache(cacheLen)
|
||||
wt.AssertNoErr(t, err)
|
||||
|
||||
requests := []*dns.Msg{}
|
||||
|
||||
t.Logf("Starting 256 queries that will block...")
|
||||
for i := 0; i < cacheLen; i++ {
|
||||
questionName := fmt.Sprintf("name%d", i)
|
||||
questionMsg := new(dns.Msg)
|
||||
questionMsg.SetQuestion(questionName, dns.TypeA)
|
||||
questionMsg.RecursionDesired = true
|
||||
|
||||
requests = append(requests, questionMsg)
|
||||
|
||||
go func(request *dns.Msg) {
|
||||
t.Logf("Querying about %s...", request.Question[0].Name)
|
||||
_, err := l.Get(request, minUDPSize, time.Now())
|
||||
wt.AssertNoErr(t, err)
|
||||
t.Logf("Waiting for %s...", request.Question[0].Name)
|
||||
r, err := l.Wait(request, 1*time.Second, minUDPSize, time.Now())
|
||||
t.Logf("Obtained response for %s:\n%s", request.Question[0].Name, r)
|
||||
wt.AssertNoErr(t, err)
|
||||
}(questionMsg)
|
||||
}
|
||||
|
||||
// insert the IPs for those names
|
||||
for i, requestMsg := range requests {
|
||||
ip := net.ParseIP(fmt.Sprintf("10.0.1.%d", i))
|
||||
ips := []net.IP{ip}
|
||||
reply := makeAddressReply(requestMsg, &requestMsg.Question[0], ips)
|
||||
|
||||
t.Logf("Inserting response for %s...", requestMsg.Question[0].Name)
|
||||
l.Put(requestMsg, reply, 0, time.Now())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user