1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
10 "git.arvados.org/arvados.git/sdk/go/arvados"
11 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
12 "git.arvados.org/arvados.git/sdk/go/arvadostest"
13 "git.arvados.org/arvados.git/sdk/go/ctxlog"
14 "github.com/prometheus/client_golang/prometheus"
15 "github.com/prometheus/common/expfmt"
19 func (s *UnitSuite) checkCacheMetrics(c *check.C, reg *prometheus.Registry, regs ...string) {
20 mfs, err := reg.Gather()
21 c.Check(err, check.IsNil)
22 buf := &bytes.Buffer{}
23 enc := expfmt.NewEncoder(buf, expfmt.FmtText)
24 for _, mf := range mfs {
25 c.Check(enc.Encode(mf), check.IsNil)
28 for _, reg := range regs {
29 c.Check(mm, check.Matches, `(?ms).*collectioncache_`+reg+`\n.*`)
33 func (s *UnitSuite) TestCache(c *check.C) {
34 arv, err := arvadosclient.MakeArvadosClient()
35 c.Assert(err, check.Equals, nil)
39 logger: ctxlog.TestLogger(c),
40 registry: prometheus.NewRegistry(),
43 // Hit the same collection 5 times using the same token. Only
44 // the first req should cause an API call; the next 4 should
46 arv.ApiToken = arvadostest.AdminToken
47 var coll *arvados.Collection
48 for i := 0; i < 5; i++ {
49 coll, err = cache.Get(arv, arvadostest.FooCollection, false)
50 c.Check(err, check.Equals, nil)
51 c.Assert(coll, check.NotNil)
52 c.Check(coll.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
53 c.Check(coll.ManifestText[:2], check.Equals, ". ")
55 s.checkCacheMetrics(c, cache.registry,
61 // Hit the same collection 2 more times, this time requesting
62 // it by PDH and using a different token. The first req should
63 // miss the permission cache and fetch the new manifest; the
64 // second should hit the Collection cache and skip the API
66 arv.ApiToken = arvadostest.ActiveToken
68 coll2, err := cache.Get(arv, arvadostest.FooCollectionPDH, false)
69 c.Check(err, check.Equals, nil)
70 c.Assert(coll2, check.NotNil)
71 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
72 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
73 c.Check(coll2.ManifestText, check.Not(check.Equals), coll.ManifestText)
75 s.checkCacheMetrics(c, cache.registry,
81 coll2, err = cache.Get(arv, arvadostest.FooCollectionPDH, false)
82 c.Check(err, check.Equals, nil)
83 c.Assert(coll2, check.NotNil)
84 c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
85 c.Check(coll2.ManifestText[:2], check.Equals, ". ")
87 s.checkCacheMetrics(c, cache.registry,
93 // Alternating between two collections N times should produce
94 // only 2 more API calls.
95 arv.ApiToken = arvadostest.AdminToken
96 for i := 0; i < 20; i++ {
99 target = arvadostest.HelloWorldCollection
101 target = arvadostest.FooBarDirCollection
103 _, err := cache.Get(arv, target, false)
104 c.Check(err, check.Equals, nil)
106 s.checkCacheMetrics(c, cache.registry,
113 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
114 arv, err := arvadosclient.MakeArvadosClient()
115 c.Assert(err, check.Equals, nil)
119 logger: ctxlog.TestLogger(c),
120 registry: prometheus.NewRegistry(),
123 for _, forceReload := range []bool{false, true, false, true} {
124 _, err := cache.Get(arv, arvadostest.FooCollectionPDH, forceReload)
125 c.Check(err, check.Equals, nil)
128 s.checkCacheMetrics(c, cache.registry,
135 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
136 arv, err := arvadosclient.MakeArvadosClient()
137 c.Assert(err, check.Equals, nil)
141 logger: ctxlog.TestLogger(c),
142 registry: prometheus.NewRegistry(),
145 for _, forceReload := range []bool{false, true, false, true} {
146 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
147 c.Check(err, check.Equals, nil)
150 s.checkCacheMetrics(c, cache.registry,