github.com/Cloud-Foundations/Dominator@v0.3.4/lib/objectserver/cachingreader/lru_test.go (about)

     1  package cachingreader
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/Cloud-Foundations/Dominator/lib/hash"
     8  	"github.com/Cloud-Foundations/Dominator/lib/log/testlogger"
     9  )
    10  
    11  var (
    12  	hash0 = hash.Hash{0x00, 0x00, 0xde, 0xed}
    13  	hash1 = hash.Hash{0x01, 0x00, 0xbe, 0xef}
    14  	hash2 = hash.Hash{0x02, 0x00, 0xfe, 0xed}
    15  	hash3 = hash.Hash{0x03, 0x00, 0xbe, 0xef}
    16  	hash4 = hash.Hash{0x04, 0x00, 0xac, 0xdc}
    17  
    18  	object0 = &objectType{hash: hash0}
    19  	object1 = &objectType{hash: hash1}
    20  	object2 = &objectType{hash: hash2}
    21  	object3 = &objectType{hash: hash3}
    22  	object4 = &objectType{hash: hash4}
    23  )
    24  
    25  func TestReadAndEmptyLRU(t *testing.T) {
    26  	objSrv := &ObjectServer{
    27  		logger: testlogger.New(t),
    28  		objects: map[hash.Hash]*objectType{
    29  			hash0: object0,
    30  			hash1: object1,
    31  			hash2: object2,
    32  			hash3: object3,
    33  		},
    34  	}
    35  	buffer := &bytes.Buffer{}
    36  	buffer.Write(object0.hash[:])
    37  	buffer.Write(object1.hash[:])
    38  	buffer.Write(object2.hash[:])
    39  	if err := objSrv.readLru(buffer); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	objSrv.linkOrphanedEntries()
    43  	objSrv.rwLock.Lock()
    44  	if objSrv.newest == nil {
    45  		t.Fatal("No newest entry")
    46  	}
    47  	if objSrv.oldest == nil {
    48  		t.Fatal("No oldest entry")
    49  	}
    50  	objSrv.objects[hash4] = object4
    51  	objSrv.addToLruWithLock(object4)
    52  	count := 0
    53  	for object := objSrv.oldest; object != nil; object = objSrv.oldest {
    54  		t.Logf("Removing: %p, %x", object, object.hash)
    55  		objSrv.removeFromLruWithLock(object)
    56  		if object.newer != nil {
    57  			t.Fatal("object.newer != nil")
    58  		}
    59  		if object.older != nil {
    60  			t.Fatal("object.older != nil")
    61  		}
    62  		count++
    63  	}
    64  	if count != 5 {
    65  		t.Fatalf("count: %d != 5", count)
    66  	}
    67  	if objSrv.newest != nil {
    68  		t.Fatal("Have newest entry")
    69  	}
    70  	if objSrv.oldest != nil {
    71  		t.Fatal("Have oldest entry")
    72  	}
    73  	defer func() { recover() }()
    74  	objSrv.removeFromLruWithLock(object2)
    75  	t.Fatal("Duplicate remove did not panic")
    76  }