1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
8 "git.curoverse.com/arvados.git/sdk/go/arvados"
9 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
10 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14 func (s *UnitSuite) TestCache(c *check.C) {
15 arv, err := arvadosclient.MakeArvadosClient()
16 c.Assert(err, check.Equals, nil)
18 cache := DefaultConfig().Cache
20 // Hit the same collection 5 times using the same token. Only
21 // the first req should cause an API call; the next 4 should
23 arv.ApiToken = arvadostest.AdminToken
24 var coll *arvados.Collection
25 for i := 0; i < 5; i++ {
26 coll, err = cache.Get(arv, arvadostest.FooCollection, false)
27 c.Check(err, check.Equals, nil)
28 c.Assert(coll, check.NotNil)
29 c.Check(coll.PortableDataHash, check.Equals, arvadostest.FooPdh)
30 c.Check(coll.ManifestText[:2], check.Equals, ". ")
32 c.Check(cache.Stats().Requests, check.Equals, uint64(5))
33 c.Check(cache.Stats().CollectionHits, check.Equals, uint64(4))
34 c.Check(cache.Stats().PermissionHits, check.Equals, uint64(4))
35 c.Check(cache.Stats().PDHHits, check.Equals, uint64(4))
36 c.Check(cache.Stats().APICalls, check.Equals, uint64(1))
38 // Hit the same collection 2 more times, this time requesting
39 // it by PDH and using a different token. The first req should
40 // miss the permission cache and fetch the new manifest; the
41 // second should hit the Collection cache and skip the API
43 arv.ApiToken = arvadostest.ActiveToken
45 coll2, err := cache.Get(arv, arvadostest.FooPdh, false)
46 c.Check(err, check.Equals, nil)
47 c.Assert(coll2, check.NotNil)
48 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooPdh)
49 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
50 c.Check(coll2.ManifestText, check.Not(check.Equals), coll.ManifestText)
52 c.Check(cache.Stats().Requests, check.Equals, uint64(5+1))
53 c.Check(cache.Stats().CollectionHits, check.Equals, uint64(4+0))
54 c.Check(cache.Stats().PermissionHits, check.Equals, uint64(4+0))
55 c.Check(cache.Stats().PDHHits, check.Equals, uint64(4+0))
56 c.Check(cache.Stats().APICalls, check.Equals, uint64(1+1))
58 coll2, err = cache.Get(arv, arvadostest.FooPdh, false)
59 c.Check(err, check.Equals, nil)
60 c.Assert(coll2, check.NotNil)
61 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooPdh)
62 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
64 c.Check(cache.Stats().Requests, check.Equals, uint64(5+2))
65 c.Check(cache.Stats().CollectionHits, check.Equals, uint64(4+1))
66 c.Check(cache.Stats().PermissionHits, check.Equals, uint64(4+1))
67 c.Check(cache.Stats().PDHHits, check.Equals, uint64(4+0))
68 c.Check(cache.Stats().APICalls, check.Equals, uint64(1+1))
70 // Alternating between two collections N times should produce
71 // only 2 more API calls.
72 arv.ApiToken = arvadostest.AdminToken
73 for i := 0; i < 20; i++ {
76 target = arvadostest.HelloWorldCollection
78 target = arvadostest.FooBarDirCollection
80 _, err := cache.Get(arv, target, false)
81 c.Check(err, check.Equals, nil)
83 c.Check(cache.Stats().Requests, check.Equals, uint64(5+2+20))
84 c.Check(cache.Stats().CollectionHits, check.Equals, uint64(4+1+18))
85 c.Check(cache.Stats().PermissionHits, check.Equals, uint64(4+1+18))
86 c.Check(cache.Stats().PDHHits, check.Equals, uint64(4+0+18))
87 c.Check(cache.Stats().APICalls, check.Equals, uint64(1+1+2))
90 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
91 arv, err := arvadosclient.MakeArvadosClient()
92 c.Assert(err, check.Equals, nil)
94 cache := DefaultConfig().Cache
96 for _, forceReload := range []bool{false, true, false, true} {
97 _, err := cache.Get(arv, arvadostest.FooPdh, forceReload)
98 c.Check(err, check.Equals, nil)
101 c.Check(cache.Stats().Requests, check.Equals, uint64(4))
102 c.Check(cache.Stats().CollectionHits, check.Equals, uint64(3))
103 c.Check(cache.Stats().PermissionHits, check.Equals, uint64(1))
104 c.Check(cache.Stats().PDHHits, check.Equals, uint64(0))
105 c.Check(cache.Stats().APICalls, check.Equals, uint64(3))
108 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
109 arv, err := arvadosclient.MakeArvadosClient()
110 c.Assert(err, check.Equals, nil)
112 cache := DefaultConfig().Cache
114 for _, forceReload := range []bool{false, true, false, true} {
115 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
116 c.Check(err, check.Equals, nil)
119 c.Check(cache.Stats().Requests, check.Equals, uint64(4))
120 c.Check(cache.Stats().CollectionHits, check.Equals, uint64(3))
121 c.Check(cache.Stats().PermissionHits, check.Equals, uint64(1))
122 c.Check(cache.Stats().PDHHits, check.Equals, uint64(3))
123 c.Check(cache.Stats().APICalls, check.Equals, uint64(3))