|  | 
|  | 1 | +// Copyright 2022 The go-ethereum Authors | 
|  | 2 | +// This file is part of the go-ethereum library. | 
|  | 3 | +// | 
|  | 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify | 
|  | 5 | +// it under the terms of the GNU Lesser General Public License as published by | 
|  | 6 | +// the Free Software Foundation, either version 3 of the License, or | 
|  | 7 | +// (at your option) any later version. | 
|  | 8 | +// | 
|  | 9 | +// The go-ethereum library is distributed in the hope that it will be useful, | 
|  | 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
|  | 12 | +// GNU Lesser General Public License for more details. | 
|  | 13 | +// | 
|  | 14 | +// You should have received a copy of the GNU Lesser General Public License | 
|  | 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
|  | 16 | + | 
|  | 17 | +package lru | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"encoding/binary" | 
|  | 21 | +	"fmt" | 
|  | 22 | +	"testing" | 
|  | 23 | + | 
|  | 24 | +	"github.com/ethereum/go-ethereum/common" | 
|  | 25 | +) | 
|  | 26 | + | 
|  | 27 | +func mkHash(i int) common.Hash { | 
|  | 28 | +	h := make([]byte, 32) | 
|  | 29 | +	binary.LittleEndian.PutUint64(h, uint64(i)) | 
|  | 30 | +	return common.BytesToHash(h) | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +func TestBlobLru(t *testing.T) { | 
|  | 34 | +	lru := NewSizeConstrainedLRU(100) | 
|  | 35 | +	var want uint64 | 
|  | 36 | +	// Add 11 items of 10 byte each. First item should be swapped out | 
|  | 37 | +	for i := 0; i < 11; i++ { | 
|  | 38 | +		k := mkHash(i) | 
|  | 39 | +		v := fmt.Sprintf("value-%04d", i) | 
|  | 40 | +		lru.Add(k, []byte(v)) | 
|  | 41 | +		want += uint64(len(v)) | 
|  | 42 | +		if want > 100 { | 
|  | 43 | +			want = 100 | 
|  | 44 | +		} | 
|  | 45 | +		if have := lru.size; have != want { | 
|  | 46 | +			t.Fatalf("size wrong, have %d want %d", have, want) | 
|  | 47 | +		} | 
|  | 48 | +	} | 
|  | 49 | +	// Zero:th should be evicted | 
|  | 50 | +	{ | 
|  | 51 | +		k := mkHash(0) | 
|  | 52 | +		if val := lru.Get(k); val != nil { | 
|  | 53 | +			t.Fatalf("should be evicted: %v", k) | 
|  | 54 | +		} | 
|  | 55 | +	} | 
|  | 56 | +	// Elems 1-11 should be present | 
|  | 57 | +	for i := 1; i < 11; i++ { | 
|  | 58 | +		k := mkHash(i) | 
|  | 59 | +		want := fmt.Sprintf("value-%04d", i) | 
|  | 60 | +		have := lru.Get(k) | 
|  | 61 | +		if have == nil { | 
|  | 62 | +			t.Fatalf("missing key %v", k) | 
|  | 63 | +		} | 
|  | 64 | +		if string(have) != want { | 
|  | 65 | +			t.Fatalf("wrong value, have %v want %v", have, want) | 
|  | 66 | +		} | 
|  | 67 | +	} | 
|  | 68 | +} | 
|  | 69 | + | 
|  | 70 | +// TestBlobLruOverflow tests what happens when inserting an element exceeding | 
|  | 71 | +// the max size | 
|  | 72 | +func TestBlobLruOverflow(t *testing.T) { | 
|  | 73 | +	lru := NewSizeConstrainedLRU(100) | 
|  | 74 | +	// Add 10 items of 10 byte each, filling the cache | 
|  | 75 | +	for i := 0; i < 10; i++ { | 
|  | 76 | +		k := mkHash(i) | 
|  | 77 | +		v := fmt.Sprintf("value-%04d", i) | 
|  | 78 | +		lru.Add(k, []byte(v)) | 
|  | 79 | +	} | 
|  | 80 | +	// Add one single large elem. We expect it to swap out all entries. | 
|  | 81 | +	{ | 
|  | 82 | +		k := mkHash(1337) | 
|  | 83 | +		v := make([]byte, 200) | 
|  | 84 | +		lru.Add(k, v) | 
|  | 85 | +	} | 
|  | 86 | +	// Elems 0-9 should be missing | 
|  | 87 | +	for i := 1; i < 10; i++ { | 
|  | 88 | +		k := mkHash(i) | 
|  | 89 | +		if val := lru.Get(k); val != nil { | 
|  | 90 | +			t.Fatalf("should be evicted: %v", k) | 
|  | 91 | +		} | 
|  | 92 | +	} | 
|  | 93 | +	// The size should be accurate | 
|  | 94 | +	if have, want := lru.size, uint64(200); have != want { | 
|  | 95 | +		t.Fatalf("size wrong, have %d want %d", have, want) | 
|  | 96 | +	} | 
|  | 97 | +	// Adding one small item should swap out the large one | 
|  | 98 | +	{ | 
|  | 99 | +		i := 0 | 
|  | 100 | +		k := mkHash(i) | 
|  | 101 | +		v := fmt.Sprintf("value-%04d", i) | 
|  | 102 | +		lru.Add(k, []byte(v)) | 
|  | 103 | +		if have, want := lru.size, uint64(10); have != want { | 
|  | 104 | +			t.Fatalf("size wrong, have %d want %d", have, want) | 
|  | 105 | +		} | 
|  | 106 | +	} | 
|  | 107 | +} | 
|  | 108 | + | 
|  | 109 | +// TestBlobLruSameItem tests what happens when inserting the same k/v multiple times. | 
|  | 110 | +func TestBlobLruSameItem(t *testing.T) { | 
|  | 111 | +	lru := NewSizeConstrainedLRU(100) | 
|  | 112 | +	// Add one 10 byte-item 10 times | 
|  | 113 | +	k := mkHash(0) | 
|  | 114 | +	v := fmt.Sprintf("value-%04d", 0) | 
|  | 115 | +	for i := 0; i < 10; i++ { | 
|  | 116 | +		lru.Add(k, []byte(v)) | 
|  | 117 | +	} | 
|  | 118 | +	// The size should be accurate | 
|  | 119 | +	if have, want := lru.size, uint64(10); have != want { | 
|  | 120 | +		t.Fatalf("size wrong, have %d want %d", have, want) | 
|  | 121 | +	} | 
|  | 122 | +} | 
0 commit comments