github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/cache/lrucache_test.go (about) 1 /** 2 * Copyright 2022 Confluent Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cache 18 19 import ( 20 "testing" 21 ) 22 23 func TestWrongCapacity(t *testing.T) { 24 for _, capacity := range []int{-1, 0} { 25 _, err := NewLRUCache(capacity) 26 if err == nil { 27 t.Fatalf("expected \"capacity must be a positive integer\" error, not nil\n") 28 } 29 } 30 } 31 32 func TestCRUD(t *testing.T) { 33 cache, err := NewLRUCache(2) 34 if err != nil { 35 t.Fatalf("expected nil error, not \"%s\"\n", err.Error()) 36 } 37 38 for key, values := range map[int][]string{ 39 1: {"test", "test2"}, 40 2: {"tests", "tests2"}, 41 } { 42 firstValue, secondValue := values[0], values[1] 43 cache.Put(key, firstValue) 44 readValue, ok := cache.Get(key) 45 if !ok { 46 t.Fatalf("expected to find key \"%v\"\n", key) 47 } 48 if readValue != firstValue { 49 t.Fatalf("expected to find value \"%v\", not \"%v\"\n", firstValue, readValue) 50 } 51 cache.Put(key, secondValue) 52 readValue, ok = cache.Get(key) 53 if !ok { 54 t.Fatalf("expected to find key \"%v\"\n", key) 55 } 56 if readValue != secondValue { 57 t.Fatalf("expected to find value \"%v\", not \"%v\"\n", secondValue, readValue) 58 } 59 cache.Delete(key) 60 readValue, ok = cache.Get(key) 61 if ok { 62 t.Fatalf("value for key \"%v\" not expected, found \"%v\"\n", key, readValue) 63 } 64 } 65 } 66 67 func TestMaxCapacity(t *testing.T) { 68 cache, err := NewLRUCache(2) 69 if err != nil { 70 t.Fatalf("expected nil error, not \"%s\"\n", err.Error()) 71 } 72 73 cache.Put(1, "test1") 74 cache.Put(2, "test2") 75 cache.Put(3, "test3") 76 77 _, ok := cache.Get(1) 78 if ok { 79 t.Fatalf("not expected to find key 1\n") 80 } 81 _, ok = cache.Get(2) 82 if !ok { 83 t.Fatalf("expected to find key 2\n") 84 } 85 _, ok = cache.Get(3) 86 if !ok { 87 t.Fatalf("expected to find key 3\n") 88 } 89 } 90 91 func TestMaxCapacityWithGet(t *testing.T) { 92 cache, err := NewLRUCache(2) 93 if err != nil { 94 t.Fatalf("expected nil error, not \"%s\"\n", err.Error()) 95 } 96 97 cache.Put(1, "test1") 98 cache.Put(2, "test2") 99 _, ok := cache.Get(1) 100 if !ok { 101 t.Fatalf("expected value \"test1\" not found for key 1\n") 102 } 103 cache.Put(3, "test3") 104 105 if !ok { 106 t.Fatalf("expected to find key 1\n") 107 } 108 _, ok = cache.Get(2) 109 if ok { 110 t.Fatalf("not expected to find key 2\n") 111 } 112 _, ok = cache.Get(3) 113 if !ok { 114 t.Fatalf("expected to find key 3\n") 115 } 116 }