From 6375b3d87a57d58ed4cac9a7c4f22686ee0afa7c Mon Sep 17 00:00:00 2001 From: sighphyre Date: Fri, 8 Dec 2023 16:08:10 +0200 Subject: [PATCH] try different client test --- client_test.go | 179 +++++++++++++++++++++++++------------------------ 1 file changed, 91 insertions(+), 88 deletions(-) diff --git a/client_test.go b/client_test.go index 920ec0b..4a80bcc 100644 --- a/client_test.go +++ b/client_test.go @@ -1,62 +1,23 @@ package unleash import ( + "time" + "github.com/Unleash/unleash-client-go/v4/api" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "gopkg.in/h2non/gock.v1" ) -func TestClientWithoutListener(t *testing.T) { - assert := assert.New(t) - defer gock.OffAll() - - gock.New(mockerServer). - Post("/client/register"). - Reply(200) - - gock.New(mockerServer). - Get("/client/features"). - Reply(200). - JSON(api.FeatureResponse{}) - - client, err := NewClient( - WithUrl(mockerServer), - WithAppName(mockAppName), - WithInstanceId(mockInstanceId), - ) - assert.Nil(err, "client should not return an error") - - go func() { - for { - select { - case e := <-client.Errors(): - t.Errorf("Unexpected error: %v", e) - return - case w := <-client.Warnings(): - t.Errorf("Unexpected warning: %v", w) - return - case <-client.Count(): - case <-client.Sent(): - } - } - }() - <-client.Registered() - <-client.Ready() - client.Close() - assert.True(gock.IsDone(), "there should be no more mocks") -} - -// func TestClient_WithFallbackFunc(t *testing.T) { +// func TestClientWithoutListener(t *testing.T) { // assert := assert.New(t) // defer gock.OffAll() // gock.New(mockerServer). // Post("/client/register"). -// MatchHeader("UNLEASH-APPNAME", mockAppName). -// MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). // Reply(200) // gock.New(mockerServer). @@ -64,36 +25,34 @@ func TestClientWithoutListener(t *testing.T) { // Reply(200). // JSON(api.FeatureResponse{}) -// feature := "does_not_exist" - -// mockListener := &MockedListener{} -// mockListener.On("OnReady").Return() -// mockListener.On("OnRegistered", mock.AnythingOfType("ClientData")) -// mockListener.On("OnCount", feature, true).Return() -// mockListener.On("OnError").Return() - // client, err := NewClient( // WithUrl(mockerServer), // WithAppName(mockAppName), // WithInstanceId(mockInstanceId), -// WithListener(mockListener), // ) - -// assert.NoError(err) - -// client.WaitForReady() - -// fallback := func(f string, ctx *context.Context) bool { -// return f == feature -// } - -// isEnabled := client.IsEnabled("does_not_exist", WithFallbackFunc(fallback)) -// assert.True(isEnabled) - +// assert.Nil(err, "client should not return an error") + +// go func() { +// for { +// select { +// case e := <-client.Errors(): +// t.Errorf("Unexpected error: %v", e) +// return +// case w := <-client.Warnings(): +// t.Errorf("Unexpected warning: %v", w) +// return +// case <-client.Count(): +// case <-client.Sent(): +// } +// } +// }() +// <-client.Registered() +// <-client.Ready() +// client.Close() // assert.True(gock.IsDone(), "there should be no more mocks") // } -// func TestClient_WithResolver(t *testing.T) { +// func TestClient_WithFallbackFunc(t *testing.T) { // assert := assert.New(t) // defer gock.OffAll() @@ -108,7 +67,7 @@ func TestClientWithoutListener(t *testing.T) { // Reply(200). // JSON(api.FeatureResponse{}) -// const feature = "some_special_value" +// feature := "does_not_exist" // mockListener := &MockedListener{} // mockListener.On("OnReady").Return() @@ -127,35 +86,79 @@ func TestClientWithoutListener(t *testing.T) { // client.WaitForReady() -// resolver := func(featureName string) *api.Feature { -// if featureName == feature { -// return &api.Feature{ -// Name: "some_special_value-resolved", -// Description: "", -// Enabled: true, -// Strategies: []api.Strategy{ -// { -// Id: 1, -// Name: "default", -// }, -// }, -// CreatedAt: time.Time{}, -// Strategy: "default-strategy", -// Parameters: nil, -// Variants: nil, -// } -// } else { -// t.Fatalf("the feature name passed %s was not the expected one %s", featureName, feature) -// return nil -// } +// fallback := func(f string, ctx *context.Context) bool { +// return f == feature // } -// isEnabled := client.IsEnabled(feature, WithResolver(resolver)) +// isEnabled := client.IsEnabled("does_not_exist", WithFallbackFunc(fallback)) // assert.True(isEnabled) // assert.True(gock.IsDone(), "there should be no more mocks") // } +func TestClient_WithResolver(t *testing.T) { + assert := assert.New(t) + defer gock.OffAll() + + gock.New(mockerServer). + Post("/client/register"). + MatchHeader("UNLEASH-APPNAME", mockAppName). + MatchHeader("UNLEASH-INSTANCEID", mockInstanceId). + Reply(200) + + gock.New(mockerServer). + Get("/client/features"). + Reply(200). + JSON(api.FeatureResponse{}) + + const feature = "some_special_value" + + mockListener := &MockedListener{} + mockListener.On("OnReady").Return() + mockListener.On("OnRegistered", mock.AnythingOfType("ClientData")) + mockListener.On("OnCount", feature, true).Return() + mockListener.On("OnError").Return() + + client, err := NewClient( + WithUrl(mockerServer), + WithAppName(mockAppName), + WithInstanceId(mockInstanceId), + WithListener(mockListener), + ) + + assert.NoError(err) + + client.WaitForReady() + + resolver := func(featureName string) *api.Feature { + if featureName == feature { + return &api.Feature{ + Name: "some_special_value-resolved", + Description: "", + Enabled: true, + Strategies: []api.Strategy{ + { + Id: 1, + Name: "default", + }, + }, + CreatedAt: time.Time{}, + Strategy: "default-strategy", + Parameters: nil, + Variants: nil, + } + } else { + t.Fatalf("the feature name passed %s was not the expected one %s", featureName, feature) + return nil + } + } + + isEnabled := client.IsEnabled(feature, WithResolver(resolver)) + assert.True(isEnabled) + + assert.True(gock.IsDone(), "there should be no more mocks") +} + // func TestClient_ListFeatures(t *testing.T) { // assert := assert.New(t) // defer gock.OffAll()