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

     1  // Copyright 2020 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 experiments
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  var (
    25  	exp1 = Register("exp1")
    26  	exp2 = Register("exp2")
    27  )
    28  
    29  func TestExperiments(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	Convey("Works", t, func() {
    33  		e1, ok := GetByName("exp1")
    34  		So(ok, ShouldBeTrue)
    35  		So(e1, ShouldResemble, exp1)
    36  		So(e1.Valid(), ShouldBeTrue)
    37  
    38  		bad, ok := GetByName("bad")
    39  		So(ok, ShouldBeFalse)
    40  		So(bad.Valid(), ShouldBeFalse)
    41  
    42  		ctx := context.Background()
    43  		So(exp1.Enabled(ctx), ShouldBeFalse)
    44  		So(exp2.Enabled(ctx), ShouldBeFalse)
    45  
    46  		ctx = Enable(ctx) // noop
    47  		So(exp1.Enabled(ctx), ShouldBeFalse)
    48  		So(exp2.Enabled(ctx), ShouldBeFalse)
    49  
    50  		ctx = Enable(ctx, exp1)
    51  		So(exp1.Enabled(ctx), ShouldBeTrue)
    52  		So(exp2.Enabled(ctx), ShouldBeFalse)
    53  
    54  		ctx = Enable(ctx, exp1) // noop
    55  		So(exp1.Enabled(ctx), ShouldBeTrue)
    56  		So(exp2.Enabled(ctx), ShouldBeFalse)
    57  
    58  		ctx2 := Enable(ctx, exp2)
    59  		So(exp1.Enabled(ctx2), ShouldBeTrue)
    60  		So(exp2.Enabled(ctx2), ShouldBeTrue)
    61  
    62  		// Hasn't touched the existing context.
    63  		So(exp2.Enabled(ctx), ShouldBeFalse)
    64  	})
    65  }