blob: 31dcb455a2d67b8667adc0a9c3ab2d82429a8365 [file] [log] [blame]
// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package experiments
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
exp1 = Register("exp1")
exp2 = Register("exp2")
)
func TestExperiments(t *testing.T) {
t.Parallel()
Convey("Works", t, func() {
e1, ok := GetByName("exp1")
So(ok, ShouldBeTrue)
So(e1, ShouldResemble, exp1)
So(e1.Valid(), ShouldBeTrue)
bad, ok := GetByName("bad")
So(ok, ShouldBeFalse)
So(bad.Valid(), ShouldBeFalse)
ctx := context.Background()
So(exp1.Enabled(ctx), ShouldBeFalse)
So(exp2.Enabled(ctx), ShouldBeFalse)
ctx = Enable(ctx) // noop
So(exp1.Enabled(ctx), ShouldBeFalse)
So(exp2.Enabled(ctx), ShouldBeFalse)
ctx = Enable(ctx, exp1)
So(exp1.Enabled(ctx), ShouldBeTrue)
So(exp2.Enabled(ctx), ShouldBeFalse)
ctx = Enable(ctx, exp1) // noop
So(exp1.Enabled(ctx), ShouldBeTrue)
So(exp2.Enabled(ctx), ShouldBeFalse)
ctx2 := Enable(ctx, exp2)
So(exp1.Enabled(ctx2), ShouldBeTrue)
So(exp2.Enabled(ctx2), ShouldBeTrue)
// Hasn't touched the existing context.
So(exp2.Enabled(ctx), ShouldBeFalse)
})
}