github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/cache/dispatcher_test.go (about) 1 // MIT License 2 3 // Copyright (c) 2020 Tree Xie 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package cache 24 25 import ( 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestLRUGetCache(t *testing.T) { 32 assert := assert.New(t) 33 httpLRU := newHTTPLRUCache(10) 34 key := []byte("abcd") 35 c, ok := httpLRU.getCache(key) 36 assert.False(ok) 37 assert.Nil(c) 38 39 httpLRU.cache.Add(string(key), "abc") 40 c, ok = httpLRU.getCache(key) 41 assert.False(ok) 42 assert.Nil(c) 43 44 hc := &httpCache{} 45 httpLRU.cache.Add(string(key), hc) 46 c, ok = httpLRU.getCache(key) 47 assert.True(ok) 48 assert.Equal(hc, c) 49 } 50 51 func TestDispatcher(t *testing.T) { 52 assert := assert.New(t) 53 d := NewDispatcher(DispatcherOption{ 54 Size: 0, 55 HitForPass: 30, 56 }) 57 assert.Equal(30, d.GetHitForPass()) 58 key := []byte("key") 59 c := d.GetHTTPCache(key) 60 c.createdAt = 1 61 for i := 0; i < 10; i++ { 62 assert.Equal(c, d.GetHTTPCache([]byte("key"))) 63 } 64 d.RemoveHTTPCache(key) 65 hc := d.GetHTTPCache(key) 66 assert.NotNil(hc) 67 assert.Empty(hc.createdAt) 68 } 69 70 func TestDispatchers(t *testing.T) { 71 assert := assert.New(t) 72 name1 := "test1" 73 name2 := "test2" 74 ds := NewDispatchers([]DispatcherOption{ 75 { 76 Name: name1, 77 Size: 100, 78 }, 79 }) 80 assert.NotNil(ds.Get(name1)) 81 // 第一次reset,清除name1,添加name2 82 ds.Reset([]DispatcherOption{ 83 { 84 Name: name2, 85 Size: 100, 86 }, 87 }) 88 assert.Nil(ds.Get(name1)) 89 assert.NotNil(ds.Get(name2)) 90 91 // 再次reset 92 ds.Reset([]DispatcherOption{ 93 { 94 Name: name2, 95 Size: 100, 96 }, 97 }) 98 assert.Nil(ds.Get(name1)) 99 assert.NotNil(ds.Get(name2)) 100 101 key := []byte("abc") 102 hc := ds.Get(name2).GetHTTPCache(key) 103 assert.NotNil(hc) 104 hc.createdAt = 1 105 106 ds.RemoveHTTPCache("", key) 107 hc1 := ds.Get(name2).GetHTTPCache(key) 108 assert.Empty(hc1.createdAt) 109 110 }