go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/settings/settings_test.go (about)

     1  // Copyright 2015 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 settings
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/common/clock/testclock"
    23  	"go.chromium.org/luci/common/logging"
    24  	"go.chromium.org/luci/common/logging/memlogger"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  )
    28  
    29  type exampleSettings struct {
    30  	Greetings string `json:"greetings"`
    31  }
    32  
    33  type anotherSettings struct{}
    34  
    35  func TestSettings(t *testing.T) {
    36  	Convey("with in-memory settings", t, func() {
    37  		ctx, tc := testclock.UseTime(context.Background(), time.Unix(1444945245, 0))
    38  		ctx = memlogger.Use(ctx)
    39  		log := logging.Get(ctx).(*memlogger.MemLogger)
    40  
    41  		settings := New(&MemoryStorage{Expiration: time.Second})
    42  		s := exampleSettings{}
    43  
    44  		Convey("settings API works", func() {
    45  			// Nothing is set yet.
    46  			So(settings.Get(ctx, "key", &s), ShouldEqual, ErrNoSettings)
    47  
    48  			// Set something.
    49  			So(settings.Set(ctx, "key", &exampleSettings{"hi"}), ShouldBeNil)
    50  
    51  			// Old value (the lack of there of) is still cached.
    52  			So(settings.Get(ctx, "key", &s), ShouldEqual, ErrNoSettings)
    53  
    54  			// Non-caching version works.
    55  			So(settings.GetUncached(ctx, "key", &s), ShouldBeNil)
    56  			So(s, ShouldResemble, exampleSettings{"hi"})
    57  
    58  			// Advance time to make old value expired.
    59  			tc.Add(2 * time.Second)
    60  			So(settings.Get(ctx, "key", &s), ShouldBeNil)
    61  			So(s, ShouldResemble, exampleSettings{"hi"})
    62  
    63  			// Not a pointer.
    64  			So(settings.Get(ctx, "key", s), ShouldEqual, ErrBadType)
    65  
    66  			// Not *exampleSettings.
    67  			So(settings.Get(ctx, "key", &anotherSettings{}), ShouldEqual, ErrBadType)
    68  		})
    69  
    70  		Convey("SetIfChanged works", func() {
    71  			// Initial value. New change notification.
    72  			So(settings.SetIfChanged(ctx, "key", &exampleSettings{"hi"}), ShouldBeNil)
    73  			So(len(log.Messages()), ShouldEqual, 1)
    74  			log.Reset()
    75  
    76  			// Noop change. No change notification.
    77  			So(settings.SetIfChanged(ctx, "key", &exampleSettings{"hi"}), ShouldBeNil)
    78  			So(len(log.Messages()), ShouldEqual, 0)
    79  
    80  			// Some real change. New change notification.
    81  			So(settings.SetIfChanged(ctx, "key", &exampleSettings{"boo"}), ShouldBeNil)
    82  			So(len(log.Messages()), ShouldEqual, 1)
    83  		})
    84  	})
    85  }
    86  
    87  func TestContext(t *testing.T) {
    88  	Convey("Works", t, func() {
    89  		ctx := context.Background()
    90  		s := exampleSettings{}
    91  
    92  		So(Get(ctx, "key", &exampleSettings{}), ShouldEqual, ErrNoSettings)
    93  		So(GetUncached(ctx, "key", &exampleSettings{}), ShouldEqual, ErrNoSettings)
    94  		So(Set(ctx, "key", &exampleSettings{}), ShouldEqual, ErrNoSettings)
    95  		So(SetIfChanged(ctx, "key", &exampleSettings{}), ShouldEqual, ErrNoSettings)
    96  
    97  		ctx = Use(ctx, New(&MemoryStorage{}))
    98  		So(Set(ctx, "key", &exampleSettings{"hi"}), ShouldBeNil)
    99  		So(SetIfChanged(ctx, "key", &exampleSettings{"hi"}), ShouldBeNil)
   100  
   101  		So(Get(ctx, "key", &s), ShouldBeNil)
   102  		So(s, ShouldResemble, exampleSettings{"hi"})
   103  
   104  		So(GetUncached(ctx, "key", &s), ShouldBeNil)
   105  		So(s, ShouldResemble, exampleSettings{"hi"})
   106  	})
   107  }