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

Side by Side Diff: server/auth/cache_test.go

Issue 2646733008: server/auth: Add in-process LRU-based cache. (Closed)
Patch Set: Rename GlobalCache => Cache, ProcCache => MemoryCache. Created 3 years, 11 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 unified diff | Download patch
« no previous file with comments | « server/auth/cache.go ('k') | server/auth/config.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package auth 5 package auth
6 6
7 import ( 7 import (
8 "math/rand" 8 "math/rand"
9 "testing" 9 "testing"
10 "time" 10 "time"
11 11
12 "golang.org/x/net/context" 12 "golang.org/x/net/context"
13 13
14 "github.com/luci/luci-go/common/clock" 14 "github.com/luci/luci-go/common/clock"
15 "github.com/luci/luci-go/common/clock/testclock" 15 "github.com/luci/luci-go/common/clock/testclock"
16 "github.com/luci/luci-go/common/data/rand/mathrand" 16 "github.com/luci/luci-go/common/data/rand/mathrand"
17 17
18 . "github.com/smartystreets/goconvey/convey" 18 . "github.com/smartystreets/goconvey/convey"
19 ) 19 )
20 20
21 func TestTokenCache(t *testing.T) { 21 func TestTokenCache(t *testing.T) {
22 t.Parallel() 22 t.Parallel()
23 23
24 » Convey("with mocked cache", t, func() { 24 » Convey("with in-process cache", t, func() {
25 » » cache := &mockedCache{} 25 » » // Create a cache large enough that the LRU doesn't cycle.
26 » » cache := MemoryCache(1024)
26 27
27 ctx := context.Background() 28 ctx := context.Background()
28 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC) 29 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC)
29 ctx = mathrand.Set(ctx, rand.New(rand.NewSource(12345))) 30 ctx = mathrand.Set(ctx, rand.New(rand.NewSource(12345)))
30 » » ctx = SetConfig(ctx, Config{GlobalCache: cache}) 31 » » ctx = SetConfig(ctx, Config{Cache: cache})
31 32
32 tc := tokenCache{ 33 tc := tokenCache{
33 Kind: "testing", 34 Kind: "testing",
34 Version: 1, 35 Version: 1,
35 ExpRandPercent: 10, 36 ExpRandPercent: 10,
36 } 37 }
37 38
38 Convey("check basic usage", func() { 39 Convey("check basic usage", func() {
39 itm, err := tc.Fetch(ctx, "some\nkey") 40 itm, err := tc.Fetch(ctx, "some\nkey")
40 So(err, ShouldBeNil) 41 So(err, ShouldBeNil)
41 So(itm, ShouldBeNil) 42 So(itm, ShouldBeNil)
42 43
43 tok := cachedToken{ 44 tok := cachedToken{
44 Key: "some\nkey", 45 Key: "some\nkey",
45 Token: "blah", 46 Token: "blah",
46 Created: clock.Now(ctx).UTC(), 47 Created: clock.Now(ctx).UTC(),
47 Expiry: clock.Now(ctx).Add(100 * time.Minute).U TC(), 48 Expiry: clock.Now(ctx).Add(100 * time.Minute).U TC(),
48 } 49 }
49 So(tc.Store(ctx, tok), ShouldBeNil) 50 So(tc.Store(ctx, tok), ShouldBeNil)
50 51
51 itm, err = tc.Fetch(ctx, "some\nkey") 52 itm, err = tc.Fetch(ctx, "some\nkey")
52 So(err, ShouldBeNil) 53 So(err, ShouldBeNil)
53 So(itm, ShouldResemble, &tok) 54 So(itm, ShouldResemble, &tok)
54 55
55 » » » So(len(cache.data), ShouldEqual, 1) 56 » » » pgc := cache.(memoryCache)
56 » » » for key := range cache.data { 57 » » » So(pgc.cache.Len(), ShouldEqual, 1)
57 » » » » So(key, ShouldEqual, "testing/1/na7cNnVNdP6Ydb9K 9UIEST04OV8")
58 » » » }
59 }) 58 })
60 59
61 Convey("check expiration randomization", func() { 60 Convey("check expiration randomization", func() {
62 tok := cachedToken{ 61 tok := cachedToken{
63 Key: "some\nkey", 62 Key: "some\nkey",
64 Token: "blah", 63 Token: "blah",
65 Created: clock.Now(ctx).UTC(), 64 Created: clock.Now(ctx).UTC(),
66 Expiry: clock.Now(ctx).Add(100 * time.Minute).U TC(), 65 Expiry: clock.Now(ctx).Add(100 * time.Minute).U TC(),
67 } 66 }
68 So(tc.Store(ctx, tok), ShouldBeNil) 67 So(tc.Store(ctx, tok), ShouldBeNil)
(...skipping 19 matching lines...) Expand all
88 87
89 // Token has expired. 0% chance of seeing it. 88 // Token has expired. 0% chance of seeing it.
90 clock.Get(ctx).(testclock.TestClock).Add(9 * time.Minute ) 89 clock.Get(ctx).(testclock.TestClock).Add(9 * time.Minute )
91 for i := 0; i < 50; i++ { 90 for i := 0; i < 50; i++ {
92 itm, _ := tc.Fetch(ctx, "some\nkey") 91 itm, _ := tc.Fetch(ctx, "some\nkey")
93 So(itm, ShouldBeNil) 92 So(itm, ShouldBeNil)
94 } 93 }
95 }) 94 })
96 }) 95 })
97 } 96 }
98
99 // mockedCache is GlobalCache implementation for tests.
100 type mockedCache struct {
101 data map[string][]byte
102 }
103
104 func (mc *mockedCache) Get(c context.Context, key string) ([]byte, error) {
105 return mc.data[key], nil
106 }
107
108 func (mc *mockedCache) Set(c context.Context, key string, value []byte, exp time .Duration) error {
109 if mc.data == nil {
110 mc.data = map[string][]byte{}
111 }
112 mc.data[key] = value
113 return nil
114 }
OLDNEW
« no previous file with comments | « server/auth/cache.go ('k') | server/auth/config.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698