Skip to content

Commit bc3efb2

Browse files
easyCZroboquat
authored andcommitted
[common-go] Add experiments test client
1 parent b413431 commit bc3efb2

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
10+
"github.com/gitpod-io/gitpod/common-go/experiments"
11+
)
12+
13+
type Client struct {
14+
BoolMatcher func(ctx context.Context, experiment string, defaultValue bool, attributes experiments.Attributes) bool
15+
IntMatcher func(ctx context.Context, experimentName string, defaultValue int, attributes experiments.Attributes) int
16+
FloatMatcher func(ctx context.Context, experimentName string, defaultValue float64, attributes experiments.Attributes) float64
17+
StringMatcher func(ctx context.Context, experimentName string, defaultValue string, attributes experiments.Attributes) string
18+
}
19+
20+
func (c *Client) GetBoolValue(ctx context.Context, experimentName string, defaultValue bool, attributes experiments.Attributes) bool {
21+
if c.BoolMatcher == nil {
22+
return defaultValue
23+
}
24+
return c.BoolMatcher(ctx, experimentName, defaultValue, attributes)
25+
}
26+
func (c *Client) GetIntValue(ctx context.Context, experimentName string, defaultValue int, attributes experiments.Attributes) int {
27+
if c.IntMatcher == nil {
28+
return defaultValue
29+
}
30+
return c.IntMatcher(ctx, experimentName, defaultValue, attributes)
31+
}
32+
func (c *Client) GetFloatValue(ctx context.Context, experimentName string, defaultValue float64, attributes experiments.Attributes) float64 {
33+
if c.FloatMatcher == nil {
34+
return defaultValue
35+
}
36+
return c.FloatMatcher(ctx, experimentName, defaultValue, attributes)
37+
}
38+
func (c *Client) GetStringValue(ctx context.Context, experimentName string, defaultValue string, attributes experiments.Attributes) string {
39+
if c.StringMatcher == nil {
40+
return defaultValue
41+
}
42+
return c.StringMatcher(ctx, experimentName, defaultValue, attributes)
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)