Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Unified Diff: go/util/testing.go

Issue 1401563003: Enable clear/purge for failed digests (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Added unit test to FileDiffStore Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/util/caching_test.go ('k') | golden/go/diff/diff.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/util/testing.go
diff --git a/go/util/testing.go b/go/util/testing.go
new file mode 100644
index 0000000000000000000000000000000000000000..7badf8700532493927c39d8a841e3eb9d3144df6
--- /dev/null
+++ b/go/util/testing.go
@@ -0,0 +1,77 @@
+package util
+
+import (
+ "fmt"
+ "strconv"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+type myTestType struct {
+ A int
+ B string
+}
+
+func UnitTestCodec() LRUCodec {
+ return JSONCodec(&myTestType{})
+}
+
+func UnitTestLRUCache(t *testing.T, cache LRUCache) {
+ purge(t, cache)
+ N := 256
+ for i := 0; i < N; i++ {
+ cache.Add(strconv.Itoa(i), i)
+ }
+
+ // Make sure out keys are correct
+ assert.Equal(t, N, cache.Len())
+ cacheKeys := cache.Keys()
+ assert.Equal(t, N, len(cacheKeys))
+ for _, k := range cacheKeys {
+ assert.IsType(t, "", k)
+ v, ok := cache.Get(k)
+ assert.True(t, ok)
+ assert.IsType(t, 0, v)
+ assert.Equal(t, k, strconv.Itoa(v.(int)))
+ }
+
+ for i := 0; i < N; i++ {
+ found, ok := cache.Get(strconv.Itoa(i))
+ assert.True(t, ok)
+ assert.IsType(t, 0, found)
+ assert.Equal(t, found.(int), i)
+ }
+
+ for i := 0; i < N; i++ {
+ _, ok := cache.Get(strconv.Itoa(i))
+ assert.True(t, ok)
+ oldLen := cache.Len()
+ cache.Remove(strconv.Itoa(i))
+ assert.Equal(t, oldLen-1, cache.Len())
+ }
+ assert.Equal(t, 0, cache.Len())
+
+ // Add some TestStructs to make sure the codec works.
+ for i := 0; i < N; i++ {
+ strKey := "structkey-" + strconv.Itoa(i)
+
+ ts := &myTestType{
+ A: i,
+ B: fmt.Sprintf("Val %d", i),
+ }
+ cache.Add(strKey, ts)
+ assert.Equal(t, i+1, cache.Len())
+ foundTS, ok := cache.Get(strKey)
+ assert.True(t, ok)
+ assert.IsType(t, &myTestType{}, foundTS)
+ assert.Equal(t, ts, foundTS)
+ }
+}
+
+func purge(t *testing.T, cache LRUCache) {
+ for _, k := range cache.Keys() {
+ cache.Remove(k)
+ }
+ assert.Equal(t, 0, cache.Len())
+}
« no previous file with comments | « go/util/caching_test.go ('k') | golden/go/diff/diff.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698