11809: Add unit test for cache.
authorTom Clegg <tom@curoverse.com>
Tue, 6 Jun 2017 19:12:53 +0000 (15:12 -0400)
committerTom Clegg <tom@curoverse.com>
Tue, 6 Jun 2017 19:13:20 +0000 (15:13 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curoverse.com>

services/keep-web/cache_test.go [new file with mode: 0644]

diff --git a/services/keep-web/cache_test.go b/services/keep-web/cache_test.go
new file mode 100644 (file)
index 0000000..ef3a760
--- /dev/null
@@ -0,0 +1,49 @@
+package main
+
+import (
+       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
+       "git.curoverse.com/arvados.git/sdk/go/arvadostest"
+       "gopkg.in/check.v1"
+)
+
+func (s *UnitSuite) TestCache(c *check.C) {
+       arv, err := arvadosclient.MakeArvadosClient()
+       c.Assert(err, check.Equals, nil)
+
+       cache := DefaultConfig().Cache
+
+       // Hit the same collection 5 times using the same token. Only
+       // the first req should cause an API call; the next 4 should
+       // hit all caches.
+       arv.ApiToken = arvadostest.AdminToken
+       for i := 0; i < 5; i++ {
+               coll, err := cache.Get(arv, arvadostest.FooCollection, false)
+               c.Check(err, check.Equals, nil)
+               c.Assert(coll, check.NotNil)
+               c.Check(coll["portable_data_hash"], check.Equals, arvadostest.FooPdh)
+               c.Check(coll["manifest_text"].(string)[:2], check.Equals, ". ")
+       }
+       c.Check(cache.Stats().Requests, check.Equals, uint64(5))
+       c.Check(cache.Stats().CollectionHits, check.Equals, uint64(4))
+       c.Check(cache.Stats().PermissionHits, check.Equals, uint64(4))
+       c.Check(cache.Stats().PDHHits, check.Equals, uint64(4))
+       c.Check(cache.Stats().APICalls, check.Equals, uint64(1))
+
+       // Hit the same collection 2 more times, this time requesting
+       // it by PDH and using a different token. The first req should
+       // miss the permission cache. Both reqs should hit the
+       // Collection cache and skip the API lookup.
+       arv.ApiToken = arvadostest.ActiveToken
+       for i := 0; i < 2; i++ {
+               coll, err := cache.Get(arv, arvadostest.FooPdh, false)
+               c.Check(err, check.Equals, nil)
+               c.Assert(coll, check.NotNil)
+               c.Check(coll["portable_data_hash"], check.Equals, arvadostest.FooPdh)
+               c.Check(coll["manifest_text"].(string)[:2], check.Equals, ". ")
+       }
+       c.Check(cache.Stats().Requests, check.Equals, uint64(7))
+       c.Check(cache.Stats().CollectionHits, check.Equals, uint64(6))
+       c.Check(cache.Stats().PermissionHits, check.Equals, uint64(5))
+       c.Check(cache.Stats().PDHHits, check.Equals, uint64(4))
+       c.Check(cache.Stats().APICalls, check.Equals, uint64(2))
+}