OLD | NEW |
1 package redisutil | 1 package redisutil |
2 | 2 |
3 import ( | 3 import ( |
4 "encoding/json" | 4 "encoding/json" |
5 "fmt" | 5 "fmt" |
6 "io/ioutil" | 6 "io/ioutil" |
7 "os" | 7 "os" |
8 "path/filepath" | 8 "path/filepath" |
9 "runtime" | 9 "runtime" |
10 "sort" | 10 "sort" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 PixelDiffPercent float32 | 43 PixelDiffPercent float32 |
44 PixelDiffFilePath string | 44 PixelDiffFilePath string |
45 // Contains the maximum difference between the images for each R/G/B cha
nnel. | 45 // Contains the maximum difference between the images for each R/G/B cha
nnel. |
46 MaxRGBADiffs []int | 46 MaxRGBADiffs []int |
47 // True if the dimensions of the compared images are different. | 47 // True if the dimensions of the compared images are different. |
48 DimDiffer bool | 48 DimDiffer bool |
49 } | 49 } |
50 | 50 |
51 func TestRedisLRUCache(t *testing.T) { | 51 func TestRedisLRUCache(t *testing.T) { |
52 testutils.SkipIfShort(t) | 52 testutils.SkipIfShort(t) |
53 » cache := NewRedisLRUCache("localhost:6379", 1, "test-di", TestStructCode
c(0)) | 53 » cache := NewRedisLRUCache("localhost:6379", 1, "test-di", util.UnitTestC
odec()) |
54 » testLRUCache(t, cache) | 54 » util.UnitTestLRUCache(t, cache) |
55 } | 55 } |
56 | 56 |
57 func BenchmarkBigDataset(b *testing.B) { | 57 func BenchmarkBigDataset(b *testing.B) { |
58 // Download the testdata and remove the testdata directory at the end. | 58 // Download the testdata and remove the testdata directory at the end. |
59 err := testutils.DownloadTestDataArchive(b, TEST_DATA_STORAGE_PATH, TEST
_DATA_DIR) | 59 err := testutils.DownloadTestDataArchive(b, TEST_DATA_STORAGE_PATH, TEST
_DATA_DIR) |
60 assert.Nil(b, err, "Unable to download testdata.") | 60 assert.Nil(b, err, "Unable to download testdata.") |
61 defer func() { | 61 defer func() { |
62 util.LogErr(os.RemoveAll(TEST_DATA_DIR)) | 62 util.LogErr(os.RemoveAll(TEST_DATA_DIR)) |
63 }() | 63 }() |
64 | 64 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 func (t TestStructCodec) Encode(v interface{}) ([]byte, error) { | 140 func (t TestStructCodec) Encode(v interface{}) ([]byte, error) { |
141 return json.Marshal(v) | 141 return json.Marshal(v) |
142 } | 142 } |
143 | 143 |
144 func (t TestStructCodec) Decode(data []byte) (interface{}, error) { | 144 func (t TestStructCodec) Decode(data []byte) (interface{}, error) { |
145 var v TestStruct | 145 var v TestStruct |
146 err := json.Unmarshal(data, &v) | 146 err := json.Unmarshal(data, &v) |
147 return &v, err | 147 return &v, err |
148 } | 148 } |
149 | 149 |
150 func testLRUCache(t *testing.T, cache util.LRUCache) { | |
151 rlruCache := cache.(*RedisLRUCache) | |
152 rlruCache.Purge() | |
153 N := 256 | |
154 for i := 0; i < N; i++ { | |
155 cache.Add(strconv.Itoa(i), i) | |
156 } | |
157 | |
158 // Make sure out keys are correct | |
159 assert.Equal(t, N, cache.Len()) | |
160 assert.Equal(t, N, len(rlruCache.Keys())) | |
161 for _, k := range rlruCache.Keys() { | |
162 assert.IsType(t, "", k) | |
163 v, ok := cache.Get(k) | |
164 assert.True(t, ok) | |
165 assert.IsType(t, 0, v) | |
166 assert.Equal(t, k, strconv.Itoa(v.(int))) | |
167 } | |
168 | |
169 for i := 0; i < N; i++ { | |
170 found, ok := cache.Get(strconv.Itoa(i)) | |
171 assert.True(t, ok) | |
172 assert.IsType(t, 0, found) | |
173 assert.Equal(t, found.(int), i) | |
174 } | |
175 | |
176 for i := 0; i < N; i++ { | |
177 _, ok := cache.Get(strconv.Itoa(i)) | |
178 assert.True(t, ok) | |
179 oldLen := cache.Len() | |
180 cache.Remove(strconv.Itoa(i)) | |
181 assert.Equal(t, oldLen-1, cache.Len()) | |
182 } | |
183 assert.Equal(t, 0, cache.Len()) | |
184 | |
185 // Add some TestStructs to make sure the codec works. | |
186 for i := 0; i < N; i++ { | |
187 strKey := "structkey-" + strconv.Itoa(i) | |
188 ts := &TestStruct{ | |
189 PixelDiffFilePath: "somesting-" + strconv.Itoa(i), | |
190 MaxRGBADiffs: []int{i * 4, i*4 + 1, i*4 + 2, i*4 +
3}, | |
191 } | |
192 cache.Add(strKey, ts) | |
193 assert.Equal(t, i+1, cache.Len()) | |
194 foundTS, ok := cache.Get(strKey) | |
195 assert.True(t, ok) | |
196 assert.IsType(t, &TestStruct{}, foundTS) | |
197 assert.Equal(t, ts, foundTS) | |
198 } | |
199 } | |
200 | |
201 type StringCodec struct{} | 150 type StringCodec struct{} |
202 | 151 |
203 func (s StringCodec) Encode(data interface{}) ([]byte, error) { | 152 func (s StringCodec) Encode(data interface{}) ([]byte, error) { |
204 return []byte(data.(string)), nil | 153 return []byte(data.(string)), nil |
205 } | 154 } |
206 | 155 |
207 func (s StringCodec) Decode(byteData []byte) (interface{}, error) { | 156 func (s StringCodec) Decode(byteData []byte) (interface{}, error) { |
208 return string(byteData), nil | 157 return string(byteData), nil |
209 } | 158 } |
210 | 159 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 foundHash, err := rp.LoadHashToStruct(TEST_HASH_KEY, &ts2) | 287 foundHash, err := rp.LoadHashToStruct(TEST_HASH_KEY, &ts2) |
339 assert.Nil(t, err) | 288 assert.Nil(t, err) |
340 assert.True(t, foundHash) | 289 assert.True(t, foundHash) |
341 assert.Equal(t, ts1, ts2) | 290 assert.Equal(t, ts1, ts2) |
342 | 291 |
343 assert.Nil(t, rp.DeleteKey(TEST_HASH_KEY)) | 292 assert.Nil(t, rp.DeleteKey(TEST_HASH_KEY)) |
344 foundHash, err = rp.LoadHashToStruct(TEST_HASH_KEY, &ts2) | 293 foundHash, err = rp.LoadHashToStruct(TEST_HASH_KEY, &ts2) |
345 assert.Nil(t, err) | 294 assert.Nil(t, err) |
346 assert.False(t, foundHash) | 295 assert.False(t, foundHash) |
347 } | 296 } |
OLD | NEW |