github.com/msales/pkg/v3@v3.24.0/cache/memcache_internal_test.go (about) 1 package cache 2 3 import ( 4 "errors" 5 "testing" 6 "time" 7 8 "github.com/bradfitz/gomemcache/memcache" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestWithIdleConns(t *testing.T) { 13 c := &memcache.Client{} 14 15 WithIdleConns(12)(c) 16 17 assert.Equal(t, 12, c.MaxIdleConns) 18 } 19 20 func TestWithTimeout(t *testing.T) { 21 c := &memcache.Client{} 22 23 WithTimeout(time.Second)(c) 24 25 assert.Equal(t, time.Second, c.Timeout) 26 } 27 28 func TestNewMemcache(t *testing.T) { 29 c := NewMemcache("test", WithIdleConns(12)).(*memcacheCache) 30 31 assert.Equal(t, 12, c.client.MaxIdleConns) 32 } 33 34 func TestEncoderError(t *testing.T) { 35 c := memcacheCache{ 36 encoder: func(v interface{}) ([]byte, error) { 37 return nil, errors.New("test error") 38 }, 39 } 40 41 assert.EqualError(t, c.Add("test", 1, 0), "test error") 42 assert.EqualError(t, c.Set("test", 1, 0), "test error") 43 assert.EqualError(t, c.Replace("test", 1, 0), "test error") 44 } 45 46 func TestByteEncode(t *testing.T) { 47 tests := []struct { 48 v interface{} 49 expect []byte 50 }{ 51 {true, []byte("1")}, 52 {false, []byte("0")}, 53 {int64(10), []byte("10")}, 54 {uint64(10), []byte("10")}, 55 {float64(10.34), []byte("10.340000")}, 56 {"foobar", []byte("foobar")}, 57 {struct{ A int }{1}, []byte(`{"A":1}`)}, 58 {[]string{"foo", "bar"}, []byte(`["foo","bar"]`)}, 59 } 60 61 for _, tt := range tests { 62 got, err := memcacheEncoder(tt.v) 63 assert.NoError(t, err) 64 65 assert.Equal(t, tt.expect, got) 66 } 67 }