go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/internal/proc_cache_test.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package internal 16 17 import ( 18 "context" 19 "fmt" 20 "sync" 21 "testing" 22 "time" 23 24 "golang.org/x/oauth2" 25 26 . "github.com/smartystreets/goconvey/convey" 27 "go.chromium.org/luci/common/clock" 28 ) 29 30 func TestMemoryTokenCache(t *testing.T) { 31 t.Parallel() 32 33 Convey("MemoryTokenCache works", t, func() { 34 testCacheSemantics(context.Background(), &MemoryTokenCache{}) 35 }) 36 37 Convey("MemoryTokenCache works (parallel)", t, func() { 38 testCacheInParallel(context.Background(), &MemoryTokenCache{}) 39 }) 40 } 41 42 func testCacheSemantics(ctx context.Context, cache TokenCache) { 43 // Missing is fine. 44 tok, err := cache.GetToken(&CacheKey{Key: "missing"}) 45 So(err, ShouldBeNil) 46 So(tok, ShouldBeNil) 47 48 // Deleting missing is fine. 49 So(cache.DeleteToken(&CacheKey{Key: "missing"}), ShouldBeNil) 50 51 // Put -> Get. 52 tok1 := &Token{ 53 Token: oauth2.Token{ 54 AccessToken: "abc", 55 Expiry: clock.Now(ctx).Add(time.Hour).UTC(), 56 }, 57 Email: "some@example.com", 58 } 59 So(cache.PutToken(&CacheKey{Key: "k"}, tok1), ShouldBeNil) 60 tok, err = cache.GetToken(&CacheKey{Key: "k"}) 61 So(err, ShouldBeNil) 62 So(tok, ShouldResemble, tok1) 63 64 // Put a bunch more. 65 for i := 0; i < 5; i++ { 66 So(cache.PutToken(&CacheKey{Key: fmt.Sprintf("k%d", i)}, tok1), ShouldBeNil) 67 } 68 69 // Overwrite -> Get. 70 tok2 := &Token{ 71 Token: oauth2.Token{ 72 AccessToken: "def", 73 Expiry: clock.Now(ctx).Add(2 * time.Hour).UTC(), 74 }, 75 Email: "another@example.com", 76 } 77 So(cache.PutToken(&CacheKey{Key: "k"}, tok2), ShouldBeNil) 78 tok, err = cache.GetToken(&CacheKey{Key: "k"}) 79 So(err, ShouldBeNil) 80 So(tok, ShouldResemble, tok2) 81 82 // Delete -> Get. 83 So(cache.DeleteToken(&CacheKey{Key: "k"}), ShouldBeNil) 84 tok, err = cache.GetToken(&CacheKey{Key: "k"}) 85 So(err, ShouldBeNil) 86 So(tok, ShouldBeNil) 87 88 // The rest is still there. Remove it. 89 for i := 0; i < 5; i++ { 90 k := &CacheKey{Key: fmt.Sprintf("k%d", i)} 91 tok, err = cache.GetToken(k) 92 So(err, ShouldBeNil) 93 So(tok, ShouldResemble, tok1) 94 So(cache.DeleteToken(k), ShouldBeNil) 95 } 96 } 97 98 func testCacheInParallel(ctx context.Context, cache TokenCache) { 99 tok := &Token{ 100 Token: oauth2.Token{ 101 AccessToken: "zzz", 102 Expiry: clock.Now(ctx).Add(365 * 24 * time.Hour), 103 }, 104 } 105 106 const ( 107 threads = 30 108 attempts = 15 109 ) 110 111 // There are no guarantees in general that parallel writes to the cache will 112 // not lose some data (see DiskTokenCache comments). But each op should be 113 // successful, even if its results are later silently discarded. 114 wg := sync.WaitGroup{} 115 errs := make(chan error, threads*attempts) 116 for i := 0; i < threads; i++ { 117 wg.Add(1) 118 go func() { 119 defer wg.Done() 120 for j := 0; j < attempts; j++ { 121 err := cache.PutToken(&CacheKey{Key: fmt.Sprintf("%d", j)}, tok) 122 if err != nil { 123 errs <- err 124 } 125 } 126 }() 127 } 128 wg.Wait() 129 130 // The channel with errors should be empty. 131 select { 132 case err := <-errs: 133 So(err, ShouldBeNil) 134 default: 135 } 136 }