Skip to content

Commit c3f9e57

Browse files
committed
added shutdown test confirming method run in MP when called
Signed-off-by: bbland1 <[email protected]>
1 parent cefca09 commit c3f9e57

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

providers/multi-provider/pkg/providers_test.go

+56-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
type MockProvider struct {
1515
oft.TestProvider
1616
InitCount *int
17+
ShutCount *int
1718
}
1819

1920
func (m *MockProvider) Init(evalCtx openfeature.EvaluationContext) error {
@@ -22,19 +23,22 @@ func (m *MockProvider) Init(evalCtx openfeature.EvaluationContext) error {
2223
}
2324

2425
func (m *MockProvider) Shutdown() {
26+
*m.ShutCount += 1
2527
}
2628

27-
func NewMockProvider(initCount *int) *MockProvider {
29+
func NewMockProvider(initCount *int, shutCount *int) *MockProvider {
2830
return &MockProvider{
2931
TestProvider: oft.NewTestProvider(),
30-
InitCount: initCount,
32+
InitCount: initCount,
33+
ShutCount: shutCount,
3134
}
3235
}
3336

3437
// MockProviderDelay utilizes openfeature's TestProvider to add testable Init & Shutdown methods to test the MultiProvider functionality with a small delay making sure the the go routines properly wait.
3538
type MockProviderDelay struct {
3639
oft.TestProvider
3740
InitCount *int
41+
ShutCount *int
3842
}
3943

4044
func (m *MockProviderDelay) Init(evalCtx openfeature.EvaluationContext) error {
@@ -44,12 +48,15 @@ func (m *MockProviderDelay) Init(evalCtx openfeature.EvaluationContext) error {
4448
}
4549

4650
func (m *MockProviderDelay) Shutdown() {
51+
time.Sleep(2 * time.Millisecond)
52+
*m.ShutCount += 1
4753
}
4854

49-
func NewMockProviderDelay(initCount *int) *MockProviderDelay {
55+
func NewMockProviderDelay(initCount *int, shutCount *int) *MockProviderDelay {
5056
return &MockProviderDelay{
5157
TestProvider: oft.NewTestProvider(),
52-
InitCount: initCount,
58+
InitCount: initCount,
59+
ShutCount: shutCount,
5360
}
5461
}
5562

@@ -223,10 +230,11 @@ func TestMultiProvider_MetaData(t *testing.T) {
223230

224231
func TestMultiProvider_Init(t *testing.T) {
225232
initializations := 0
233+
shutdowns := 0
226234

227-
testProvider1 := NewMockProvider(&initializations)
235+
testProvider1 := NewMockProvider(&initializations, &shutdowns)
228236
testProvider2 := oft.NewTestProvider()
229-
testProvider3 := NewMockProviderDelay(&initializations)
237+
testProvider3 := NewMockProviderDelay(&initializations, &shutdowns)
230238

231239
defaultLogger, err := hooks.NewLoggingHook(false)
232240
if err != nil {
@@ -240,7 +248,7 @@ func TestMultiProvider_Init(t *testing.T) {
240248
}, {
241249
Provider: testProvider2,
242250
UniqueName: "provider2",
243-
},{
251+
}, {
244252
Provider: testProvider3,
245253
UniqueName: "provider3",
246254
},
@@ -270,6 +278,47 @@ func TestMultiProvider_Init(t *testing.T) {
270278

271279
}
272280

281+
func TestMultiProvider_Shutdown(t *testing.T) {
282+
initializations := 0
283+
shutdowns := 0
284+
285+
testProvider1 := NewMockProvider(&initializations, &shutdowns)
286+
testProvider2 := oft.NewTestProvider()
287+
testProvider3 := NewMockProviderDelay(&initializations, &shutdowns)
288+
289+
defaultLogger, err := hooks.NewLoggingHook(false)
290+
if err != nil {
291+
t.Errorf("Issue setting up logger,'%s'", err)
292+
}
293+
294+
mp, err := NewMultiProvider([]UniqueNameProvider{
295+
{
296+
Provider: testProvider1,
297+
UniqueName: "provider1",
298+
}, {
299+
Provider: testProvider2,
300+
UniqueName: "provider2",
301+
}, {
302+
Provider: testProvider3,
303+
UniqueName: "provider3",
304+
},
305+
}, "test", defaultLogger)
306+
307+
if err != nil {
308+
t.Errorf("Expected the multiprovider to successfully make an instance, '%s'", err)
309+
}
310+
311+
mp.Shutdown()
312+
313+
if shutdowns == 0 {
314+
t.Errorf("Expected there to be shutdowns, but none were ran.")
315+
}
316+
317+
if shutdowns != 2 {
318+
t.Errorf("Expected there to be '2' shutdown steps ran, but got: '%d'.", shutdowns)
319+
}
320+
}
321+
273322
func TestNewMultiProvider_ProviderUniqueNames(t *testing.T) {
274323
testProvider1 := memprovider.NewInMemoryProvider(map[string]memprovider.InMemoryFlag{
275324
"boolFlag": {

0 commit comments

Comments
 (0)