|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package experimentstest |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gitpod-io/gitpod/common-go/experiments" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestClient(t *testing.T) { |
| 16 | + ctx := context.Background() |
| 17 | + experimentName := "foo_bar" |
| 18 | + |
| 19 | + client := &Client{ |
| 20 | + BoolMatcher: func(ctx context.Context, experiment string, defaultValue bool, attributes experiments.Attributes) bool { |
| 21 | + if experiment == experimentName { |
| 22 | + return true |
| 23 | + } |
| 24 | + return defaultValue |
| 25 | + }, |
| 26 | + IntMatcher: func(ctx context.Context, experiment string, defaultValue int, attributes experiments.Attributes) int { |
| 27 | + if experiment == experimentName { |
| 28 | + return 7 |
| 29 | + } |
| 30 | + return defaultValue |
| 31 | + }, |
| 32 | + FloatMatcher: func(ctx context.Context, experiment string, defaultValue float64, attributes experiments.Attributes) float64 { |
| 33 | + if experiment == experimentName { |
| 34 | + return 133.7 |
| 35 | + } |
| 36 | + return defaultValue |
| 37 | + }, |
| 38 | + StringMatcher: func(ctx context.Context, experiment, defaultValue string, attributes experiments.Attributes) string { |
| 39 | + if experiment == experimentName { |
| 40 | + return "foo" |
| 41 | + } |
| 42 | + return defaultValue |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + require.Equal(t, false, client.GetBoolValue(ctx, "random", false, experiments.Attributes{})) |
| 47 | + require.Equal(t, true, client.GetBoolValue(ctx, experimentName, false, experiments.Attributes{})) |
| 48 | + |
| 49 | + require.Equal(t, 1, client.GetIntValue(ctx, "random", 1, experiments.Attributes{})) |
| 50 | + require.Equal(t, 7, client.GetIntValue(ctx, experimentName, 1, experiments.Attributes{})) |
| 51 | + |
| 52 | + require.Equal(t, 1.2, client.GetFloatValue(ctx, "random", 1.2, experiments.Attributes{})) |
| 53 | + require.Equal(t, 133.7, client.GetFloatValue(ctx, experimentName, 1, experiments.Attributes{})) |
| 54 | + |
| 55 | + require.Equal(t, "bar", client.GetStringValue(ctx, "random", "bar", experiments.Attributes{})) |
| 56 | + require.Equal(t, "foo", client.GetStringValue(ctx, experimentName, "bar", experiments.Attributes{})) |
| 57 | +} |
0 commit comments