diff options
author | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-03-03 17:24:17 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-03-03 17:24:17 +0530 |
commit | 7f9a513ea1803af327067b7bd84326c63b3d3844 (patch) | |
tree | d0fd50db0aa8d4d59a79c22fc9eb9b84e305a261 /store_test.go | |
parent | Improved Concurency (diff) | |
download | cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.gz cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.bz2 cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.lz cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.xz cache-7f9a513ea1803af327067b7bd84326c63b3d3844.tar.zst cache-7f9a513ea1803af327067b7bd84326c63b3d3844.zip |
Added documentation and tests
Diffstat (limited to '')
-rw-r--r-- | store_test.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/store_test.go b/store_test.go index f6cccc9..4b9d2f3 100644 --- a/store_test.go +++ b/store_test.go @@ -18,6 +18,107 @@ func setupTestStore(tb testing.TB) *store { return store } +func TestNodeIsValid(t *testing.T) { + tests := []struct { + name string + node *node + isValid bool + }{ + { + name: "Valid node with non-zero expiration", + node: &node{Key: []byte("key1"), Value: []byte("value1"), Expiration: time.Now().Add(10 * time.Minute)}, + isValid: true, + }, + { + name: "Valid node with zero expiration", + node: &node{Key: []byte("key2"), Value: []byte("value2"), Expiration: time.Time{}}, + isValid: true, + }, + { + name: "Expired node", + node: &node{Key: []byte("key3"), Value: []byte("value3"), Expiration: time.Now().Add(-1 * time.Minute)}, + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.node.IsValid(); got != tt.isValid { + t.Errorf("IsValid() = %v, want %v", got, tt.isValid) + } + }) + } +} + +func TestNodeTTL(t *testing.T) { + tests := []struct { + name string + node *node + ttl time.Duration + }{ + { + name: "Node with non-zero expiration", + node: &node{Key: []byte("key1"), Value: []byte("value1"), Expiration: time.Now().Add(10 * time.Minute)}, + ttl: 10 * time.Minute, + }, + { + name: "Node with zero expiration", + node: &node{Key: []byte("key2"), Value: []byte("value2"), Expiration: time.Time{}}, + ttl: 0, + }, + { + name: "Expired node", + node: &node{Key: []byte("key3"), Value: []byte("value3"), Expiration: time.Now().Add(-1 * time.Minute)}, + ttl: -1 * time.Minute, // Should be negative or 0, depending on the implementation + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.node.TTL().Round(time.Second); got != tt.ttl { + t.Errorf("TTL() = %v, want %v", got, tt.ttl) + } + }) + } +} + +func TestNodeCost(t *testing.T) { + tests := []struct { + name string + node *node + cost uint64 + }{ + { + name: "Node with non-empty Key and Value", + node: &node{Key: []byte("key1"), Value: []byte("value1")}, + cost: 10, + }, + { + name: "Node with empty Key and Value", + node: &node{Key: []byte(""), Value: []byte("")}, + cost: 0, + }, + { + name: "Node with non-empty Key and empty Value", + node: &node{Key: []byte("key1"), Value: []byte("")}, + cost: 4, + }, + { + name: "Node with empty Key and non-empty Value", + node: &node{Key: []byte(""), Value: []byte("value1")}, + cost: 6, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.node.Cost(); got != tt.cost { + t.Errorf("Cost() = %v, want %v", got, tt.cost) + } + }) + } +} + func TestStoreGetSet(t *testing.T) { t.Parallel() |