GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — lru-cache/cache_test.go at master · dikaeinstein/lru-cache Expand file tree/ Copy pathcache_test.goMore file actions 151 lines (117 loc) · 3.24 KB / Copy pathcache_test.goFile metadata and controls151 lines (117 loc) · 3.24 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 package cache import ( "testing" "time" ) func TestCacheSetAddNewValue(t *testing.T) { store := NewInMemoryStore() c := New(2, store) expiresAt := time.Now().Add(10 * time.Second).Unix() c.Set("name", "Dika", int(expiresAt)) v, exist := c.Get("name") if !exist || v != "Dika" { t.Errorf("expected: %s, got: %s", "Dika", v) } } func TestCacheSetOverridesExistingKey(t *testing.T) { store := NewInMemoryStore() c := New(1, store) expiresAt := time.Now().Add(10 * time.Second).Unix() c.Set("name", "Dika", int(expiresAt)) c.Set("name", "UpdatedDika", int(expiresAt)) v, exist := c.Get("name") if !exist || v != "UpdatedDika" { t.Errorf("expected: %s, got: %s", "UpdatedDika", v) } } func TestCacheSetRemovesLRUValue(t *testing.T) { store := NewInMemoryStore() c := New(1, store) expiresAt := time.Now().Add(10 * time.Second).Unix() c.Set("name", "Dika", int(expiresAt)) if _, exist := c.Get("name"); exist { t.Errorf("name should have been removed") } email, exist := c.Get("email") } } func TestCacheSetSafeForConcurrentUse(t *testing.T) { store := NewInMemoryStore() c := New(1, store) expiresAt := time.Now().Add(10 * time.Second).Unix() go c.Set("name", "Dika", int(expiresAt)) if c.Size() != 1 { t.Errorf("cache size should be %d, got %d. Only one 'set' operation should proceed", 1, c.Size()) } } func TestCacheGet(t *testing.T) { store := NewInMemoryStore() c := New(2, store) expiresAt := time.Now().Add(10 * time.Second).Unix() c.Set("name", "Dika", int(expiresAt)) v, exist := c.Get("name") if !exist || v != "Dika" { t.Errorf("expected: %s, got: %s", "Dika", v) } } func TestCacheGetReturnsFalseWhenKeyNotFound(t *testing.T) { store := NewInMemoryStore() c := New(2, store) _, exist := c.Get("name") if exist { t.Errorf("expected: %T, got: %T", false, exist) } } func TestCacheGetReturnsFalseWhenKeyHasExpired(t *testing.T) { store := NewInMemoryStore() c := New(2, store) c.Set("name", "Dika", 0) _, exist := c.Get("name") if exist { t.Errorf("Key should have expired. expected: %T, got: %T", false, exist) } } func TestDelete(t *testing.T) { store := NewInMemoryStore() c := New(2, store) expiresAt := time.Now().Add(10 * time.Second).Unix() c.Set("name", "Dika", int(expiresAt)) c.Delete("name") if _, exist := c.Get("name"); exist { t.Errorf("Key should have been deleted. expected: %T, got: %T", false, exist) } } func TestDeleteNoop(t *testing.T) { store := NewInMemoryStore() c := New(1, store) c.Delete("name") if _, exist := c.Get("name"); exist { t.Errorf("Key should have been deleted. expected: %T, got: %T", false, exist) } } func TestHas(t *testing.T) { store := NewInMemoryStore() c := New(1, store) expiresAt := time.Now().Add(10 * time.Second).Unix() c.Set("name", "Dika", int(expiresAt)) exist := c.Has("name") if !exist { t.Errorf("expected %T, got %T", true, exist) } } func TestHasReturnsFalseNotFound(t *testing.T) { store := NewInMemoryStore() c := New(1, store) exist := c.Has("name") if exist { t.Errorf("expected %T, got %T", false, exist) } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |