11809: Add unit test for cache.
[arvados.git] / services / keep-web / cache_test.go
1 package main
2
3 import (
4         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
5         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
6         "gopkg.in/check.v1"
7 )
8
9 func (s *UnitSuite) TestCache(c *check.C) {
10         arv, err := arvadosclient.MakeArvadosClient()
11         c.Assert(err, check.Equals, nil)
12
13         cache := DefaultConfig().Cache
14
15         // Hit the same collection 5 times using the same token. Only
16         // the first req should cause an API call; the next 4 should
17         // hit all caches.
18         arv.ApiToken = arvadostest.AdminToken
19         for i := 0; i < 5; i++ {
20                 coll, err := cache.Get(arv, arvadostest.FooCollection, false)
21                 c.Check(err, check.Equals, nil)
22                 c.Assert(coll, check.NotNil)
23                 c.Check(coll["portable_data_hash"], check.Equals, arvadostest.FooPdh)
24                 c.Check(coll["manifest_text"].(string)[:2], check.Equals, ". ")
25         }
26         c.Check(cache.Stats().Requests, check.Equals, uint64(5))
27         c.Check(cache.Stats().CollectionHits, check.Equals, uint64(4))
28         c.Check(cache.Stats().PermissionHits, check.Equals, uint64(4))
29         c.Check(cache.Stats().PDHHits, check.Equals, uint64(4))
30         c.Check(cache.Stats().APICalls, check.Equals, uint64(1))
31
32         // Hit the same collection 2 more times, this time requesting
33         // it by PDH and using a different token. The first req should
34         // miss the permission cache. Both reqs should hit the
35         // Collection cache and skip the API lookup.
36         arv.ApiToken = arvadostest.ActiveToken
37         for i := 0; i < 2; i++ {
38                 coll, err := cache.Get(arv, arvadostest.FooPdh, false)
39                 c.Check(err, check.Equals, nil)
40                 c.Assert(coll, check.NotNil)
41                 c.Check(coll["portable_data_hash"], check.Equals, arvadostest.FooPdh)
42                 c.Check(coll["manifest_text"].(string)[:2], check.Equals, ". ")
43         }
44         c.Check(cache.Stats().Requests, check.Equals, uint64(7))
45         c.Check(cache.Stats().CollectionHits, check.Equals, uint64(6))
46         c.Check(cache.Stats().PermissionHits, check.Equals, uint64(5))
47         c.Check(cache.Stats().PDHHits, check.Equals, uint64(4))
48         c.Check(cache.Stats().APICalls, check.Equals, uint64(2))
49 }