go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/appengine/gaemiddleware/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 gaemiddleware 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "go.chromium.org/luci/common/clock" 23 "go.chromium.org/luci/common/clock/testclock" 24 "go.chromium.org/luci/gae/impl/memory" 25 "go.chromium.org/luci/server/caching" 26 27 . "github.com/smartystreets/goconvey/convey" 28 ) 29 30 func TestGlobalCache(t *testing.T) { 31 t.Parallel() 32 33 Convey("Works", t, func() { 34 ctx := context.Background() 35 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC) 36 ctx = memory.Use(ctx) 37 ctx = caching.WithGlobalCache(ctx, blobCacheProvider) 38 39 cache := caching.GlobalCache(ctx, "namespace") 40 41 // Cache miss. 42 val, err := cache.Get(ctx, "key") 43 So(err, ShouldEqual, caching.ErrCacheMiss) 44 So(val, ShouldBeNil) 45 46 So(cache.Set(ctx, "key_permanent", []byte("1"), 0), ShouldBeNil) 47 So(cache.Set(ctx, "key_temp", []byte("2"), time.Minute), ShouldBeNil) 48 49 // Cache hit. 50 val, err = cache.Get(ctx, "key_permanent") 51 So(err, ShouldBeNil) 52 So(val, ShouldResemble, []byte("1")) 53 54 val, err = cache.Get(ctx, "key_temp") 55 So(err, ShouldBeNil) 56 So(val, ShouldResemble, []byte("2")) 57 58 // Expire one item. 59 clock.Get(ctx).(testclock.TestClock).Add(2 * time.Minute) 60 61 val, err = cache.Get(ctx, "key_permanent") 62 So(err, ShouldBeNil) 63 So(val, ShouldResemble, []byte("1")) 64 65 // Expired! 66 val, err = cache.Get(ctx, "key_temp") 67 So(err, ShouldEqual, caching.ErrCacheMiss) 68 So(val, ShouldBeNil) 69 }) 70 }