mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	* pkg/cache: fix race in Add() and Evict() This fixes a race in Add() when the shard is at max capacity and the key being added is already stored. Previously, the shard would evict a random value - when all it needed to do was replace an existing value. There was a race in how Evict() picked which key to remove, which would cause concurrent calls to Evict() to remove the same key. Additionally, this commit removes a lot of the lock contention and a race around Add() and Evict() by changing them to immediately hold the write lock. Previously, they would check conditions with the read lock held and not re-check those conditions once the write lock was acquired (this is a race). * pkg/cache: code review comments * pkg/cache: simplify Add() logic
		
			
				
	
	
		
			65 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package cache
 | |
| 
 | |
| import "testing"
 | |
| 
 | |
| func TestCacheAddAndGet(t *testing.T) {
 | |
| 	const N = shardSize * 4
 | |
| 	c := New(N)
 | |
| 	c.Add(1, 1)
 | |
| 
 | |
| 	if _, found := c.Get(1); !found {
 | |
| 		t.Fatal("Failed to find inserted record")
 | |
| 	}
 | |
| 
 | |
| 	for i := 0; i < N; i++ {
 | |
| 		c.Add(uint64(i), 1)
 | |
| 	}
 | |
| 	for i := 0; i < N; i++ {
 | |
| 		c.Add(uint64(i), 1)
 | |
| 		if c.Len() != N {
 | |
| 			t.Fatal("A item was unnecessarily evicted from the cache")
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestCacheLen(t *testing.T) {
 | |
| 	c := New(4)
 | |
| 
 | |
| 	c.Add(1, 1)
 | |
| 	if l := c.Len(); l != 1 {
 | |
| 		t.Fatalf("Cache size should %d, got %d", 1, l)
 | |
| 	}
 | |
| 
 | |
| 	c.Add(1, 1)
 | |
| 	if l := c.Len(); l != 1 {
 | |
| 		t.Fatalf("Cache size should %d, got %d", 1, l)
 | |
| 	}
 | |
| 
 | |
| 	c.Add(2, 2)
 | |
| 	if l := c.Len(); l != 2 {
 | |
| 		t.Fatalf("Cache size should %d, got %d", 2, l)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestCacheSharding(t *testing.T) {
 | |
| 	c := New(shardSize)
 | |
| 	for i := 0; i < shardSize*2; i++ {
 | |
| 		c.Add(uint64(i), 1)
 | |
| 	}
 | |
| 	for i, s := range c.shards {
 | |
| 		if s.Len() == 0 {
 | |
| 			t.Errorf("Failed to populate shard: %d", i)
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func BenchmarkCache(b *testing.B) {
 | |
| 	b.ReportAllocs()
 | |
| 
 | |
| 	c := New(4)
 | |
| 	for n := 0; n < b.N; n++ {
 | |
| 		c.Add(1, 1)
 | |
| 		c.Get(1)
 | |
| 	}
 | |
| }
 |