Skip to content

Commit

Permalink
try different client test
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Dec 8, 2023
1 parent e9dc290 commit 6375b3d
Showing 1 changed file with 91 additions and 88 deletions.
179 changes: 91 additions & 88 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,58 @@
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).
// Get("/client/features").
// 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()

Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 6375b3d

Please sign in to comment.