Skip to content

Commit e1dcb63

Browse files
committed
mock feature flag hang
1 parent 2518798 commit e1dcb63

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

components/service-waiter/cmd/component_test.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func Test_actualWaitFeatureFlag(t *testing.T) {
2727
values []*string
2828
defaultValue bool
2929
nilClient bool
30+
clientHanging time.Duration
3031
}
3132
tests := []struct {
3233
name string
@@ -42,6 +43,7 @@ func Test_actualWaitFeatureFlag(t *testing.T) {
4243
values: []*string{nil, nil, &trueValue},
4344
defaultValue: false,
4445
nilClient: false,
46+
clientHanging: time.Second * 0,
4547
},
4648
wantFlagValue: true,
4749
wantOk: true,
@@ -54,6 +56,20 @@ func Test_actualWaitFeatureFlag(t *testing.T) {
5456
values: []*string{nil, nil, nil, &falseValue},
5557
defaultValue: false,
5658
nilClient: false,
59+
clientHanging: time.Second * 0,
60+
},
61+
wantFlagValue: false,
62+
wantOk: true,
63+
fetchTimes: 4,
64+
},
65+
{
66+
name: "happy path 3",
67+
args: args{
68+
timeoutDuration: baseDuration * 1000,
69+
values: []*string{nil, nil, nil, &falseValue},
70+
defaultValue: false,
71+
nilClient: false,
72+
clientHanging: baseDuration * 10,
5773
},
5874
wantFlagValue: false,
5975
wantOk: true,
@@ -66,6 +82,7 @@ func Test_actualWaitFeatureFlag(t *testing.T) {
6682
values: []*string{nil, nil, nil, &unknownValue, &falseValue},
6783
defaultValue: false,
6884
nilClient: false,
85+
clientHanging: time.Second * 0,
6986
},
7087
wantFlagValue: false,
7188
wantOk: true,
@@ -78,6 +95,7 @@ func Test_actualWaitFeatureFlag(t *testing.T) {
7895
values: []*string{nil, nil, nil, &unknownValue, &falseValue},
7996
defaultValue: false,
8097
nilClient: true,
98+
clientHanging: time.Second * 0,
8199
},
82100
wantFlagValue: false,
83101
wantOk: false,
@@ -90,58 +108,75 @@ func Test_actualWaitFeatureFlag(t *testing.T) {
90108
values: []*string{nil, nil, nil, &unknownValue, &falseValue},
91109
defaultValue: true,
92110
nilClient: true,
111+
clientHanging: time.Second * 0,
93112
},
94113
wantFlagValue: true,
95114
wantOk: false,
96115
fetchTimes: 0,
97116
},
98117
{
99-
name: "timed out with default value: true",
118+
name: "ctx timed out should get default value: true",
100119
args: args{
101120
timeoutDuration: baseDuration * 30,
102121
values: []*string{nil, nil, nil, &unknownValue, &falseValue},
103122
defaultValue: true,
104123
nilClient: false,
124+
clientHanging: time.Second * 0,
105125
},
106126
wantFlagValue: true,
107127
wantOk: false,
108128
fetchTimes: 2,
109129
},
110130
{
111-
name: "timed out with default value: false",
131+
name: "ctx timed out should get default value: false",
112132
args: args{
113133
timeoutDuration: baseDuration * 30,
114134
values: []*string{nil, nil, nil, &unknownValue, &falseValue},
115135
defaultValue: false,
116136
nilClient: false,
137+
clientHanging: time.Second * 0,
117138
},
118139
wantFlagValue: false,
119140
wantOk: false,
120141
fetchTimes: 2,
121142
},
143+
{
144+
name: "client hanging should get default value",
145+
args: args{
146+
timeoutDuration: baseDuration * 30,
147+
values: []*string{nil, nil, nil, &unknownValue, &falseValue},
148+
defaultValue: true,
149+
nilClient: false,
150+
clientHanging: baseDuration * 60,
151+
},
152+
wantFlagValue: true,
153+
wantOk: false,
154+
fetchTimes: 1,
155+
},
122156
}
123157
for _, tt := range tests {
124158
t.Run(tt.name, func(t *testing.T) {
125159
ctx, cancel := context.WithTimeout(context.Background(), tt.args.timeoutDuration)
126160
defer cancel()
127-
client := newMockClient(tt.args.values, tt.args.nilClient)
128-
gotFlagValue, gotOk, gotWaitTimes := cmd.ActualWaitFeatureFlag(ctx, client, tt.args.defaultValue)
161+
client := newMockClient(tt.args.values, tt.args.nilClient, tt.args.clientHanging)
162+
gotFlagValue, gotOk, gotFetchTimes := cmd.ActualWaitFeatureFlag(ctx, client, tt.args.defaultValue)
129163
if gotFlagValue != tt.wantFlagValue {
130164
t.Errorf("actualWaitFeatureFlag() gotFlagValue = %v, want %v", gotFlagValue, tt.wantFlagValue)
131165
}
132166
if gotOk != tt.wantOk {
133167
t.Errorf("actualWaitFeatureFlag() gotOk = %v, want %v", gotOk, tt.wantOk)
134168
}
135-
if gotWaitTimes != tt.fetchTimes {
136-
t.Errorf("actualWaitFeatureFlag() gotWaitTimes = %v, want %v", gotWaitTimes, tt.fetchTimes)
169+
if gotFetchTimes != tt.fetchTimes {
170+
t.Errorf("actualWaitFeatureFlag() gotFetchTimes = %v, want %v", gotFetchTimes, tt.fetchTimes)
137171
}
138172
})
139173
}
140174
}
141175

142176
type mockClient struct {
143-
values []*string
144-
i int
177+
values []*string
178+
i int
179+
hangingDuration time.Duration
145180
}
146181

147182
// GetBoolValue implements experiments.Client.
@@ -159,18 +194,19 @@ func (*mockClient) GetIntValue(ctx context.Context, experimentName string, defau
159194
panic("unimplemented")
160195
}
161196

162-
func newMockClient(values []*string, isNil bool) experiments.Client {
197+
func newMockClient(values []*string, isNil bool, hangingDuration time.Duration) experiments.Client {
163198
if isNil {
164199
return nil
165200
}
166-
return &mockClient{values: values, i: 0}
201+
return &mockClient{values: values, i: 0, hangingDuration: hangingDuration}
167202
}
168203

169204
// GetStringValue implements experiments.Client.
170205
func (c *mockClient) GetStringValue(ctx context.Context, experimentName string, defaultValue string, attributes experiments.Attributes) string {
171206
defer func() {
172207
c.i += 1
173208
}()
209+
time.Sleep(c.hangingDuration)
174210
if c.i >= len(c.values) {
175211
return defaultValue
176212
}

0 commit comments

Comments
 (0)